Practice in Algorithm Analysis 1 Solving Recurrences • Write recursive expression for time taken • Keep substituting and see a pattern • Find a value for k for which you know the solution (base case) – You may need to evaluate a series to get your final answer 2 Recursion Example 1 long f(int n) { if( n <= 1 ) return 1; else return 1 + f(n-1); } In terms of big-Oh: t(1) = 1 t(n) = 1 + t(n-1) Note: t(n-1) = 1 + t(n-2) t(n-2) = 1 + t(n-3) t(n-3) = 1 + t(n-4) In terms of big-Oh: t(1) = 1 t(n) = 1 + t(n-1) = 1 + 1 + t(n-2) = 1 + 1 + 1 + t(n-3) = ... k + t(n-k) If n-k = 1, we know the time Choose k = n-1 t(n) = n-1 + t(1) = n-1 + 1 = O(n) 3 Recursion Example 2 f(1) = 5 f(n) = 5 + f(n-1) In terms of big-Oh: t(1) = ? t(n) = ? 4 Recursion Example 2 f(1) = 5 f(n) = 5 + f(n-1) In terms of big-Oh: t(1) = 1 t(n) = 1 + t(n-1) 5 Recursion Example 3 f(1) = 5 f(n) = 5 + 5*f(n-1) In terms of big-Oh: t(1) = ? t(n) = ? 6 Recursion Example 3 f(1) = 5 f(n) = 5 + 5*f(n-1) In terms of big-Oh: t(1) = 1 t(n) = 1 + t(n-1) 7 Recursion Example 4 f(1) = 5 f(n) = 5 + 5*f(n-1) + 2*f(n-1) In terms of big-Oh: t(1) = ? t(n) = ? 8 Recursion Example 4 f(1) = 5 f(n) = 5 + 5*f(n-1) + 2*f(n-1) In terms of big-Oh: t(1) = 1 t(n) = 1 + 2*t(n-1) 9 Recursion Example 5 f(1) = 5 f(n) = 5 + 5*f(n/3) + 2*f(n/3) In terms of big-Oh: t(1) = ? t(n) = ? 10 Recursion Example 5 f(1) = 5 f(n) = 5 + 5*f(n/3) + 2*f(n/3) In terms of big-Oh: t(1) = 1 t(n) = 1 + 2*t(n/3) t(n/3) = ? t(n/9) = ? t(n/27) = ? 11 Recursion Example 5 f(1) = 5 f(n) = 5 + 5*f(n/3) + 2*f(n/3) In terms of big-Oh: t(1) = 1 t(n) = 1 + 2*t(n/3) t(n/3) = 1 + 2*t(n/9) t(n/9) = 1 + 2*t(n/27) t(n/27) = 1 + 2*t(n/81) Find the general pattern for t(n) by repeated substitution 12 Recursion Example 5 f(1) = 5 f(n) = 5 + 5*f(n/3) + 2*f(n/3) In terms of big-Oh: t(1) = 1 t(n) = 1 + 2*t(n/3) t(n) = 1 + 2*t(n/3) = 1 + 2[1+2*t(n/9)] = 1 + 2 + 4*t(n/9) = 1 + 2 + 4[1+ 2*t(n/27)] = 1 + 2 + 4 + 8*t(n/27) = 1 + 2 + 4 + ... 2k-1 + 2kt(n/3k) = 2k-1 + 2kt(n/3k) t(n/3) = 1 + 2*t(n/9) t(n/9) = 1 + 2*t(n/27) t(n/27) = 1 + 2*t(n/81) What value of k should we choose? 13 Recursion Example 5 f(1) = 5 f(n) = 5 + 5*f(n/3) + 2*f(n/3) In terms of big-Oh: t(1) = 1 t(n) = 1 + 2*t(n/3) t(n/3) = 1 + 2*t(n/9) t(n/9) = 1 + 2*t(n/27) t(n/27) = 1 + 2*t(n/81) t(n) = 1 + 2*t(n/3) = 1 + 2[1+2*t(n/9)] = 1 + 2 + 4*t(n/9) = 1 + 2 + 4[1+ 2*t(n/27)] = 1 + 2 + 4 + 8*t(n/27) = 1 + 2 + 4 + ... 2k-1 + 2kt(n/3k) = 2k-1 + 2kt(n/3k) n/3k = 1, so n = 3k so k = log3n Substitute for k 14 Recursion Example 5 f(1) = 5 f(n) = 5 + 5*f(n/3) + 2*f(n/3) In terms of big-Oh: t(1) = 1 t(n) = 1 + 2*t(n/3) t(n/3) = 1 + 2*t(n/9) t(n/9) = 1 + 2*t(n/27) t(n/27) = 1 + 2*t(n/81) t(n) = 1 + 2*t(n/3) = 1 + 2[1+2*t(n/9)] = 1 + 2 + 4*t(n/9) = 1 + 2 + 4[1+ 2*t(n/27)] = 1 + 2 + 4 + 8*t(n/27) = 1 + 2 + 4 + ... 2k-1 + 2kt(n/3k) = 2k-1 + 2kt(n/3k) n/3k = 1, so n = 3k so k = log3n t(n) = 2log3n - 1 + 2log3n t(1) = O(2log3n) = O(2 log2n log32) = O(nlog32) 15