Title: Practice in Algorithm Analysis
1Practice inAlgorithm Analysis
2Solving 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
3Recursion Example 1
- long f(int n)
-
- if( n lt 1 )
- return 1
- else
- return 1 f(n-1)
-
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)
In terms of big-Oh t(1) 1 t(n) 1 t(n-1)
If n-k 1, we know the time Choose k n-1 t(n)
n-1 t(1) n-1 1 O(n)
Note t(n-1) 1 t(n-2) t(n-2) 1
t(n-3) t(n-3) 1 t(n-4)
4Recursion Example 2
In terms of big-Oh t(1) ? t(n) ?
4
5Recursion Example 2
In terms of big-Oh t(1) 1 t(n) 1 t(n-1)
5
6Recursion Example 3
In terms of big-Oh t(1) ? t(n) ?
6
7Recursion Example 3
In terms of big-Oh t(1) 1 t(n) 1 t(n-1)
7
8Recursion Example 4
- f(1) 5
- f(n) 5 5f(n-1) 2f(n-1)
In terms of big-Oh t(1) ? t(n) ?
8
9Recursion Example 4
- f(1) 5
- f(n) 5 5f(n-1) 2f(n-1)
In terms of big-Oh t(1) 1 t(n) 1 2t(n-1)
9
10Recursion Example 5
- f(1) 5
- f(n) 5 5f(n/3) 2f(n/3)
In terms of big-Oh t(1) ? t(n) ?
10
11Recursion Example 5
- f(1) 5
- f(n) 5 5f(n/3) 2f(n/3)
In terms of big-Oh t(1) 1 t(n) 1 2t(n/3)
t(n/3) ? t(n/9) ? t(n/27) ?
11
12Recursion Example 5
- f(1) 5
- f(n) 5 5f(n/3) 2f(n/3)
In terms of big-Oh t(1) 1 t(n) 1 2t(n/3)
t(n/3) 1 2t(n/9) t(n/9) 1
2t(n/27) t(n/27) 1 2t(n/81)
Find the general pattern for t(n) by repeated
substitution
12
13Recursion Example 5
- f(1) 5
- f(n) 5 5f(n/3) 2f(n/3)
In terms of big-Oh t(1) 1 t(n) 1 2t(n/3)
t(n) 1 2t(n/3) 1 212t(n/9)
1 2 4t(n/9) 1 2 41
2t(n/27) 1 2 4 8t(n/27)
1 2 4 ... 2k-1 2kt(n/3k) 2k-1
2kt(n/3k)
t(n/3) 1 2t(n/9) t(n/9) 1
2t(n/27) t(n/27) 1 2t(n/81)
What value of k should we choose?
13
14Recursion Example 5
- f(1) 5
- f(n) 5 5f(n/3) 2f(n/3)
In terms of big-Oh t(1) 1 t(n) 1 2t(n/3)
t(n) 1 2t(n/3) 1 212t(n/9)
1 2 4t(n/9) 1 2 41
2t(n/27) 1 2 4 8t(n/27)
1 2 4 ... 2k-1 2kt(n/3k) 2k-1
2kt(n/3k)
t(n/3) 1 2t(n/9) t(n/9) 1
2t(n/27) t(n/27) 1 2t(n/81)
n/3k 1, so n 3k so k log3n
Substitute for k
14
15Recursion Example 5
- f(1) 5
- f(n) 5 5f(n/3) 2f(n/3)
In terms of big-Oh t(1) 1 t(n) 1 2t(n/3)
t(n) 1 2t(n/3) 1 212t(n/9)
1 2 4t(n/9) 1 2 41
2t(n/27) 1 2 4 8t(n/27)
1 2 4 ... 2k-1 2kt(n/3k) 2k-1
2kt(n/3k)
t(n/3) 1 2t(n/9) t(n/9) 1
2t(n/27) t(n/27) 1 2t(n/81)
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