460 likes | 1.16k Vues
Recurrence relations are equations that define sequences based on previous terms. These are commonly used in algorithms, especially in divide-and-conquer strategies like Merge Sort. We explore various methods for solving recurrences: the substitution method, which involves guessing and proving solutions through mathematical induction; the iteration method and recursion trees; and the Master Theorem for classifying and solving recurrences. Each method has its own application and usefulness in analyzing algorithm complexity.
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