L13cs2110su09 2
L13cs2110su09 2
Recurrences
Lecture 13
CS2110 – Summer 2009
In the past…
Complexity:
Big-O definition
Non-recursive programs
2
Today
Complexity of recursive programs
Introduce recurrences
Methods to solve recurrences
Substitution
Recursion tree
Master
3
Analysis of Merge-Sort
public static Comparable[] mergeSort(Comparable[] A, int low, int high) {
if (low < high) { //at least 2 elements? cost = c
int mid = (low + high)/2; cost = d
Comparable[] A1 = mergeSort(A, low, mid); cost = T(n/2) + e
Comparable[] A2 = mergeSort(A, mid+1, high); cost = T(n/2) + f
return merge(A1,A2); cost = gn + h
}
Comparable[] temp = new Comparable[1]; cost = i (base case)
temp[0] = A[low];
return temp;
}
Recurrence:
T(n) = c + d + e + f + 2T(n/2) + gn + h recurrence
T(1) = i base case
Some techniques:
Substitution:
Make a guess, then prove the guess correct by induction
Can sometimes change variables to get a simpler recurrence
Recursion-tree:
Build a recursion tree and use it to determine solution
Can use the Master Method
A “cookbook” scheme that handles many common recurrences
5
Substitution Method
1. Guess the form of the solution
2. Use induction to show that the solution works.
6
Ex1: Analysis of Merge-Sort
Recurrence:
T(n) = c + d + e + f + 2T(n/2) + gn + h
T(1) = i
7
Analysis of Merge-Sort
Induction step
T(n) 2T(n/2) + kn
2(pn/2 log n/2) + kn (IH)
= pn (log n – 1) + kn
= pn log n + (k – p)n
pn log n 8
Ex2: The Fibonacci Function
Mathematical definition:
fib(0) = 0
fib(1) = 1
fib(n) = fib(n 1) + fib(n 2), n ≥ 2
9
Recursive Execution
static int fib(int n) {
if (n == 0) return 0;
else if (n == 1) return 1;
else return fib(n-1) + fib(n-2);
}
fib(3) fib(2)
fib(1) fib(0)
10
The Fibonacci Recurrence
static int fib(int n) {
if (n == 0) return 0;
else if (n == 1) return 1;
else return fib(n-1) + fib(n-2);
}
T(0) = c
T(1) = c
T(n) = T(n – 1) + T(n – 2) + c
Solution is exponential in n
But not quite O(2n)...
11
The Golden Ratio
= (a+b)/b = b/a
2 = + 1
1+5
=
2
ratio of sum of sides
(a+b) to longer side (b) = 1.618...
a =
ratio of longer side (b) to
b
shorter side (a)
12
Fibonacci Recurrence is O(n)
• Basis:
• T(0) = c = c0
• T(1) = c c1
• Induction step:
• T(n+2) = T(n+1) + T(n) cn+1 + cn = cn+2
13
Detour:
Can We Do Better?
if (n <= 1) return n;
int parent = 0;
int current = 1;
for (int i = 2; i ≤ n; i++) {
int next = current + parent;
parent = current;
current = next;
}
return (current);
14
Detour:
...But We Can Do Even Better!
Let fn denote the nth Fibonacci number
f0 = 0
f1 = 1
fn+2 = fn+1 + fn, n 0
0 1 fn fn+1 0 1 n f0 fn
Note that = , thus =
1 1 fn+1 fn+2 1 1 f1 fn+1
17
Hints on Proving
Prove the exact form
E.g., T(n) = 2T(n/2) + n, guess T(n) = O(n)
T(n) ≤ 2(cn/2) + n
≤ cn + n
= O(n) WRONG
Change variables
E.g., T(n) = 2T(n.5) + log n
Let m = log n
=> T(2m) = 2T(2m/2) + m
Let S(m) = T(2m)
=> S(m) = 2S(m/2) + m
=> S(m) = O(m log m)
=> T(n) = T(2m) = S(m) = O(m log m) = O(log n * log log n)
18
Recursion Tree Method
Node = cost of a single subproblem
Sum nodes in same level to get per-level costs
Sum all per-level costs to get total costs
19
Ex: Merge-Sort
T(n) = 2T(n/2) + cn
cn cn
c(n/2) c(n/2) cn
c c c c c c c c c c cn
21
Recurrence Examples
22
Moral: Complexity Matters!
But you are acquiring the best tools to deal with it!
23