460 likes | 1.06k Views
Recurrence Relations. 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.
E N D
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 • Use mathematical induction to solve your recursion • Iteration method • Break the sums into a mathematical series • Master theorem • Check for an instant solution
n 0 1 2 3 4 5 6 7 0 1 3 7 15 31 63 127 Substitution method • Guess a solution and them prove it using mathematical induction. • To guess the solution, play around with small values for insight
Prove by induction • Claim • Show that the basis is true: • Now assume true for T(n-1 ) • Using this assumption show:
Iteration method • Example • How many terms until we reach T(1) ?
Master Theorem • A powerful theorem allowing to solve a large class of recursion relations of the form where • There are 3 cases to remember: • If for some constant then
Master Theorem • If then • 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 • Use case 2 of Master theorem
Examples • Use Master theorem case 1 where є = 1/6
Examples • Use case 2 of Master Theorem
Examples • Use Master Theorem – case 3
Example • Fits case 3 of Master theorem ?
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 n • i=1 (3/4)n • i=2 (9/16)n • i=3 (27/64)n • i=k (3/4)^k * n