SlideShare a Scribd company logo
1 of 57
Download to read offline
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.1 
Introduction to Algorithms 
6.046J/18.401J 
LECTURE2Asymptotic Notation•O-, Ω-, and Θ-notationRecurrences•Substitution method•Iterating the recurrence•Recursion tree•Master method 
Prof. Erik Demaine
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.2 
Asymptotic notation 
O-notation (upper bounds): We write f(n) = O(g(n))if there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n)for all n≥n0. We write f(n) = O(g(n))if there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n)for all n≥
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.3 
Asymptotic notation 
O-notation (upper bounds): We write f(n) = O(g(n))if there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n)for all n≥n0. 
We write f(n) = O(g(n))if there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n)for all n≥EXAMPLE:2n2= O(n3) 
(c= 1, n0= 2)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.4 
Asymptotic notation 
O-notation (upper bounds): We write f(n) = O(g(n))if there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n)for all n≥n0. 
We write f(n) = O(g(n))if there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n)for all n≥EXAMPLE:2n2= O(n3) 
(c= 1, n0= 2) functions, not values
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.5 
Asymptotic notation 
O-notation (upper bounds): We write f(n) = O(g(n))if there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n)for all n≥n0. We write f(n) = O(g(n))if there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n)for all n≥EXAMPLE:2n2= O(n3)(c= 1, n0= 2) 
functions, not values 
funny, “one-way” equality
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.6 
Set definition of O-notation 
O(g(n))= { f(n) :there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n) for all n≥n0} O(g(n))= { f(n) :there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n) for all n≥
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.7 
Set definition of O-notation 
O(g(n))= { f(n) :there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n) for all n≥n0} 
O(g(n))= { f(n) :there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n) for all n≥EXAMPLE:2n2∈O(n3)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.8 
Set definition of O-notation 
O(g(n))= { f(n) :there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n) for all n≥n0} 
O(g(n))= { f(n) :there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n) for all n≥EXAMPLE:2n2∈O(n3) 
(Logicians:λn.2n2∈O(λn.n3), but it’s convenient to be sloppy, as long as we understand what’s reallygoing on.)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.9 
Macro substitution 
Convention:A set in a formula represents an anonymous function in the set.
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.10 
Macro substitution 
Convention:A set in a formula represents an anonymous function in the set. 
f(n) = n3+ O(n2) 
means 
f(n) = n3+ h(n) 
for some h(n) ∈O(n2). 
EXAMPLE:
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.11 
Macro substitution 
Convention:A set in a formula represents an anonymous function in the set. 
n2+ O(n) = O(n2) 
means 
for any f(n) ∈O(n): 
n2+ f(n) = h(n) 
for some h(n) ∈O(n2). 
EXAMPLE:
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.12 
Ω-notation (lower bounds) 
O-notation is an upper-boundnotation. It makes no sense to say f(n)is at least O(n2).
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.13 
Ω-notation (lower bounds) 
O-notation is an upper-boundnotation. It makes no sense to say f(n)is at least O(n2). 
Ω(g(n))= { f(n) :there exist constants c> 0, n0> 0such that 0 ≤cg(n) ≤f(n) for all n≥n0} Ω(g(n))= { f(n) :there exist constants c> 0, 0such that 0 ≤cg(n) ≤f(n) for all n≥
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.14 
Ω-notation (lower bounds) 
O-notation is an upper-boundnotation. It makes no sense to say f(n)is at least O(n2). 
Ω(g(n))= { f(n) :there exist constants c> 0, n0> 0such that 0 ≤cg(n) ≤f(n) for all n≥n0} 
Ω(g(n))= { f(n) :there exist constants c> 0, 0such that 0 ≤cg(n) ≤f(n) for all n≥EXAMPLE: 
(c= 1, n0= 16))(lgnnΩ=
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.15 
Θ-notation (tight bounds) 
Θ(g(n)) = O(g(n)) ∩Ω(g(n))Θ(g(n)) = O(g(n)) ∩Ω(g(n))
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.16 
Θ-notation (tight bounds) 
Θ(g(n)) = O(g(n)) ∩Ω(g(n))Θ(g(n)) = O(g(n)) ∩Ω(g(n)) )(22221nnnΘ=− 
EXAMPLE:
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.17 
ο-notation and ω-notation 
O-notation and Ω-notation are like ≤and ≥. 
o-notation and ω-notation are like < and >. 
ο(g(n))= { f(n) :for any constant c> 0, there is a constant n0> 0such that 0 ≤f(n) <cg(n) for all n≥n0} 
ο(g(n))= { f(n) :for any constant c> 0, there is a constant 0such that 0 ≤f(n) <cg(n) for all n≥EXAMPLE: 
(n0= 2/c) 
2n2= o(n3)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.18 
ο-notation and ω-notation 
O-notation and Ω-notation are like ≤and ≥. 
o-notation and ω-notation are like < and >. 
ω(g(n))= { f(n) :for any constant c> 0, there is a constant n0> 0such that 0 ≤cg(n)<f(n) for all n≥n0} 
ω(g(n))= { f(n) :for any constant c> 0, there is a constant 0such that 0 ≤cg(n)<f(n) for all n≥EXAMPLE: 
(n0= 1+1/c))(lgnnω=
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.19 
Solving recurrences 
•The analysis of merge sort from Lecture 1required us to solve a recurrence. 
•Recurrences are like solving integrals, differential equations, etc. 
oLearn a few tricks. 
•Lecture 3: Applications of recurrences to divide-and-conquer algorithms.
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.20 
Substitution method 
1.Guessthe form of the solution. 
2.Verifyby induction. 
3.Solvefor constants. 
The most general method:
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.21 
Substitution method 
1.Guessthe form of the solution. 
2.Verifyby induction. 
3.Solvefor constants. 
The most general method: 
EXAMPLE:T(n) = 4T(n/2) + n•[Assume that T(1) = Θ(1).] •Guess O(n3). (Prove Oand Ωseparately.) •Assume that T(k) ≤ck3for k< n . •Prove T(n) ≤cn3by induction.
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.22 
Example of substitution 
33333))2/(( )2/( )2/(4)2/(4)( cnnnccnnncnncnnTnT≤ −−= += +≤ += desired–residualwhenever (c/2)n3–n≥0, for example, if c ≥2and n ≥1. desiredresidual
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.23 
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 n0is a suitable constant. 
•For 1 ≤n< n0, we have “Θ(1)”≤cn3, if we pick cbig enough.
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.24 
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 n0is a suitable constant. 
•For 1 ≤n< n0, we have “Θ(1)”≤cn3, if we pick cbig enough. 
This bound is not tight!
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.25 
A tighter upper bound? 
We shall prove that T(n) = O(n2).
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.26 
A tighter upper bound? 
We shall prove that T(n) = O(n2). 
Assume that T(k) ≤ck2for k< n: 
)( )2/(4)2/(4)( 222nOncnnncnnTnT= += +≤ +=
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.27 
A tighter upper bound? 
We shall prove that T(n) = O(n2). 
Assume that T(k) ≤ck2for k< n: 
)( )2/(4)2/(4)( 222nOncnnncnnTnT= += +≤ += Wrong!We must prove the I.H.
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.28 
A tighter upper bound? 
We shall prove that T(n) = O(n2). 
Assume that T(k) ≤ck2for k< n: 
)( )2/(4)2/(4)( 222nOncnnncnnTnT= += +≤ += Wrong!We must prove the I.H. 22)( cnncn≤ −−= for nochoice of c> 0. Lose! [ desired–residual]
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.29 
A tighter upper bound! 
IDEA:Strengthen the inductive hypothesis. 
•Subtracta low-order term. 
Inductive hypothesis: T(k) ≤c1k2–c2kfor k< n.
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.30 
A tighter upper bound! 
IDEA:Strengthen the inductive hypothesis. 
•Subtracta low-order term. 
Inductive hypothesis: T(k) ≤c1k2–c2kfor k< n. 
T(n)= 4T(n/2) + n 
= 4(c1(n/2)2–c2(n/2))+ n 
= c1n2–2c2n+ n 
= c1n2–c2n–(c2n–n) 
≤c1n2–c2nifc2≥1.
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.31 
A tighter upper bound! 
IDEA:Strengthen the inductive hypothesis. 
•Subtracta low-order term. 
Inductive hypothesis: T(k) ≤c1k2–c2kfor k< n. 
T(n)= 4T(n/2) + n 
= 4(c1(n/2)2–c2(n/2))+ n 
= c1n2–2c2n+ n 
= c1n2–c2n–(c2n–n) 
≤c1n2–c2nifc2≥1. 
Pick c1big enough to handle the initial conditions.
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.32 
Recursion-tree method 
•A recursion tree models the costs (time) of a recursive execution of an algorithm. 
•The recursion-tree method can be unreliable, just like any method that uses ellipses (…). 
•The recursion-tree method promotes intuition, however. 
•The recursion tree method is good for generating guesses for the substitution method.
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.33 
Example of recursion tree 
Solve T(n) = T(n/4) + T(n/2)+ n2:
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.34 
Example of recursion tree 
Solve T(n) = T(n/4) + T(n/2)+ n2: 
T(n)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.35 
Example of recursion tree 
Solve T(n) = T(n/4) + T(n/2)+ n2: T(n/4)T(n/2) n2
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.36 
Example of recursion tree 
Solve T(n) = T(n/4) + T(n/2)+ n2: n2(n/4)2(n/2)2T(n/16)T(n/8)T(n/8)T(n/4)
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.37 
Example of recursion tree 
Solve T(n) = T(n/4) + T(n/2)+ n2: n2(n/16)2(n/8)2(n/8)2(n/4)2(n/4)2(n/2)2Θ(1) …
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.38 
Example of recursion tree 
Solve T(n) = T(n/4) + T(n/2)+ n2: n22n(n/16)2(n/8)2(n/8)2(n/4)2(n/4)2(n/2)2Θ(1) …
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.39 
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) … 2165n2nn2
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.40 
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) … 2165n2n225625nn2(n/2)2 
…
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.41 
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) … 2165n2n225625n()()() 1316521651652L++++n= Θ(n2) 
… 
Total = n2(n/2)2geometric series
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.42 
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 fis asymptotically positive.
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.43 
Three common cases 
Compare f(n)with nlogba: 
1.f(n) = O(nlogba–ε)for some constant ε> 0. 
•f(n) grows polynomiallyslower than nlogba(by an nεfactor). 
Solution: T(n) = Θ(nlogba).
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.44 
Three common cases 
Compare f(n)with nlogba: 
1.f(n) = O(nlogba–ε)for some constant ε> 0. 
•f(n) grows polynomiallyslower than nlogba(by an nεfactor). 
Solution: T(n) = Θ(nlogba). 
2.f(n) = Θ(nlogbalgkn)for some constant k≥0. •f(n) and nlogbagrow at similar rates. Solution: T(n) = Θ(nlogbalgk+1n).
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.45 
Three common cases (cont.) 
Compare f(n)with nlogba: 
3.f(n) = Ω(nlogba+ ε)for some constant ε> 0. 
•f(n) grows polynomiallyfaster than nlogba(by an nεfactor), 
andf(n) satisfies the regularity conditionthat af(n/b) ≤cf(n) for some constant c< 1. 
Solution: T(n) = Θ(f(n)).
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.46 
Examples 
EX.T(n) = 4T(n/2) + na =4, b= 2 ⇒nlogba=n2; f(n) = n. CASE1: f(n) = O(n2–ε)for ε= 1. ∴T(n) = Θ(n2).
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.47 
Examples 
EX.T(n) = 4T(n/2) + na =4, b= 2 ⇒nlogba=n2; f(n) = n. CASE1: 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. 
CASE2: f(n) = Θ(n2lg0n), that is, k = 0. 
∴T(n) = Θ(n2lgn).
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.48 
Examples 
EX.T(n) = 4T(n/2) + n3a =4, b= 2 ⇒nlogba=n2; f(n) = n3. CASE3: f(n) = Ω(n2+ ε)for ε= 1and4(n/2)3≤cn3 (reg. cond.) for c= 1/2. ∴T(n) = Θ(n3).
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.49 
Examples 
EX.T(n) = 4T(n/2) + n3a =4, b= 2 ⇒nlogba=n2; f(n) = n3. CASE3: f(n) = Ω(n2+ ε)for ε= 1and4(n/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).
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.50 
f(n/b) 
Idea of master theorem 
Recursion tree: f(n/b)f(n/b) Τ(1) … … f(n)af(n/b2)f(n/b2)f(n/b2)… a
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.51 
f(n/b) 
Idea of master theorem 
Recursion tree: f(n/b)f(n/b) Τ(1) … … f(n)af(n/b2)f(n/b2)f(n/b2)… af(n) af(n/b) a2f(n/b2) …
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.52 
f(n/b) 
Idea of master theoremf(n/b)f(n/b) Τ(1) … Recursion tree: … f(n)af(n/b2)f(n/b2)f(n/b2)… ah= logbnf(n) af(n/b) a2f(n/b2) …
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.53 
nlogbaΤ(1) f(n/b) 
Idea of master theoremf(n/b)f(n/b) Τ(1) … Recursion tree: … f(n)af(n/b2)f(n/b2)f(n/b2)… ah= logbnf(n) af(n/b) a2f(n/b2) #leaves = ah= alogbn= nlogba …
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.54 
f(n/b) 
Idea of master theorem 
Recursion tree: f(n/b)f(n/b) Τ(1) … … f(n)af(n/b2)f(n/b2)f(n/b2)… ah= logbnf(n) af(n/b) a2f(n/b2) CASE1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. CASE1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. Θ(nlogba) nlogbaΤ(1) 
…
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.55 
f(n/b) 
Idea of master theorem 
Recursion tree: f(n/b)f(n/b) Τ(1) … … f(n)af(n/b2)f(n/b2)f(n/b2)… ah= logbnf(n) af(n/b) a2f(n/b2) CASE2: (k= 0) The weight is approximately the same on each of the logbnlevels. CASE2: (k= 0) The weight is approximately the same on each of the logbnlevels. Θ(nlogbalgn) nlogbaΤ(1) 
…
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.56 
f(n/b) 
Idea of master theorem 
Recursion tree: f(n/b)f(n/b) Τ(1) … … f(n)af(n/b2)f(n/b2)f(n/b2)… ah= logbnf(n) af(n/b) a2f(n/b2) 
… CASE3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. CASE3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. nlogbaΤ(1) Θ(f(n))
September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.57 
Appendix: geometric series 11112xxxxxnn− −=++++ + Lfor x≠1 1112xxx− =+++Lfor |x| < 1 
Return to last slide viewed.

More Related Content

What's hot

SMB_2012_HR_VAN_ST-last version
SMB_2012_HR_VAN_ST-last versionSMB_2012_HR_VAN_ST-last version
SMB_2012_HR_VAN_ST-last versionLilyana Vankova
 
Important Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringImportant Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringASPAK2014
 
P, NP and NP-Complete, Theory of NP-Completeness V2
P, NP and NP-Complete, Theory of NP-Completeness V2P, NP and NP-Complete, Theory of NP-Completeness V2
P, NP and NP-Complete, Theory of NP-Completeness V2S.Shayan Daneshvar
 
Fixed-Parameter Intractability
Fixed-Parameter IntractabilityFixed-Parameter Intractability
Fixed-Parameter IntractabilityASPAK2014
 
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVE
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVEChapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVE
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVEnszakir
 
Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)IJERD Editor
 
Imc2020 day1&amp;2 problems&amp;solutions
Imc2020 day1&amp;2 problems&amp;solutionsImc2020 day1&amp;2 problems&amp;solutions
Imc2020 day1&amp;2 problems&amp;solutionsChristos Loizos
 
Iterative Compression
Iterative CompressionIterative Compression
Iterative CompressionASPAK2014
 
free Video lecture
free Video lecture free Video lecture
free Video lecture Edhole.com
 
Chapter-4: More on Direct Proof and Proof by Contrapositive
Chapter-4: More on Direct Proof and Proof by ContrapositiveChapter-4: More on Direct Proof and Proof by Contrapositive
Chapter-4: More on Direct Proof and Proof by Contrapositivenszakir
 
Topic modeling with Poisson factorization (2)
Topic modeling with Poisson factorization (2)Topic modeling with Poisson factorization (2)
Topic modeling with Poisson factorization (2)Tomonari Masada
 
Paths and Polynomials
Paths and PolynomialsPaths and Polynomials
Paths and PolynomialsASPAK2014
 
Lesson 16 The Spectral Theorem and Applications
Lesson 16  The Spectral Theorem and ApplicationsLesson 16  The Spectral Theorem and Applications
Lesson 16 The Spectral Theorem and ApplicationsMatthew Leingang
 

What's hot (20)

SMB_2012_HR_VAN_ST-last version
SMB_2012_HR_VAN_ST-last versionSMB_2012_HR_VAN_ST-last version
SMB_2012_HR_VAN_ST-last version
 
Imc2016 day2-solutions
Imc2016 day2-solutionsImc2016 day2-solutions
Imc2016 day2-solutions
 
Important Cuts and (p,q)-clustering
Important Cuts and (p,q)-clusteringImportant Cuts and (p,q)-clustering
Important Cuts and (p,q)-clustering
 
P, NP and NP-Complete, Theory of NP-Completeness V2
P, NP and NP-Complete, Theory of NP-Completeness V2P, NP and NP-Complete, Theory of NP-Completeness V2
P, NP and NP-Complete, Theory of NP-Completeness V2
 
Ambiguty
AmbigutyAmbiguty
Ambiguty
 
Fixed-Parameter Intractability
Fixed-Parameter IntractabilityFixed-Parameter Intractability
Fixed-Parameter Intractability
 
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVE
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVEChapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVE
Chapter-3: DIRECT PROOF AND PROOF BY CONTRAPOSITIVE
 
Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)
 
Imc2020 day1&amp;2 problems&amp;solutions
Imc2020 day1&amp;2 problems&amp;solutionsImc2020 day1&amp;2 problems&amp;solutions
Imc2020 day1&amp;2 problems&amp;solutions
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Iterative Compression
Iterative CompressionIterative Compression
Iterative Compression
 
free Video lecture
free Video lecture free Video lecture
free Video lecture
 
Chapter-4: More on Direct Proof and Proof by Contrapositive
Chapter-4: More on Direct Proof and Proof by ContrapositiveChapter-4: More on Direct Proof and Proof by Contrapositive
Chapter-4: More on Direct Proof and Proof by Contrapositive
 
Imc2016 day1-solutions
Imc2016 day1-solutionsImc2016 day1-solutions
Imc2016 day1-solutions
 
Poisson factorization
Poisson factorizationPoisson factorization
Poisson factorization
 
Topic modeling with Poisson factorization (2)
Topic modeling with Poisson factorization (2)Topic modeling with Poisson factorization (2)
Topic modeling with Poisson factorization (2)
 
Time complexity
Time complexityTime complexity
Time complexity
 
Paths and Polynomials
Paths and PolynomialsPaths and Polynomials
Paths and Polynomials
 
LPS talk notes
LPS talk notesLPS talk notes
LPS talk notes
 
Lesson 16 The Spectral Theorem and Applications
Lesson 16  The Spectral Theorem and ApplicationsLesson 16  The Spectral Theorem and Applications
Lesson 16 The Spectral Theorem and Applications
 

Similar to mit 演算法 Lec2

6-Nfa & equivalence with RE.pdf
6-Nfa & equivalence with RE.pdf6-Nfa & equivalence with RE.pdf
6-Nfa & equivalence with RE.pdfshruti533256
 
Prime numbers boundary
Prime numbers boundary Prime numbers boundary
Prime numbers boundary Camilo Ulloa
 
Las funciones L en teoría de números
Las funciones L en teoría de númerosLas funciones L en teoría de números
Las funciones L en teoría de númerosmmasdeu
 
My PhD talk "Application of H-matrices for computing partial inverse"
My PhD talk "Application of H-matrices for computing partial inverse"My PhD talk "Application of H-matrices for computing partial inverse"
My PhD talk "Application of H-matrices for computing partial inverse"Alexander Litvinenko
 
A note on arithmetic progressions in sets of integers
A note on arithmetic progressions in sets of integersA note on arithmetic progressions in sets of integers
A note on arithmetic progressions in sets of integersLukas Nabergall
 
Improving estimates for discrete polynomial averaging operators
Improving estimates for discrete polynomial averaging operatorsImproving estimates for discrete polynomial averaging operators
Improving estimates for discrete polynomial averaging operatorsVjekoslavKovac1
 
Persamaan Differensial Biasa 2014
Persamaan Differensial Biasa 2014 Persamaan Differensial Biasa 2014
Persamaan Differensial Biasa 2014 Rani Sulvianuri
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of QuicksortRajendran
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms IiSri Prasanna
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
 
formulation of first order linear and nonlinear 2nd order differential equation
formulation of first order linear and nonlinear 2nd order differential equationformulation of first order linear and nonlinear 2nd order differential equation
formulation of first order linear and nonlinear 2nd order differential equationMahaswari Jogia
 
First order linear differential equation
First order linear differential equationFirst order linear differential equation
First order linear differential equationNofal Umair
 
CiE 2010 talk
CiE 2010 talkCiE 2010 talk
CiE 2010 talkilyaraz
 

Similar to mit 演算法 Lec2 (20)

Lec3 Algott
Lec3 AlgottLec3 Algott
Lec3 Algott
 
6-Nfa & equivalence with RE.pdf
6-Nfa & equivalence with RE.pdf6-Nfa & equivalence with RE.pdf
6-Nfa & equivalence with RE.pdf
 
Analysis of algo
Analysis of algoAnalysis of algo
Analysis of algo
 
L2
L2L2
L2
 
Prime numbers boundary
Prime numbers boundary Prime numbers boundary
Prime numbers boundary
 
Las funciones L en teoría de números
Las funciones L en teoría de númerosLas funciones L en teoría de números
Las funciones L en teoría de números
 
My PhD talk "Application of H-matrices for computing partial inverse"
My PhD talk "Application of H-matrices for computing partial inverse"My PhD talk "Application of H-matrices for computing partial inverse"
My PhD talk "Application of H-matrices for computing partial inverse"
 
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
 
A note on arithmetic progressions in sets of integers
A note on arithmetic progressions in sets of integersA note on arithmetic progressions in sets of integers
A note on arithmetic progressions in sets of integers
 
Improving estimates for discrete polynomial averaging operators
Improving estimates for discrete polynomial averaging operatorsImproving estimates for discrete polynomial averaging operators
Improving estimates for discrete polynomial averaging operators
 
NANO266 - Lecture 10 - Temperature
NANO266 - Lecture 10 - TemperatureNANO266 - Lecture 10 - Temperature
NANO266 - Lecture 10 - Temperature
 
Persamaan Differensial Biasa 2014
Persamaan Differensial Biasa 2014 Persamaan Differensial Biasa 2014
Persamaan Differensial Biasa 2014
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of Quicksort
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms Ii
 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
 
03 dc
03 dc03 dc
03 dc
 
formulation of first order linear and nonlinear 2nd order differential equation
formulation of first order linear and nonlinear 2nd order differential equationformulation of first order linear and nonlinear 2nd order differential equation
formulation of first order linear and nonlinear 2nd order differential equation
 
First order linear differential equation
First order linear differential equationFirst order linear differential equation
First order linear differential equation
 
CiE 2010 talk
CiE 2010 talkCiE 2010 talk
CiE 2010 talk
 

mit 演算法 Lec2

  • 1. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.1 Introduction to Algorithms 6.046J/18.401J LECTURE2Asymptotic Notation•O-, Ω-, and Θ-notationRecurrences•Substitution method•Iterating the recurrence•Recursion tree•Master method Prof. Erik Demaine
  • 2. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.2 Asymptotic notation O-notation (upper bounds): We write f(n) = O(g(n))if there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n)for all n≥n0. We write f(n) = O(g(n))if there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n)for all n≥
  • 3. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.3 Asymptotic notation O-notation (upper bounds): We write f(n) = O(g(n))if there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n)for all n≥n0. We write f(n) = O(g(n))if there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n)for all n≥EXAMPLE:2n2= O(n3) (c= 1, n0= 2)
  • 4. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.4 Asymptotic notation O-notation (upper bounds): We write f(n) = O(g(n))if there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n)for all n≥n0. We write f(n) = O(g(n))if there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n)for all n≥EXAMPLE:2n2= O(n3) (c= 1, n0= 2) functions, not values
  • 5. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.5 Asymptotic notation O-notation (upper bounds): We write f(n) = O(g(n))if there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n)for all n≥n0. We write f(n) = O(g(n))if there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n)for all n≥EXAMPLE:2n2= O(n3)(c= 1, n0= 2) functions, not values funny, “one-way” equality
  • 6. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.6 Set definition of O-notation O(g(n))= { f(n) :there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n) for all n≥n0} O(g(n))= { f(n) :there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n) for all n≥
  • 7. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.7 Set definition of O-notation O(g(n))= { f(n) :there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n) for all n≥n0} O(g(n))= { f(n) :there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n) for all n≥EXAMPLE:2n2∈O(n3)
  • 8. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.8 Set definition of O-notation O(g(n))= { f(n) :there exist constants c> 0, n0> 0such that 0 ≤f(n) ≤cg(n) for all n≥n0} O(g(n))= { f(n) :there exist constants c> 0, 0such that 0 ≤f(n) ≤cg(n) for all n≥EXAMPLE:2n2∈O(n3) (Logicians:λn.2n2∈O(λn.n3), but it’s convenient to be sloppy, as long as we understand what’s reallygoing on.)
  • 9. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.9 Macro substitution Convention:A set in a formula represents an anonymous function in the set.
  • 10. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.10 Macro substitution Convention:A set in a formula represents an anonymous function in the set. f(n) = n3+ O(n2) means f(n) = n3+ h(n) for some h(n) ∈O(n2). EXAMPLE:
  • 11. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.11 Macro substitution Convention:A set in a formula represents an anonymous function in the set. n2+ O(n) = O(n2) means for any f(n) ∈O(n): n2+ f(n) = h(n) for some h(n) ∈O(n2). EXAMPLE:
  • 12. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.12 Ω-notation (lower bounds) O-notation is an upper-boundnotation. It makes no sense to say f(n)is at least O(n2).
  • 13. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.13 Ω-notation (lower bounds) O-notation is an upper-boundnotation. It makes no sense to say f(n)is at least O(n2). Ω(g(n))= { f(n) :there exist constants c> 0, n0> 0such that 0 ≤cg(n) ≤f(n) for all n≥n0} Ω(g(n))= { f(n) :there exist constants c> 0, 0such that 0 ≤cg(n) ≤f(n) for all n≥
  • 14. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.14 Ω-notation (lower bounds) O-notation is an upper-boundnotation. It makes no sense to say f(n)is at least O(n2). Ω(g(n))= { f(n) :there exist constants c> 0, n0> 0such that 0 ≤cg(n) ≤f(n) for all n≥n0} Ω(g(n))= { f(n) :there exist constants c> 0, 0such that 0 ≤cg(n) ≤f(n) for all n≥EXAMPLE: (c= 1, n0= 16))(lgnnΩ=
  • 15. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.15 Θ-notation (tight bounds) Θ(g(n)) = O(g(n)) ∩Ω(g(n))Θ(g(n)) = O(g(n)) ∩Ω(g(n))
  • 16. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.16 Θ-notation (tight bounds) Θ(g(n)) = O(g(n)) ∩Ω(g(n))Θ(g(n)) = O(g(n)) ∩Ω(g(n)) )(22221nnnΘ=− EXAMPLE:
  • 17. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.17 ο-notation and ω-notation O-notation and Ω-notation are like ≤and ≥. o-notation and ω-notation are like < and >. ο(g(n))= { f(n) :for any constant c> 0, there is a constant n0> 0such that 0 ≤f(n) <cg(n) for all n≥n0} ο(g(n))= { f(n) :for any constant c> 0, there is a constant 0such that 0 ≤f(n) <cg(n) for all n≥EXAMPLE: (n0= 2/c) 2n2= o(n3)
  • 18. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.18 ο-notation and ω-notation O-notation and Ω-notation are like ≤and ≥. o-notation and ω-notation are like < and >. ω(g(n))= { f(n) :for any constant c> 0, there is a constant n0> 0such that 0 ≤cg(n)<f(n) for all n≥n0} ω(g(n))= { f(n) :for any constant c> 0, there is a constant 0such that 0 ≤cg(n)<f(n) for all n≥EXAMPLE: (n0= 1+1/c))(lgnnω=
  • 19. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.19 Solving recurrences •The analysis of merge sort from Lecture 1required us to solve a recurrence. •Recurrences are like solving integrals, differential equations, etc. oLearn a few tricks. •Lecture 3: Applications of recurrences to divide-and-conquer algorithms.
  • 20. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.20 Substitution method 1.Guessthe form of the solution. 2.Verifyby induction. 3.Solvefor constants. The most general method:
  • 21. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.21 Substitution method 1.Guessthe form of the solution. 2.Verifyby induction. 3.Solvefor constants. The most general method: EXAMPLE:T(n) = 4T(n/2) + n•[Assume that T(1) = Θ(1).] •Guess O(n3). (Prove Oand Ωseparately.) •Assume that T(k) ≤ck3for k< n . •Prove T(n) ≤cn3by induction.
  • 22. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.22 Example of substitution 33333))2/(( )2/( )2/(4)2/(4)( cnnnccnnncnncnnTnT≤ −−= += +≤ += desired–residualwhenever (c/2)n3–n≥0, for example, if c ≥2and n ≥1. desiredresidual
  • 23. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.23 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 n0is a suitable constant. •For 1 ≤n< n0, we have “Θ(1)”≤cn3, if we pick cbig enough.
  • 24. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.24 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 n0is a suitable constant. •For 1 ≤n< n0, we have “Θ(1)”≤cn3, if we pick cbig enough. This bound is not tight!
  • 25. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.25 A tighter upper bound? We shall prove that T(n) = O(n2).
  • 26. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.26 A tighter upper bound? We shall prove that T(n) = O(n2). Assume that T(k) ≤ck2for k< n: )( )2/(4)2/(4)( 222nOncnnncnnTnT= += +≤ +=
  • 27. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.27 A tighter upper bound? We shall prove that T(n) = O(n2). Assume that T(k) ≤ck2for k< n: )( )2/(4)2/(4)( 222nOncnnncnnTnT= += +≤ += Wrong!We must prove the I.H.
  • 28. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.28 A tighter upper bound? We shall prove that T(n) = O(n2). Assume that T(k) ≤ck2for k< n: )( )2/(4)2/(4)( 222nOncnnncnnTnT= += +≤ += Wrong!We must prove the I.H. 22)( cnncn≤ −−= for nochoice of c> 0. Lose! [ desired–residual]
  • 29. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.29 A tighter upper bound! IDEA:Strengthen the inductive hypothesis. •Subtracta low-order term. Inductive hypothesis: T(k) ≤c1k2–c2kfor k< n.
  • 30. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.30 A tighter upper bound! IDEA:Strengthen the inductive hypothesis. •Subtracta low-order term. Inductive hypothesis: T(k) ≤c1k2–c2kfor k< n. T(n)= 4T(n/2) + n = 4(c1(n/2)2–c2(n/2))+ n = c1n2–2c2n+ n = c1n2–c2n–(c2n–n) ≤c1n2–c2nifc2≥1.
  • 31. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.31 A tighter upper bound! IDEA:Strengthen the inductive hypothesis. •Subtracta low-order term. Inductive hypothesis: T(k) ≤c1k2–c2kfor k< n. T(n)= 4T(n/2) + n = 4(c1(n/2)2–c2(n/2))+ n = c1n2–2c2n+ n = c1n2–c2n–(c2n–n) ≤c1n2–c2nifc2≥1. Pick c1big enough to handle the initial conditions.
  • 32. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.32 Recursion-tree method •A recursion tree models the costs (time) of a recursive execution of an algorithm. •The recursion-tree method can be unreliable, just like any method that uses ellipses (…). •The recursion-tree method promotes intuition, however. •The recursion tree method is good for generating guesses for the substitution method.
  • 33. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.33 Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2:
  • 34. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.34 Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2: T(n)
  • 35. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.35 Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2: T(n/4)T(n/2) n2
  • 36. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.36 Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2: n2(n/4)2(n/2)2T(n/16)T(n/8)T(n/8)T(n/4)
  • 37. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.37 Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2: n2(n/16)2(n/8)2(n/8)2(n/4)2(n/4)2(n/2)2Θ(1) …
  • 38. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.38 Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2: n22n(n/16)2(n/8)2(n/8)2(n/4)2(n/4)2(n/2)2Θ(1) …
  • 39. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.39 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) … 2165n2nn2
  • 40. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.40 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) … 2165n2n225625nn2(n/2)2 …
  • 41. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.41 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) … 2165n2n225625n()()() 1316521651652L++++n= Θ(n2) … Total = n2(n/2)2geometric series
  • 42. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.42 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 fis asymptotically positive.
  • 43. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.43 Three common cases Compare f(n)with nlogba: 1.f(n) = O(nlogba–ε)for some constant ε> 0. •f(n) grows polynomiallyslower than nlogba(by an nεfactor). Solution: T(n) = Θ(nlogba).
  • 44. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.44 Three common cases Compare f(n)with nlogba: 1.f(n) = O(nlogba–ε)for some constant ε> 0. •f(n) grows polynomiallyslower than nlogba(by an nεfactor). Solution: T(n) = Θ(nlogba). 2.f(n) = Θ(nlogbalgkn)for some constant k≥0. •f(n) and nlogbagrow at similar rates. Solution: T(n) = Θ(nlogbalgk+1n).
  • 45. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.45 Three common cases (cont.) Compare f(n)with nlogba: 3.f(n) = Ω(nlogba+ ε)for some constant ε> 0. •f(n) grows polynomiallyfaster than nlogba(by an nεfactor), andf(n) satisfies the regularity conditionthat af(n/b) ≤cf(n) for some constant c< 1. Solution: T(n) = Θ(f(n)).
  • 46. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.46 Examples EX.T(n) = 4T(n/2) + na =4, b= 2 ⇒nlogba=n2; f(n) = n. CASE1: f(n) = O(n2–ε)for ε= 1. ∴T(n) = Θ(n2).
  • 47. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.47 Examples EX.T(n) = 4T(n/2) + na =4, b= 2 ⇒nlogba=n2; f(n) = n. CASE1: 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. CASE2: f(n) = Θ(n2lg0n), that is, k = 0. ∴T(n) = Θ(n2lgn).
  • 48. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.48 Examples EX.T(n) = 4T(n/2) + n3a =4, b= 2 ⇒nlogba=n2; f(n) = n3. CASE3: f(n) = Ω(n2+ ε)for ε= 1and4(n/2)3≤cn3 (reg. cond.) for c= 1/2. ∴T(n) = Θ(n3).
  • 49. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.49 Examples EX.T(n) = 4T(n/2) + n3a =4, b= 2 ⇒nlogba=n2; f(n) = n3. CASE3: f(n) = Ω(n2+ ε)for ε= 1and4(n/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).
  • 50. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.50 f(n/b) Idea of master theorem Recursion tree: f(n/b)f(n/b) Τ(1) … … f(n)af(n/b2)f(n/b2)f(n/b2)… a
  • 51. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.51 f(n/b) Idea of master theorem Recursion tree: f(n/b)f(n/b) Τ(1) … … f(n)af(n/b2)f(n/b2)f(n/b2)… af(n) af(n/b) a2f(n/b2) …
  • 52. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.52 f(n/b) Idea of master theoremf(n/b)f(n/b) Τ(1) … Recursion tree: … f(n)af(n/b2)f(n/b2)f(n/b2)… ah= logbnf(n) af(n/b) a2f(n/b2) …
  • 53. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.53 nlogbaΤ(1) f(n/b) Idea of master theoremf(n/b)f(n/b) Τ(1) … Recursion tree: … f(n)af(n/b2)f(n/b2)f(n/b2)… ah= logbnf(n) af(n/b) a2f(n/b2) #leaves = ah= alogbn= nlogba …
  • 54. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.54 f(n/b) Idea of master theorem Recursion tree: f(n/b)f(n/b) Τ(1) … … f(n)af(n/b2)f(n/b2)f(n/b2)… ah= logbnf(n) af(n/b) a2f(n/b2) CASE1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. CASE1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. Θ(nlogba) nlogbaΤ(1) …
  • 55. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.55 f(n/b) Idea of master theorem Recursion tree: f(n/b)f(n/b) Τ(1) … … f(n)af(n/b2)f(n/b2)f(n/b2)… ah= logbnf(n) af(n/b) a2f(n/b2) CASE2: (k= 0) The weight is approximately the same on each of the logbnlevels. CASE2: (k= 0) The weight is approximately the same on each of the logbnlevels. Θ(nlogbalgn) nlogbaΤ(1) …
  • 56. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.56 f(n/b) Idea of master theorem Recursion tree: f(n/b)f(n/b) Τ(1) … … f(n)af(n/b2)f(n/b2)f(n/b2)… ah= logbnf(n) af(n/b) a2f(n/b2) … CASE3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. CASE3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. nlogbaΤ(1) Θ(f(n))
  • 57. September 12, 2005 Copyright © 2001-5 Erik D. Demaine and Charles E. Leiserson L2.57 Appendix: geometric series 11112xxxxxnn− −=++++ + Lfor x≠1 1112xxx− =+++Lfor |x| < 1 Return to last slide viewed.