Professional Documents
Culture Documents
Introduction To Algorithms: Asymptotic Notation Recurrences
Introduction To Algorithms: Asymptotic Notation Recurrences
6.046J/18.401J LECTURE 2
Asymptotic Notation O-, -, and -notation Recurrences 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.1
Asymptotic notation
O-notation (upper bounds): We We write write ff((n n)) = =O O((g g((n n)) )) if if there there exist >0 0 such such exist constants constants c c> > 0, 0, n n00 > that that 0 0 ff((n n)) cg cg((n n)) for for all all n n n n00..
L2.2
Asymptotic notation
O-notation (upper bounds): We We write write ff((n n)) = =O O((g g((n n)) )) if if there there exist >0 0 such such exist constants constants c c> > 0, 0, n n00 > that that 0 0 ff((n n)) cg cg((n n)) for for all all n n n n00.. EXAMPLE: 2n2 = O(n3) (c = 1, n0 = 2)
L2.3
Asymptotic notation
O-notation (upper bounds): We We write write ff((n n)) = =O O((g g((n n)) )) if if there there exist >0 0 such such exist constants constants c c> > 0, 0, n n00 > that that 0 0 ff((n n)) cg cg((n n)) for for all all n n n n00.. EXAMPLE: 2n2 = O(n3)
functions, not values
September 12, 2005 Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.4
(c = 1, n0 = 2)
Asymptotic notation
O-notation (upper bounds): We We write write ff((n n)) = =O O((g g((n n)) )) if if there there exist >0 0 such such exist constants constants c c> > 0, 0, n n00 > that that 0 0 ff((n n)) cg cg((n n)) for for all all n n n n00.. EXAMPLE: 2n2 = O(n3)
functions, not values
September 12, 2005
(c = 1, n0 = 2)
funny, one-way equality
L2.5
L2.6
L2.7
Macro substitution
Convention: A set in a formula represents an anonymous function in the set.
L2.9
Macro substitution
Convention: A set in a formula represents an anonymous function in the set. EXAMPLE: f(n) = n3 + O(n2) means f(n) = n3 + h(n) for some h(n) O(n2) .
L2.10
Macro substitution
Convention: A set in a formula represents an anonymous function in the set. EXAMPLE: n2 + O(n) = O(n2) means for any f(n) O(n): n2 + f(n) = h(n) for some h(n) O(n2) .
Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.11
L2.12
L2.13
n = (lg n) (c = 1, n0 = 16)
Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.14
L2.15
n 2 n = ( n )
2 2
L2.16
((g g((n n)) )) = ={ { ff((n n)) :: for for any any constant constant c c> > 0, 0,
there >0 0 there is is a a constant constant n n00 > such such that that 0 0 ff((n n)) < < cg cg((n n)) for } for all all n n n n00 } (n0 = 2/c)
L2.17
((g g((n n)) )) = ={ { ff((n n)) :: for for any any constant constant c c> > 0, 0,
there >0 0 there is is a a constant constant n n00 > such such that that 0 0 cg cg((n n)) < < ff((n n)) for } for all all n n n n00 } (n0 = 1+1/c)
L2.18
EXAMPLE:
September 12, 2005
n = (lg n)
Solving recurrences
The analysis of merge sort from Lecture 1 required us to solve a recurrence. Recurrences are like solving integrals, differential equations, etc. o Learn a few tricks. Lecture 3: Applications of recurrences to divide-and-conquer algorithms.
L2.19
Substitution method
The most general method: 1. Guess the form of the solution. 2. Verify by induction. 3. Solve for constants.
L2.20
Substitution method
The most general method: 1. Guess the form of the solution. 2. Verify by induction. 3. Solve for constants. EXAMPLE: T(n) = 4T(n/2) + n [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.
September 12, 2005 Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.21
Example of substitution
T (n) = 4T (n / 2) + n 4c ( n / 2 ) 3 + n = ( c / 2) n 3 + n desired residual = cn3 ((c / 2)n3 n) cn3 desired whenever (c/2)n3 n 0, for example, if c 2 and n 1. residual
September 12, 2005 Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.22
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.
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 n0 is a suitable constant. For 1 n < n0, we have (1) cn3, if we pick c big enough.
This bound is not tight!
September 12, 2005 Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.24
L2.25
L2.26
L2.27
L2.29
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.32
L2.33
L2.34
L2.35
L2.36
(n/8)2
n2
(n/8)2
n2
5 n2 16
(n/8)2
n2
5 n2 16 25 n 2 256
(n/8)2
n2
5 n2 16 25 n 2 256
5 3 16
(n/8)2
(1)
September 12, 2005
Total = n = (n2)
2 5 5 1 + 16 + 16
+L geometric series
L2.41
( ) +( )
L2.42
L2.43
L2.45
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).
L2.46
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) = (n2lg0n), that is, k = 0. T(n) = (n2lg n).
September 12, 2005 Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.47
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(n/2)3 cn3 (reg. cond.) for c = 1/2. T(n) = (n3).
L2.48
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(n/2)3 cn3 (reg. cond.) for c = 1/2. T(n) = (n3). EX. T(n) = 4T(n/2) + n2/lg n a = 4, b = 2 nlogba = n2; f (n) = n2/lg n. Master method does not apply. In particular, for every constant > 0, we have n = (lg n).
September 12, 2005 Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.49
f (n)
(1)
September 12, 2005 Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.50
f (n)
(1)
September 12, 2005 Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.51
f (n)
(1)
September 12, 2005 Copyright 2001-5 Erik D. Demaine and Charles E. Leiserson L2.52
f (n)
(1)
September 12, 2005
nlogba (1)
L2.53
f (n)
f (n)
(1)
September 12, 2005
C ASE :: (( k C ASE 2 2 k= = 0) 0) The The weight weight is is approximately approximately the the same same on on each n each of of the the log log n levels. levels. bb
f (n)
L2.57