Algorithms
Algorithms
6.046J/18.401J/SMA5503
Lecture 2
Prof. Erik Demaine
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.
Day 3
Introduction to Algorithms
L2.2
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.
Day 3 Introduction to Algorithms L2.3
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
Day 3 Introduction to Algorithms L2.4
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!
Day 3 Introduction to Algorithms L2.5
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.
Day 3 Introduction to Algorithms L2.8
Day 3
Introduction to Algorithms
L2.9
Day 3
Introduction to Algorithms
L2.10
Day 3
Introduction to Algorithms
L2.11
Day 3
Introduction to Algorithms
L2.12
(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)
Day 3
Total = n = (n2)
5 + 5 2 1 + 16 16
+L geometric series
L2.17
( ) +( )
Introduction to Algorithms
Day 3
Introduction to Algorithms
L2.18
Day 3
Introduction to Algorithms
L2.20
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).
Day 3 Introduction to Algorithms L2.21
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/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).
Day 3 Introduction to Algorithms L2.22
(ai /bip ) = 1.
Then, the answers are the same as for the master method, but with np instead of nlogba. (Akra and Bazzi also prove an even more general result.)
Day 3 Introduction to Algorithms L2.23
i =1
f (n)
L2.24
(1)
Day 3
f (n)
f (n)
(1)
Day 3
CASE 2: (k = 0) The weight CASE 2: (k = 0) The weight is approximately the same on is approximately the same on each of the logbbn levels. each of the log n levels.
Introduction to Algorithms
f (n)
Conclusion
Next time: applying the master method. For proof of master theorem, see CLRS.
Day 3
Introduction to Algorithms
L2.28
Day 3
Introduction to Algorithms
L2.29