SlideShare a Scribd company logo
1 of 27
David Luebke 1 8/29/2022
CS 332: Algorithms
Merge Sort
Solving Recurrences
The Master Theorem
David Luebke 2 8/29/2022
Administrative Question
 Who here cannot make Monday-Wednesday
office hours at 10 AM?
 If nobody, should we change class time?
David Luebke 3 8/29/2022
Homework 1
 Homework 1 will be posted later today
 (Problem with the exercise numbering, sorry)
 Due Monday Jan 28 at 9 AM
 Should be a fairly simple warm-up problem set
David Luebke 4 8/29/2022
Review: Asymptotic Notation
 Upper Bound Notation:
 f(n) is O(g(n)) if there exist positive constants c
and n0 such that f(n)  c  g(n) for all n  n0
 Formally, O(g(n)) = { f(n):  positive constants c
and n0 such that f(n)  c  g(n)  n  n0
 Big O fact:
 A polynomial of degree k is O(nk)
David Luebke 5 8/29/2022
Review: Asymptotic Notation
 Asymptotic lower bound:
 f(n) is (g(n)) if  positive constants c and n0 such
that 0  cg(n)  f(n)  n  n0
 Asymptotic tight bound:
 f(n) is (g(n)) if  positive constants c1, c2, and n0
such that c1 g(n)  f(n)  c2 g(n)  n  n0
 f(n) = (g(n)) if and only if
f(n) = O(g(n)) AND f(n) = (g(n))
David Luebke 6 8/29/2022
Other Asymptotic Notations
 A function f(n) is o(g(n)) if  positive
constants c and n0 such that
f(n) < c g(n)  n  n0
 A function f(n) is (g(n)) if  positive
constants c and n0 such that
c g(n) < f(n)  n  n0
 Intuitively,
 o() is like <
 O() is like 
 () is like >
 () is like 
 () is like =
David Luebke 7 8/29/2022
Merge Sort
MergeSort(A, left, right) {
if (left < right) {
mid = floor((left + right) / 2);
MergeSort(A, left, mid);
MergeSort(A, mid+1, right);
Merge(A, left, mid, right);
}
}
// Merge() takes two sorted subarrays of A and
// merges them into a single sorted subarray of A
// (how long should this take?)
David Luebke 8 8/29/2022
Merge Sort: Example
 Show MergeSort() running on the array
A = {10, 5, 7, 6, 1, 4, 8, 3, 2, 9};
David Luebke 9 8/29/2022
Analysis of Merge Sort
Statement Effort
 So T(n) = (1) when n = 1, and
2T(n/2) + (n) when n > 1
 So what (more succinctly) is T(n)?
MergeSort(A, left, right) { T(n)
if (left < right) { (1)
mid = floor((left + right) / 2); (1)
MergeSort(A, left, mid); T(n/2)
MergeSort(A, mid+1, right); T(n/2)
Merge(A, left, mid, right); (n)
}
}
David Luebke 10 8/29/2022
Recurrences
 The expression:
is a recurrence.
 Recurrence: an equation that describes a function
in terms of its value on smaller functions

















1
2
2
1
)
(
n
cn
n
T
n
c
n
T
David Luebke 11 8/29/2022
Recurrence Examples








0
0
)
1
(
0
)
(
n
n
n
s
c
n
s








0
)
1
(
0
0
)
(
n
n
s
n
n
n
s

















1
2
2
1
)
(
n
c
n
T
n
c
n
T

















1
1
)
(
n
cn
b
n
aT
n
c
n
T
David Luebke 12 8/29/2022
Solving Recurrences
 Substitution method
 Iteration method
 Master method
David Luebke 13 8/29/2022
Solving Recurrences
 The substitution method (CLR 4.1)
 A.k.a. the “making a good guess method”
 Guess the form of the answer, then use induction
to find the constants and show that solution works
 Examples:
 T(n) = 2T(n/2) + (n)  T(n) = (n lg n)
 T(n) = 2T(n/2) + n  ???
David Luebke 14 8/29/2022
Solving Recurrences
 The substitution method (CLR 4.1)
 A.k.a. the “making a good guess method”
 Guess the form of the answer, then use induction
to find the constants and show that solution works
 Examples:
 T(n) = 2T(n/2) + (n)  T(n) = (n lg n)
 T(n) = 2T(n/2) + n  T(n) = (n lg n)
 T(n) = 2T(n/2 )+ 17) + n  ???
David Luebke 15 8/29/2022
Solving Recurrences
 The substitution method (CLR 4.1)
 A.k.a. the “making a good guess method”
 Guess the form of the answer, then use induction
to find the constants and show that solution works
 Examples:
 T(n) = 2T(n/2) + (n)  T(n) = (n lg n)
 T(n) = 2T(n/2) + n  T(n) = (n lg n)
 T(n) = 2T(n/2+ 17) + n  (n lg n)
David Luebke 16 8/29/2022
Solving Recurrences
 Another option is what the book calls the
“iteration method”
 Expand the recurrence
 Work some algebra to express as a summation
 Evaluate the summation
 We will show several examples
David Luebke 17 8/29/2022
 s(n) =
c + s(n-1)
c + c + s(n-2)
2c + s(n-2)
2c + c + s(n-3)
3c + s(n-3)
…
kc + s(n-k) = ck + s(n-k)








0
)
1
(
0
0
)
(
n
n
s
c
n
n
s
David Luebke 18 8/29/2022
 So far for n >= k we have
 s(n) = ck + s(n-k)
 What if k = n?
 s(n) = cn + s(0) = cn








0
)
1
(
0
0
)
(
n
n
s
c
n
n
s
David Luebke 19 8/29/2022
 So far for n >= k we have
 s(n) = ck + s(n-k)
 What if k = n?
 s(n) = cn + s(0) = cn
 So
 Thus in general
 s(n) = cn








0
)
1
(
0
0
)
(
n
n
s
c
n
n
s








0
)
1
(
0
0
)
(
n
n
s
c
n
n
s
David Luebke 20 8/29/2022
 s(n)
= n + s(n-1)
= n + n-1 + s(n-2)
= n + n-1 + n-2 + s(n-3)
= n + n-1 + n-2 + n-3 + s(n-4)
= …
= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)








0
)
1
(
0
0
)
(
n
n
s
n
n
n
s
David Luebke 21 8/29/2022
 s(n)
= n + s(n-1)
= n + n-1 + s(n-2)
= n + n-1 + n-2 + s(n-3)
= n + n-1 + n-2 + n-3 + s(n-4)
= …
= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)
=








0
)
1
(
0
0
)
(
n
n
s
n
n
n
s
)
(
1
k
n
s
i
n
k
n
i





David Luebke 22 8/29/2022
 So far for n >= k we have








0
)
1
(
0
0
)
(
n
n
s
n
n
n
s
)
(
1
k
n
s
i
n
k
n
i





David Luebke 23 8/29/2022
 So far for n >= k we have
 What if k = n?








0
)
1
(
0
0
)
(
n
n
s
n
n
n
s
)
(
1
k
n
s
i
n
k
n
i





David Luebke 24 8/29/2022
 So far for n >= k we have
 What if k = n?








0
)
1
(
0
0
)
(
n
n
s
n
n
n
s
)
(
1
k
n
s
i
n
k
n
i





2
1
0
)
0
(
1
1




 
 

n
n
i
s
i
n
i
n
i
David Luebke 25 8/29/2022
 So far for n >= k we have
 What if k = n?
 Thus in general








0
)
1
(
0
0
)
(
n
n
s
n
n
n
s
)
(
1
k
n
s
i
n
k
n
i





2
1
0
)
0
(
1
1




 
 

n
n
i
s
i
n
i
n
i
2
1
)
(


n
n
n
s
David Luebke 26 8/29/2022
 T(n) =
2T(n/2) + c
2(2T(n/2/2) + c) + c
22T(n/22) + 2c + c
22(2T(n/22/2) + c) + 3c
23T(n/23) + 4c + 3c
23T(n/23) + 7c
23(2T(n/23/2) + c) + 7c
24T(n/24) + 15c
…
2kT(n/2k) + (2k - 1)c














 1
2
2
1
)
( n
c
n
T
n
c
n
T
David Luebke 27 8/29/2022
 So far for n > 2k we have
 T(n) = 2kT(n/2k) + (2k - 1)c
 What if k = lg n?
 T(n) = 2lg n T(n/2lg n) + (2lg n - 1)c
= n T(n/n) + (n - 1)c
= n T(1) + (n-1)c
= nc + (n-1)c = (2n - 1)c














 1
2
2
1
)
( n
c
n
T
n
c
n
T

More Related Content

Similar to CS 332 Algorithms: Merge Sort Recurrence Solving

Lecture1-Architecture
Lecture1-ArchitectureLecture1-Architecture
Lecture1-ArchitectureDanny Albay
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
Observations On X2 + Y2 = 2Z2 - 62W2
Observations On  X2 + Y2 = 2Z2 - 62W2Observations On  X2 + Y2 = 2Z2 - 62W2
Observations On X2 + Y2 = 2Z2 - 62W2IRJET Journal
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika MamaMa28
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
All homework and exams in one file.pdf
All homework and exams in one file.pdfAll homework and exams in one file.pdf
All homework and exams in one file.pdfAnn Wera
 
differential-calculus-1-23.pdf
differential-calculus-1-23.pdfdifferential-calculus-1-23.pdf
differential-calculus-1-23.pdfIILSASTOWER
 
ECES302 Exam 1 Solutions
ECES302 Exam 1 SolutionsECES302 Exam 1 Solutions
ECES302 Exam 1 SolutionsThomas Woo
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013Gary Short
 
Mathematical Induction
Mathematical InductionMathematical Induction
Mathematical InductionEdelyn Cagas
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfShiwani Gupta
 
Lec001 math1 Fall 2021 .pdf
Lec001 math1 Fall 2021 .pdfLec001 math1 Fall 2021 .pdf
Lec001 math1 Fall 2021 .pdfYassinAlaraby
 
Question bank Engineering Mathematics- ii
Question bank Engineering Mathematics- ii Question bank Engineering Mathematics- ii
Question bank Engineering Mathematics- ii Mohammad Imran
 

Similar to CS 332 Algorithms: Merge Sort Recurrence Solving (20)

Lecture1-Architecture
Lecture1-ArchitectureLecture1-Architecture
Lecture1-Architecture
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Observations On X2 + Y2 = 2Z2 - 62W2
Observations On  X2 + Y2 = 2Z2 - 62W2Observations On  X2 + Y2 = 2Z2 - 62W2
Observations On X2 + Y2 = 2Z2 - 62W2
 
Jurnal informatika
Jurnal informatika Jurnal informatika
Jurnal informatika
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
All homework and exams in one file.pdf
All homework and exams in one file.pdfAll homework and exams in one file.pdf
All homework and exams in one file.pdf
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
Recurrences
RecurrencesRecurrences
Recurrences
 
differential-calculus-1-23.pdf
differential-calculus-1-23.pdfdifferential-calculus-1-23.pdf
differential-calculus-1-23.pdf
 
ECES302 Exam 1 Solutions
ECES302 Exam 1 SolutionsECES302 Exam 1 Solutions
ECES302 Exam 1 Solutions
 
Algorithms - Rocksolid Tour 2013
Algorithms  - Rocksolid Tour 2013Algorithms  - Rocksolid Tour 2013
Algorithms - Rocksolid Tour 2013
 
Mathematical Induction
Mathematical InductionMathematical Induction
Mathematical Induction
 
Recurrences
RecurrencesRecurrences
Recurrences
 
module2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdfmodule2_dIVIDEncONQUER_2022.pdf
module2_dIVIDEncONQUER_2022.pdf
 
Lec001 math1 Fall 2021 .pdf
Lec001 math1 Fall 2021 .pdfLec001 math1 Fall 2021 .pdf
Lec001 math1 Fall 2021 .pdf
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
 
Question bank Engineering Mathematics- ii
Question bank Engineering Mathematics- ii Question bank Engineering Mathematics- ii
Question bank Engineering Mathematics- ii
 
Fdtd
FdtdFdtd
Fdtd
 
Laplace_1.ppt
Laplace_1.pptLaplace_1.ppt
Laplace_1.ppt
 

Recently uploaded

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 

Recently uploaded (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 

CS 332 Algorithms: Merge Sort Recurrence Solving

  • 1. David Luebke 1 8/29/2022 CS 332: Algorithms Merge Sort Solving Recurrences The Master Theorem
  • 2. David Luebke 2 8/29/2022 Administrative Question  Who here cannot make Monday-Wednesday office hours at 10 AM?  If nobody, should we change class time?
  • 3. David Luebke 3 8/29/2022 Homework 1  Homework 1 will be posted later today  (Problem with the exercise numbering, sorry)  Due Monday Jan 28 at 9 AM  Should be a fairly simple warm-up problem set
  • 4. David Luebke 4 8/29/2022 Review: Asymptotic Notation  Upper Bound Notation:  f(n) is O(g(n)) if there exist positive constants c and n0 such that f(n)  c  g(n) for all n  n0  Formally, O(g(n)) = { f(n):  positive constants c and n0 such that f(n)  c  g(n)  n  n0  Big O fact:  A polynomial of degree k is O(nk)
  • 5. David Luebke 5 8/29/2022 Review: Asymptotic Notation  Asymptotic lower bound:  f(n) is (g(n)) if  positive constants c and n0 such that 0  cg(n)  f(n)  n  n0  Asymptotic tight bound:  f(n) is (g(n)) if  positive constants c1, c2, and n0 such that c1 g(n)  f(n)  c2 g(n)  n  n0  f(n) = (g(n)) if and only if f(n) = O(g(n)) AND f(n) = (g(n))
  • 6. David Luebke 6 8/29/2022 Other Asymptotic Notations  A function f(n) is o(g(n)) if  positive constants c and n0 such that f(n) < c g(n)  n  n0  A function f(n) is (g(n)) if  positive constants c and n0 such that c g(n) < f(n)  n  n0  Intuitively,  o() is like <  O() is like   () is like >  () is like   () is like =
  • 7. David Luebke 7 8/29/2022 Merge Sort MergeSort(A, left, right) { if (left < right) { mid = floor((left + right) / 2); MergeSort(A, left, mid); MergeSort(A, mid+1, right); Merge(A, left, mid, right); } } // Merge() takes two sorted subarrays of A and // merges them into a single sorted subarray of A // (how long should this take?)
  • 8. David Luebke 8 8/29/2022 Merge Sort: Example  Show MergeSort() running on the array A = {10, 5, 7, 6, 1, 4, 8, 3, 2, 9};
  • 9. David Luebke 9 8/29/2022 Analysis of Merge Sort Statement Effort  So T(n) = (1) when n = 1, and 2T(n/2) + (n) when n > 1  So what (more succinctly) is T(n)? MergeSort(A, left, right) { T(n) if (left < right) { (1) mid = floor((left + right) / 2); (1) MergeSort(A, left, mid); T(n/2) MergeSort(A, mid+1, right); T(n/2) Merge(A, left, mid, right); (n) } }
  • 10. David Luebke 10 8/29/2022 Recurrences  The expression: is a recurrence.  Recurrence: an equation that describes a function in terms of its value on smaller functions                  1 2 2 1 ) ( n cn n T n c n T
  • 11. David Luebke 11 8/29/2022 Recurrence Examples         0 0 ) 1 ( 0 ) ( n n n s c n s         0 ) 1 ( 0 0 ) ( n n s n n n s                  1 2 2 1 ) ( n c n T n c n T                  1 1 ) ( n cn b n aT n c n T
  • 12. David Luebke 12 8/29/2022 Solving Recurrences  Substitution method  Iteration method  Master method
  • 13. David Luebke 13 8/29/2022 Solving Recurrences  The substitution method (CLR 4.1)  A.k.a. the “making a good guess method”  Guess the form of the answer, then use induction to find the constants and show that solution works  Examples:  T(n) = 2T(n/2) + (n)  T(n) = (n lg n)  T(n) = 2T(n/2) + n  ???
  • 14. David Luebke 14 8/29/2022 Solving Recurrences  The substitution method (CLR 4.1)  A.k.a. the “making a good guess method”  Guess the form of the answer, then use induction to find the constants and show that solution works  Examples:  T(n) = 2T(n/2) + (n)  T(n) = (n lg n)  T(n) = 2T(n/2) + n  T(n) = (n lg n)  T(n) = 2T(n/2 )+ 17) + n  ???
  • 15. David Luebke 15 8/29/2022 Solving Recurrences  The substitution method (CLR 4.1)  A.k.a. the “making a good guess method”  Guess the form of the answer, then use induction to find the constants and show that solution works  Examples:  T(n) = 2T(n/2) + (n)  T(n) = (n lg n)  T(n) = 2T(n/2) + n  T(n) = (n lg n)  T(n) = 2T(n/2+ 17) + n  (n lg n)
  • 16. David Luebke 16 8/29/2022 Solving Recurrences  Another option is what the book calls the “iteration method”  Expand the recurrence  Work some algebra to express as a summation  Evaluate the summation  We will show several examples
  • 17. David Luebke 17 8/29/2022  s(n) = c + s(n-1) c + c + s(n-2) 2c + s(n-2) 2c + c + s(n-3) 3c + s(n-3) … kc + s(n-k) = ck + s(n-k)         0 ) 1 ( 0 0 ) ( n n s c n n s
  • 18. David Luebke 18 8/29/2022  So far for n >= k we have  s(n) = ck + s(n-k)  What if k = n?  s(n) = cn + s(0) = cn         0 ) 1 ( 0 0 ) ( n n s c n n s
  • 19. David Luebke 19 8/29/2022  So far for n >= k we have  s(n) = ck + s(n-k)  What if k = n?  s(n) = cn + s(0) = cn  So  Thus in general  s(n) = cn         0 ) 1 ( 0 0 ) ( n n s c n n s         0 ) 1 ( 0 0 ) ( n n s c n n s
  • 20. David Luebke 20 8/29/2022  s(n) = n + s(n-1) = n + n-1 + s(n-2) = n + n-1 + n-2 + s(n-3) = n + n-1 + n-2 + n-3 + s(n-4) = … = n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)         0 ) 1 ( 0 0 ) ( n n s n n n s
  • 21. David Luebke 21 8/29/2022  s(n) = n + s(n-1) = n + n-1 + s(n-2) = n + n-1 + n-2 + s(n-3) = n + n-1 + n-2 + n-3 + s(n-4) = … = n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k) =         0 ) 1 ( 0 0 ) ( n n s n n n s ) ( 1 k n s i n k n i     
  • 22. David Luebke 22 8/29/2022  So far for n >= k we have         0 ) 1 ( 0 0 ) ( n n s n n n s ) ( 1 k n s i n k n i     
  • 23. David Luebke 23 8/29/2022  So far for n >= k we have  What if k = n?         0 ) 1 ( 0 0 ) ( n n s n n n s ) ( 1 k n s i n k n i     
  • 24. David Luebke 24 8/29/2022  So far for n >= k we have  What if k = n?         0 ) 1 ( 0 0 ) ( n n s n n n s ) ( 1 k n s i n k n i      2 1 0 ) 0 ( 1 1          n n i s i n i n i
  • 25. David Luebke 25 8/29/2022  So far for n >= k we have  What if k = n?  Thus in general         0 ) 1 ( 0 0 ) ( n n s n n n s ) ( 1 k n s i n k n i      2 1 0 ) 0 ( 1 1          n n i s i n i n i 2 1 ) (   n n n s
  • 26. David Luebke 26 8/29/2022  T(n) = 2T(n/2) + c 2(2T(n/2/2) + c) + c 22T(n/22) + 2c + c 22(2T(n/22/2) + c) + 3c 23T(n/23) + 4c + 3c 23T(n/23) + 7c 23(2T(n/23/2) + c) + 7c 24T(n/24) + 15c … 2kT(n/2k) + (2k - 1)c                1 2 2 1 ) ( n c n T n c n T
  • 27. David Luebke 27 8/29/2022  So far for n > 2k we have  T(n) = 2kT(n/2k) + (2k - 1)c  What if k = lg n?  T(n) = 2lg n T(n/2lg n) + (2lg n - 1)c = n T(n/n) + (n - 1)c = n T(1) + (n-1)c = nc + (n-1)c = (2n - 1)c                1 2 2 1 ) ( n c n T n c n T