=============================================================== Lect 16 - November 18, 2008 - ECS 20 - Fall 2008 - Phil Rogaway =============================================================== Today: o Quiz 3 o Divide and conquer / Solving recurrence relations --------------------------- EXAMPLE 1: Cake cutting. --------------------------- Review algorithm from discussion section. 1. Persons 1.. n-1 people divide the cake (into n-1 pieces) using a recursive call to this procedure. 2. Each person divides his share into n pieces 3. Person n takes the largest piece from each of of the set of shares from person 1..n-1 4. Each of 1..n-1 take their remaining pieces. To analyze: Let T(n) = the total number of pieces of cakes produced as the parties run the algorithm above (I'm assuming that pieces of cake are not "recombined", as this is not specified in the algorithm). Note that this is one more than that number of cuts made. Then Let T_n = T_{n-1} + n for n\ge 2 T_1 = 1 Solve by repeated substitution: T(n) = n T(n-1) = n (n-1) T(n-2) = n (n-1) (n-2) T(n-3) = ... = n! Yuck! (Note that I stumbled in class, initially defining T(n) as the time to run the algorithm or the number of cuts a party performs, but the definition above is best.) --------------------------- EXAMPLE 2: Towers of Hanoi. --------------------------- We did this. T_n = 2 T_{n-1} + 1 for n\ge 1 0 for n=0 Re-do the analysis using repeated substitution. Now show that you prove the answer by induction on n Claim: The solution to the recurrence above is T_n = 2^n - 1 T_0 = 0 OK (T_1 = 1 OK) (T_2 = 3 OK) Suppose true for n = k. T_k = 2^k - 1. Want to show true for n = k+1: T_{k+1} = 2T_k - 1 = 2(2^k-1) + 1 =2^{k+1} - 2 + 1 =2^{k+1} + 1 QED ------------------------------------------------- Example 3: another recurrence: T(n) = 3T(n/2) + n ------------------------------------------------- Repeatedly substitute T(n) = 3T(n/2) + n = 3[3T(n/4) + n/2)] + n = 3^2 T(N/4) + 3n/2 + n = ... = 3^4 T(N/16) + n(1+3/2 + (3/2)^2 + (3/2)^3) = O(3^{lg n} + n[1 + p + p^2 + ... + p^{lg n}) where p=3/2 ** Now if 1 + p + p^2 + ... + p^k = x then 1 + p + p^2 + ... + p^k + p^{k+1}= p x + 1 x + p^{k+1}= p x + 1 p^{k+1} - 1 = x(p-1) so x = (p^{k+1} - 1)/(p-1) Substituting in ** x for the 1 + p ... p^lg n we have = O(3^{lg n} + n((3/2)^lg n - 1)) = O(3^{lg n} + n((3/2)^lg n)) = O(n^{lg 3} + n(n^lg(3/2)) = O(n^{lg 3} + n^{lg 3}) = O(n^1.59) -------------------------------- Master Theorem -------------------------------- If T(n)= aT(\lceil n/b\rceil) + O(n^d) for a,b>0, d\ge 0, then T(n) = O(n^{log_b a}) if d < log_b a T(n) = O(n^d) if d > log_b a T(n) = O(n^d log n) if d = log_b a eg: T(n) = 4T(n/2) + n O(n^2) T(n) = 4T(n/2) + n^2 O(n^2 log n) T(n) = 4T(n/2) + n^3 O(n^3) ----------------------------------- EXAMPLE 4: Binary search ----------------------------------- Example 1. Binary search Given: A_1 <= ... <= A_n and x determine if x =a_i for some i. Algorithm BS(A,l,r,x) if l>r then return false; if l=r then return (x==a_l); mid = \lceil l+r/2 \rceil if l=r then return (a_mid == x) else if x<=a_mid then return BS(A,l,mid-1) else return BS(A,mid+1,r) Let T(n) = # of comparisons in this algorithm T(1) = 1 T(n) = T(\lceil n/2\rceil) + 1 Usually ok to ignore ceilings, floors: T(n) = T(n/2) + 1 // 2^0 = T(n/4) + 2 = T(n/2^2) + 2 //2^1 = T(n/8) + 3 = T(n/2^3) + 3 // = T(n/2^k) + (k-1) ... = T(n/n) + (lg n -1) = 1 + (lg n -1) = lg n OR: just use the "Master Theorem"