SlideShare a Scribd company logo
1 of 30
Introduction to Algorithms
6.046J
Lecture 2
Prof. Shafi Goldwasser
L2.2
Solving recurrences
• The analysis of integer multiplication
from Lecture 1 required us to solve a
recurrence.
• Recurrences are a major tool for
analysis of algorithms
• Today: Learn a few methods.
• Lecture 3: Divide and Conquer
algorithms which are analyzable by
recurrences.
L2.3
Recall: Integer Multiplication
• Let X = A B and Y = C D where A,B,C
and D are n/2 bit integers
• Simple Method: XY = (2n/2
A+B)(2n/2
C+D)
• Running Time Recurrence
T(n) < 4T(n/2) + 100n
How do we solve it?
L2.4
Substitution method
1. Guess the form of the solution.
2. Verify by induction.
3. Solve for constants.
The most general method:
Example: T(n) = 4T(n/2) + 100n
• [Assume that T(1) = Θ(1).]
• Guess O(n3
) . (Prove O and Ω separately.)
• Assume that T(k) ≤ ck3
for k < n .
• Prove T(n) ≤ cn3
by induction.
L2.5
Example of substitution
desired – residual
whenever (c/2)n3
– 100n ≥ 0, for
example, if c ≥ 200 and n ≥ 1.
desired
residual
3
33
3
3
))2/((
)2/(
)2/(4
)2/(4)(
cn
100nnccn
100nnc
100nnc
100nnTnT
≤
−−=
+=
+≤
+=
L2.6
Example (continued)
• We must also handle the initial conditions,
that is, ground the induction with base
cases.
• Base: T(n) = Θ(1) for all n < n0, where n0
is a suitable constant.
• For 1 ≤ n < n0, we have “Θ(1)” ≤ cn3
, if we
pick c big enough.
This bound is not tight!
L2.7
A tighter upper bound?
We shall prove that T(n) = O(n2
).
Assume that T(k) ≤ ck2
for k < n:
for no choice of c > 0. Lose!
)2/(4)(
2 100ncn
100nnTnT
+≤
+=
2
cn≤
L2.8
A tighter upper bound!
IDEA: Strengthen the inductive hypothesis.
• Subtract a low-order term.
Inductive hypothesis: T(k) ≤ c1k2
– c2k for k < n.
if c2 > 100.
Pick c1 big enough to handle the initial conditions.
)(
2
))2/()2/((4
)2/(4)(
2
2
1
22
2
1
2
2
1
2
2
1
ncnc
100nncncnc
100nncnc
100nncnc
100nnTnT
−≤
−−−=
+−=
+−≤
+=
L2.9
Recursion-tree method
• A recursion tree models the costs (time) of a
recursive execution of an algorithm.
• The recursion tree method is good for
generating guesses for the substitution
method.
• The recursion-tree method can be unreliable,
just like any method that uses ellipses (…).
• The recursion-tree method promotes intuition,
however.
L2.10
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2
:
L2.11
Example of recursion tree
T(n)
Solve T(n) = T(n/4) + T(n/2) + n2
:
L2.12
Example of recursion tree
T(n/4) T(n/2)
n2
Solve T(n) = T(n/4) + T(n/2) + n2
:
L2.13
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2
:
n2
(n/4)2 (n/2)2
T(n/16) T(n/8) T(n/8) T(n/4)
L2.14
Example of recursion tree
(n/16)2
(n/8)2
(n/8)2
(n/4)2
(n/4)2 (n/2)2
Θ(1)
…
Solve T(n) = T(n/4) + T(n/2) + n2
:
n2
L2.15
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2
:
(n/16)2
(n/8)2
(n/8)2
(n/4)2
(n/4)2 (n/2)2
Θ(1)
…
2nn2
L2.16
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2
:
(n/16)2
(n/8)2
(n/8)2
(n/4)2
(n/4)2 (n/2)2
Θ(1)
…
2
16
5 n
2nn2
L2.17
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2
:
(n/16)2
(n/8)2
(n/8)2
(n/4)2
(n/4)2
Θ(1)
…
2
16
5 n
2n
2
256
25 n
n2
(n/2)2
…
L2.18
Example of recursion tree
Solve T(n) = T(n/4) + T(n/2) + n2
:
(n/16)2
(n/8)2
(n/8)2
(n/4)2
(n/4)2
Θ(1)
…
2
16
5 n
2n
2
256
25 n
( ) ( )( )1
3
16
52
16
5
16
52
++++n
…
Total =
= Θ(n2
)
n2
(n/2)2
geometric series
L2.19
Appendix: geometric series
1
1
1 2
x
xx
−
=+++  for |x| < 1
1
1
1
1
2
x
x
xxx
n
n
−
−
=++++
+
 for x ≠ 1
Return to last
slide viewed.
L2.20
The master method
The master method applies to recurrences of
the form
T(n) = aT(n/b) + f(n) ,
where a ≥ 1, b > 1, and f is asymptotically
positive.
L2.21
f(n/b)
Idea of master theorem
f(n/b) f(n/b)
Τ(1)
…
Recursion tree:
…
f(n) a
f(n/b2
)f(n/b2
) f(n/b2
)…
ah = logbn
f(n)
af(n/b)
a2
f(n/b2
)
…
#leaves = ah
= alogbn
= nlogba
nlogba
Τ(1)
L2.22
Three common cases
Compare f(n) with nlogba
:
1. f(n) = O(nlogba – ε
) for some constant ε > 0.
• f(n) grows polynomially slower than nlogba
(by an nε
factor).
Solution: T(n) = Θ(nlogba
) .
L2.23
f(n/b)
Idea of master theorem
f(n/b) f(n/b)
Τ(1)
…
Recursion tree:
…
f(n) a
f(n/b2
)f(n/b2
) f(n/b2
)…
ah = logbn
f(n)
af(n/b)
a2
f(n/b2
)
…
nlogba
Τ(1)
CASE 1: The weight increases
geometrically from the root to the
leaves. The leaves hold a constant
fraction of the total weight.
CASE 1: The weight increases
geometrically from the root to the
leaves. The leaves hold a constant
fraction of the total weight. Θ(nlogba
)
L2.24
Three common cases
Compare f(n) with nlogba
:
2. f(n) = Θ(nlogba
lgk
n) for some constant k ≥ 0.
• f(n) and nlogba
grow at similar rates.
Solution: T(n) = Θ(nlogba
lgk+1
n) .
L2.25
f(n/b)
Idea of master theorem
f(n/b) f(n/b)
Τ(1)
…
Recursion tree:
…
f(n) a
f(n/b2
)f(n/b2
) f(n/b2
)…
ah = logbn
f(n)
af(n/b)
a2
f(n/b2
)
…
nlogba
Τ(1)CASE 2: (k = 0) The weight
is approximately the same on
each of the logbn levels.
CASE 2: (k = 0) The weight
is approximately the same on
each of the logbn levels.
Θ(nlogba
lgn)
L2.26
Three common cases (cont.)
Compare f(n) with nlogba
:
3. f(n) = Ω(nlogba + ε
) for some constant ε > 0.
• f(n) grows polynomially faster than nlogba
(by
an nε
factor),
and f(n) satisfies the regularity condition that
af(n/b) ≤ c f(n) for some constant c < 1.
Solution: T(n) = Θ( f(n)) .
L2.27
f(n/b)
Idea of master theorem
f(n/b) f(n/b)
Τ(1)
…
Recursion tree:
…
f(n) a
f(n/b2
)f(n/b2
) f(n/b2
)…
ah = logbn
f(n)
af(n/b)
a2
f(n/b2
)
…
nlogba
Τ(1)
CASE 3: The weight decreases
geometrically from the root to the
leaves. The root holds a constant
fraction of the total weight.
CASE 3: The weight decreases
geometrically from the root to the
leaves. The root holds a constant
fraction of the total weight. Θ(f(n))
L2.28
Examples
Ex. T(n) = 4T(n/2) + n
a = 4, b = 2 ⇒ nlogba
= n2
; f(n) = n.
CASE 1: f(n) = O(n2 – ε
) for ε = 1.
∴ T(n) = Θ(n2
).
Ex. T(n) = 4T(n/2) + n2
a = 4, b = 2 ⇒ nlogba
= n2
; f(n) = n2
.
CASE 2: f(n) = Θ(n2
lg0
n), that is, k = 0.
∴ T(n) = Θ(n2
lgn).
L2.29
Examples
Ex. T(n) = 4T(n/2) + n3
a = 4, b = 2 ⇒ nlogba
= n2
; f(n) = n3
.
CASE 3: f(n) = Ω(n2 + ε
) for ε = 1
and 4(cn/2)3
≤ cn3
(reg. cond.) for c = 1/2.
∴ T(n) = Θ(n3
).
Ex. T(n) = 4T(n/2) + n2
/lgn
a = 4, b = 2 ⇒ nlogba
= n2
; f(n) = n2
/lgn.
Master method does not apply. In particular,
for every constant ε > 0, we have nε
= ω(lgn).
L2.30
Conclusion
• Next time: applying the master method.
• For proof of master theorem, goto section

More Related Content

What's hot

Proof master theorem
Proof master theoremProof master theorem
Proof master theoremRajendran
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5Kumar
 
Recurrences
RecurrencesRecurrences
RecurrencesDEVTYPE
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesMegha V
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquerVikas Sharma
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesjayavignesh86
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
recurence solutions
recurence solutionsrecurence solutions
recurence solutionssoumya8396
 
Solving linear homogeneous recurrence relations
Solving linear homogeneous recurrence relationsSolving linear homogeneous recurrence relations
Solving linear homogeneous recurrence relationsDr. Maamoun Ahmed
 

What's hot (18)

Proof master theorem
Proof master theoremProof master theorem
Proof master theorem
 
Recurrence relationclass 5
Recurrence relationclass 5Recurrence relationclass 5
Recurrence relationclass 5
 
Recurrence relation
Recurrence relationRecurrence relation
Recurrence relation
 
Time complexity
Time complexityTime complexity
Time complexity
 
Recurrences
RecurrencesRecurrences
Recurrences
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
L2
L2L2
L2
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrencesLecture 5 6_7 - divide and conquer and method of solving recurrences
Lecture 5 6_7 - divide and conquer and method of solving recurrences
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
recurence solutions
recurence solutionsrecurence solutions
recurence solutions
 
Solving linear homogeneous recurrence relations
Solving linear homogeneous recurrence relationsSolving linear homogeneous recurrence relations
Solving linear homogeneous recurrence relations
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Stirling theorem
Stirling theoremStirling theorem
Stirling theorem
 
Master method
Master methodMaster method
Master method
 

Viewers also liked

Networking Chapter 16
Networking Chapter 16Networking Chapter 16
Networking Chapter 16mlrbrown
 
Computer Security Test
Computer Security TestComputer Security Test
Computer Security Testkhant14
 
Networking Chapter 15
Networking Chapter 15Networking Chapter 15
Networking Chapter 15mlrbrown
 
Networking Chapter 7
Networking Chapter 7Networking Chapter 7
Networking Chapter 7mlrbrown
 
Networking Chapter 8
Networking Chapter 8Networking Chapter 8
Networking Chapter 8mlrbrown
 
Networking Chapter 11
Networking Chapter 11Networking Chapter 11
Networking Chapter 11mlrbrown
 
Cso gaddis java_chapter15
Cso gaddis java_chapter15Cso gaddis java_chapter15
Cso gaddis java_chapter15mlrbrown
 

Viewers also liked (7)

Networking Chapter 16
Networking Chapter 16Networking Chapter 16
Networking Chapter 16
 
Computer Security Test
Computer Security TestComputer Security Test
Computer Security Test
 
Networking Chapter 15
Networking Chapter 15Networking Chapter 15
Networking Chapter 15
 
Networking Chapter 7
Networking Chapter 7Networking Chapter 7
Networking Chapter 7
 
Networking Chapter 8
Networking Chapter 8Networking Chapter 8
Networking Chapter 8
 
Networking Chapter 11
Networking Chapter 11Networking Chapter 11
Networking Chapter 11
 
Cso gaddis java_chapter15
Cso gaddis java_chapter15Cso gaddis java_chapter15
Cso gaddis java_chapter15
 

Similar to L2

T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutionssubhashchandra197
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Traian Rebedea
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerJoepang2015
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probabilitybryan111472
 
Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715HelpWithAssignment.com
 
Hierarchical matrices for approximating large covariance matries and computin...
Hierarchical matrices for approximating large covariance matries and computin...Hierarchical matrices for approximating large covariance matries and computin...
Hierarchical matrices for approximating large covariance matries and computin...Alexander Litvinenko
 
Conjugate Gradient Methods
Conjugate Gradient MethodsConjugate Gradient Methods
Conjugate Gradient MethodsMTiti1
 
lecture 4
lecture 4lecture 4
lecture 4sajinsc
 
Week 8 [compatibility mode]
Week 8 [compatibility mode]Week 8 [compatibility mode]
Week 8 [compatibility mode]Hazrul156
 
Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...Alexander Litvinenko
 
Crib Sheet AP Calculus AB and BC exams
Crib Sheet AP Calculus AB and BC examsCrib Sheet AP Calculus AB and BC exams
Crib Sheet AP Calculus AB and BC examsA Jorge Garcia
 

Similar to L2 (20)

T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3Algorithm Design and Complexity - Course 3
Algorithm Design and Complexity - Course 3
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
 
CS330-Lectures Statistics And Probability
CS330-Lectures Statistics And ProbabilityCS330-Lectures Statistics And Probability
CS330-Lectures Statistics And Probability
 
2.pptx
2.pptx2.pptx
2.pptx
 
Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715
 
3.pdf
3.pdf3.pdf
3.pdf
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 3
Unit 3Unit 3
Unit 3
 
M220w07
M220w07M220w07
M220w07
 
Hierarchical matrices for approximating large covariance matries and computin...
Hierarchical matrices for approximating large covariance matries and computin...Hierarchical matrices for approximating large covariance matries and computin...
Hierarchical matrices for approximating large covariance matries and computin...
 
numerical.ppt
numerical.pptnumerical.ppt
numerical.ppt
 
03 dc
03 dc03 dc
03 dc
 
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
 
Conjugate Gradient Methods
Conjugate Gradient MethodsConjugate Gradient Methods
Conjugate Gradient Methods
 
lecture 4
lecture 4lecture 4
lecture 4
 
Week 8 [compatibility mode]
Week 8 [compatibility mode]Week 8 [compatibility mode]
Week 8 [compatibility mode]
 
Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...
 
Crib Sheet AP Calculus AB and BC exams
Crib Sheet AP Calculus AB and BC examsCrib Sheet AP Calculus AB and BC exams
Crib Sheet AP Calculus AB and BC exams
 

More from fika sweety

Query optimization and performance
Query optimization and performanceQuery optimization and performance
Query optimization and performancefika sweety
 
Program design techniques
Program design techniquesProgram design techniques
Program design techniquesfika sweety
 
Modeling and simulation ch 1
Modeling and simulation ch 1Modeling and simulation ch 1
Modeling and simulation ch 1fika sweety
 
Macros...presentation
Macros...presentationMacros...presentation
Macros...presentationfika sweety
 
Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchartfika sweety
 
Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02fika sweety
 
Coal presentationt
Coal presentationtCoal presentationt
Coal presentationtfika sweety
 
1 Computer Architecture
1 Computer Architecture1 Computer Architecture
1 Computer Architecturefika sweety
 
Warehouse chapter3
Warehouse chapter3   Warehouse chapter3
Warehouse chapter3 fika sweety
 
Query optimization and performance
Query optimization and performanceQuery optimization and performance
Query optimization and performancefika sweety
 
Database security copy
Database security   copyDatabase security   copy
Database security copyfika sweety
 

More from fika sweety (20)

Query optimization and performance
Query optimization and performanceQuery optimization and performance
Query optimization and performance
 
Program design techniques
Program design techniquesProgram design techniques
Program design techniques
 
Plsql
PlsqlPlsql
Plsql
 
Shift rotate
Shift rotateShift rotate
Shift rotate
 
Graphss
GraphssGraphss
Graphss
 
Modeling and simulation ch 1
Modeling and simulation ch 1Modeling and simulation ch 1
Modeling and simulation ch 1
 
Macros...presentation
Macros...presentationMacros...presentation
Macros...presentation
 
Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchart
 
Diversity (HRM)
Diversity (HRM)Diversity (HRM)
Diversity (HRM)
 
Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02
 
Coal presentationt
Coal presentationtCoal presentationt
Coal presentationt
 
1 Computer Architecture
1 Computer Architecture1 Computer Architecture
1 Computer Architecture
 
3 Pipelining
3 Pipelining3 Pipelining
3 Pipelining
 
19 primkruskal
19 primkruskal19 primkruskal
19 primkruskal
 
Warehouse chapter3
Warehouse chapter3   Warehouse chapter3
Warehouse chapter3
 
Storage memory
Storage memoryStorage memory
Storage memory
 
Quick sort
Quick sortQuick sort
Quick sort
 
Query optimization and performance
Query optimization and performanceQuery optimization and performance
Query optimization and performance
 
Database security copy
Database security   copyDatabase security   copy
Database security copy
 
Programming
ProgrammingProgramming
Programming
 

Recently uploaded

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

L2

  • 1. Introduction to Algorithms 6.046J Lecture 2 Prof. Shafi Goldwasser
  • 2. L2.2 Solving recurrences • The analysis of integer multiplication from Lecture 1 required us to solve a recurrence. • Recurrences are a major tool for analysis of algorithms • Today: Learn a few methods. • Lecture 3: Divide and Conquer algorithms which are analyzable by recurrences.
  • 3. L2.3 Recall: Integer Multiplication • Let X = A B and Y = C D where A,B,C and D are n/2 bit integers • Simple Method: XY = (2n/2 A+B)(2n/2 C+D) • Running Time Recurrence T(n) < 4T(n/2) + 100n How do we solve it?
  • 4. L2.4 Substitution method 1. Guess the form of the solution. 2. Verify by induction. 3. Solve for constants. The most general method: Example: T(n) = 4T(n/2) + 100n • [Assume that T(1) = Θ(1).] • Guess O(n3 ) . (Prove O and Ω separately.) • Assume that T(k) ≤ ck3 for k < n . • Prove T(n) ≤ cn3 by induction.
  • 5. L2.5 Example of substitution desired – residual whenever (c/2)n3 – 100n ≥ 0, for example, if c ≥ 200 and n ≥ 1. desired residual 3 33 3 3 ))2/(( )2/( )2/(4 )2/(4)( cn 100nnccn 100nnc 100nnc 100nnTnT ≤ −−= += +≤ +=
  • 6. L2.6 Example (continued) • We must also handle the initial conditions, that is, ground the induction with base cases. • Base: T(n) = Θ(1) for all n < n0, where n0 is a suitable constant. • For 1 ≤ n < n0, we have “Θ(1)” ≤ cn3 , if we pick c big enough. This bound is not tight!
  • 7. L2.7 A tighter upper bound? We shall prove that T(n) = O(n2 ). Assume that T(k) ≤ ck2 for k < n: for no choice of c > 0. Lose! )2/(4)( 2 100ncn 100nnTnT +≤ += 2 cn≤
  • 8. L2.8 A tighter upper bound! IDEA: Strengthen the inductive hypothesis. • Subtract a low-order term. Inductive hypothesis: T(k) ≤ c1k2 – c2k for k < n. if c2 > 100. Pick c1 big enough to handle the initial conditions. )( 2 ))2/()2/((4 )2/(4)( 2 2 1 22 2 1 2 2 1 2 2 1 ncnc 100nncncnc 100nncnc 100nncnc 100nnTnT −≤ −−−= +−= +−≤ +=
  • 9. L2.9 Recursion-tree method • A recursion tree models the costs (time) of a recursive execution of an algorithm. • The recursion tree method is good for generating guesses for the substitution method. • The recursion-tree method can be unreliable, just like any method that uses ellipses (…). • The recursion-tree method promotes intuition, however.
  • 10. L2.10 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2 :
  • 11. L2.11 Example of recursion tree T(n) Solve T(n) = T(n/4) + T(n/2) + n2 :
  • 12. L2.12 Example of recursion tree T(n/4) T(n/2) n2 Solve T(n) = T(n/4) + T(n/2) + n2 :
  • 13. L2.13 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2 : n2 (n/4)2 (n/2)2 T(n/16) T(n/8) T(n/8) T(n/4)
  • 14. L2.14 Example of recursion tree (n/16)2 (n/8)2 (n/8)2 (n/4)2 (n/4)2 (n/2)2 Θ(1) … Solve T(n) = T(n/4) + T(n/2) + n2 : n2
  • 15. L2.15 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2 : (n/16)2 (n/8)2 (n/8)2 (n/4)2 (n/4)2 (n/2)2 Θ(1) … 2nn2
  • 16. L2.16 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2 : (n/16)2 (n/8)2 (n/8)2 (n/4)2 (n/4)2 (n/2)2 Θ(1) … 2 16 5 n 2nn2
  • 17. L2.17 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2 : (n/16)2 (n/8)2 (n/8)2 (n/4)2 (n/4)2 Θ(1) … 2 16 5 n 2n 2 256 25 n n2 (n/2)2 …
  • 18. L2.18 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2 : (n/16)2 (n/8)2 (n/8)2 (n/4)2 (n/4)2 Θ(1) … 2 16 5 n 2n 2 256 25 n ( ) ( )( )1 3 16 52 16 5 16 52 ++++n … Total = = Θ(n2 ) n2 (n/2)2 geometric series
  • 19. L2.19 Appendix: geometric series 1 1 1 2 x xx − =+++  for |x| < 1 1 1 1 1 2 x x xxx n n − − =++++ +  for x ≠ 1 Return to last slide viewed.
  • 20. L2.20 The master method The master method applies to recurrences of the form T(n) = aT(n/b) + f(n) , where a ≥ 1, b > 1, and f is asymptotically positive.
  • 21. L2.21 f(n/b) Idea of master theorem f(n/b) f(n/b) Τ(1) … Recursion tree: … f(n) a f(n/b2 )f(n/b2 ) f(n/b2 )… ah = logbn f(n) af(n/b) a2 f(n/b2 ) … #leaves = ah = alogbn = nlogba nlogba Τ(1)
  • 22. L2.22 Three common cases Compare f(n) with nlogba : 1. f(n) = O(nlogba – ε ) for some constant ε > 0. • f(n) grows polynomially slower than nlogba (by an nε factor). Solution: T(n) = Θ(nlogba ) .
  • 23. L2.23 f(n/b) Idea of master theorem f(n/b) f(n/b) Τ(1) … Recursion tree: … f(n) a f(n/b2 )f(n/b2 ) f(n/b2 )… ah = logbn f(n) af(n/b) a2 f(n/b2 ) … nlogba Τ(1) CASE 1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. CASE 1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. Θ(nlogba )
  • 24. L2.24 Three common cases Compare f(n) with nlogba : 2. f(n) = Θ(nlogba lgk n) for some constant k ≥ 0. • f(n) and nlogba grow at similar rates. Solution: T(n) = Θ(nlogba lgk+1 n) .
  • 25. L2.25 f(n/b) Idea of master theorem f(n/b) f(n/b) Τ(1) … Recursion tree: … f(n) a f(n/b2 )f(n/b2 ) f(n/b2 )… ah = logbn f(n) af(n/b) a2 f(n/b2 ) … nlogba Τ(1)CASE 2: (k = 0) The weight is approximately the same on each of the logbn levels. CASE 2: (k = 0) The weight is approximately the same on each of the logbn levels. Θ(nlogba lgn)
  • 26. L2.26 Three common cases (cont.) Compare f(n) with nlogba : 3. f(n) = Ω(nlogba + ε ) for some constant ε > 0. • f(n) grows polynomially faster than nlogba (by an nε factor), and f(n) satisfies the regularity condition that af(n/b) ≤ c f(n) for some constant c < 1. Solution: T(n) = Θ( f(n)) .
  • 27. L2.27 f(n/b) Idea of master theorem f(n/b) f(n/b) Τ(1) … Recursion tree: … f(n) a f(n/b2 )f(n/b2 ) f(n/b2 )… ah = logbn f(n) af(n/b) a2 f(n/b2 ) … nlogba Τ(1) CASE 3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. CASE 3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. Θ(f(n))
  • 28. L2.28 Examples Ex. T(n) = 4T(n/2) + n a = 4, b = 2 ⇒ nlogba = n2 ; f(n) = n. CASE 1: f(n) = O(n2 – ε ) for ε = 1. ∴ T(n) = Θ(n2 ). Ex. T(n) = 4T(n/2) + n2 a = 4, b = 2 ⇒ nlogba = n2 ; f(n) = n2 . CASE 2: f(n) = Θ(n2 lg0 n), that is, k = 0. ∴ T(n) = Θ(n2 lgn).
  • 29. L2.29 Examples Ex. T(n) = 4T(n/2) + n3 a = 4, b = 2 ⇒ nlogba = n2 ; f(n) = n3 . CASE 3: f(n) = Ω(n2 + ε ) for ε = 1 and 4(cn/2)3 ≤ cn3 (reg. cond.) for c = 1/2. ∴ T(n) = Θ(n3 ). Ex. T(n) = 4T(n/2) + n2 /lgn a = 4, b = 2 ⇒ nlogba = n2 ; f(n) = n2 /lgn. Master method does not apply. In particular, for every constant ε > 0, we have nε = ω(lgn).
  • 30. L2.30 Conclusion • Next time: applying the master method. • For proof of master theorem, goto section