Tutorial Exercise (Recurrences) CS3381 Design and Analysis of Algorithms Helena Wong, 2001 1. Describe how Merge-Sort follows the Divide-and-Conquer paradigm. 2. To prove the correctness of the Merge(A,p,q,r) procedure. Let’s state the loop invariant as “At the start of each iteration of the for loop of lines 12-17, the subarray A[p..k-1] contains the k-p smallest elements of L[1..n1+1] and R[1..n2+1], in sorted order. Moreover, L[I] and R[j] are the smallest elements of their arrays that have not been copied back to A.” Complete the prove by showing the 3 properties of the invariant. 3. Use a recursion tree to determine a good asymptotic upper bound on the recurrence: T(n) = 3T(n/2)+n 1 if n>1 if n=1 Level 0 => n n n T(n) T(n/2) T(n/2) Level 1 T(n/2) n/2 n/2 n/2 => (3/2)n Level 2 T(n/4) T(n/4) T(n/4) 2 T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) => (3/2) n => (3/2)(lg n)-1n Level (lg n)-1 Level lg n 1 1 1 1 1 1 1 1 1 1 1 1 3lg n T(n)=n+1.51n+1.52n+..+1.5(lg n – 1)n + 3lg n can be verified using T(4)=3T(2)+4=3[3T(1)+2]+4=3*5+4=19 T(n)=n(1+1.5+1.52+1.5(lg n – 1))+3lg n =n *(1.5lg n – 1) / 0.5 + 3 lg n = 2n*1.5lg n - 2n + 3lg n = 2n*nlg1.5 – 2n + nlg3 = 2n1+lg1.5-2n+nlg3=2nlg3-2n+nlg3=3nlg3-2n => Answer: O(nlg3) 1 => 3lg n * 1 1+x+x2+..+xk = (xk+1-1)/(x-1) alogbC=clogba, logba=logca / logcb Tutorial Exercise (Recurrences) CS3381 Design and Analysis of Algorithms Helena Wong, 2001 4. Given T(n)=T(n/2)+T(n/2)+1. We guess that it is O(n). However, it is difficult to prove that “T(n) cn for some positive constant c”, because by substitution (T(n/2) and T(n/2) are true), T(n) c(n/2)+c(n/2)+1=cn+1, that does not imply T(n) cn. Try to prove that T(n) is O(n) by showing T(n) cn -b, for some positive constants c and b. Note: Normally, if not specified, we assume T(1)=1. When n=1, T(1) =1 cn-b when c=2 and b=1 Assume that “T(n) cn –b, for some +ve c,b” is true for T(n/2) and T(n/2), ie. T(n/2) cn/2 -b and T(n/2) cn/2 -b for some +ve c and b. T(n) =T(n/2)+T(n/2)+1 (cn/2-b)+(cn/2-b)+1 = cn-2b+1 cn-b as long as b>=1 Conclusion: taking c as 2 and b as 1, we have seen T(n) cn –b is true when n=1, and we have proven that it is true for T(n) by assuming T(n/2) and T(n/2) are true. “T(n) cn –b, for some +ve c,b” is true “T(n) cn, for some +ve c,b” is true T(n) is O(n). 5. Use the master method to give tight asymptotic bounds for the following recurrences: a. T(n) = 4T(n/2)+n b. T(n) = 4T(n/2)+n2 c. T(n) = 4T(n/2)+n3 Master Theorem For T(n) = a T(n/b) + f(n) where a >=1 and b > 1. If f(n) = O(nlogba-) If f(n) = (nlogba), for some constant >0, then T(n) = (nlogbalg n) If f(n) = (nlogba+) for some constant then T(n) = (nlogba) >0, and if a f (n/b) <= c f(n) for some constant c <1 and all sufficiently large n, then T(n) = (f(n)) a=4, b=2, => nlogba=n2 (a) f(n)=n, => f(n)=nlogba-1=> f(n)=O(n logba-) where =1>0 => case 1 => T(n)=(nlogba)= (n2) (b) f(n)=n2, => f(n)=nlogba=> f(n)= (n logba) => case 2 => T(n)=(nlogbalg n)= (n2lg n) (c) f(n)=n3, => f(n)=nlogba+1=> f(n)= (n logba+) where =1>0 a f(n/b)=4*f(n/2)=4*(n/2)3=n3/2=0.5*f(n)=c*f(n) where c=0.5<1 => case 3 => T(n)=(f(n)) = (n3)