Recurrence Relations • A recurrence relation is an equation which is defined in terms of itself. • Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally modeled by recurrence relations
Example Merge Sort • Merge-Sort(A, p, r): if p < r then q¬(p+r)/2 Merge-Sort(A, p, q) Merge-Sort(A, q+1, r) Merge(A, p, q, r) •
Solving Recurrences • We will show 3 ways of solving recurrences: – Substitution method - Guess and prove • – Iteration method • – Use mathematical induction to solve your recursion Break the sums into a mathematical series Master theorem • Check for an instant solution
Substitution method • Guess a solution and them prove it using mathematical induction. • To guess the solution, play around with small values for insight n 0 1 2 3 4 5 6 7 0 1 3 7 15 31 63 127
Prove by induction • Claim 1. Show that the basis is true: 2. Now assume true for T(n-1 ) 3. Using this assumption show:
Iteration method • Example • How many terms until we reach T(1) ?
Iteration method
Recursion Trees •
Recursion Trees •
Recursion Trees
Recursion Trees •
Master Theorem • A powerful theorem allowing to solve a large class of recursion relations of the form where • There are 3 cases to remember: 1. If for some constant then
Master Theorem 2. If then 3. If for some constant and for some c < 1 then for all sufficiently large n
Using Master Theorem • T(n) = 4 T(n/2) + n • Case 1 of master theorem
Using Master Theorem •
Using Master Theorem • • Use case 2 of Master theorem
Using Iteration •
Using Master theorem •
Examples • • Use Master theorem case 1 where є = 1/6
Master theorem
Examples • Use case 2 of Master Theorem
Examples • Use Master Theorem – case 3
Example • Fits case 3 of Master theorem ?
Examples
Examples • Master Theorem – does not fit • Substitution method – too complicated • Recursion tree
• T(n) is at least the price for a complete tree of height T(n) is at most the price for a complete tree of height
• • • i=0 i=1 i=2 i=3 i=k n (3/4)n (9/16)n (27/64)n (3/4)^k * n