220 likes | 502 Views
Recurrence Relations. By: Sean Lyn March 25, 2008. Algorithm Analysis. “The process of deriving the estimates for the time and space required to execute an algorithm.” Direct Proof Proof by Contradiction Proof by Induction. Time Complexity. Θ (g(n)) = is g(n) O(g(n)) = is at most g(n)
E N D
Recurrence Relations By: Sean Lyn March 25, 2008
Algorithm Analysis • “The process of deriving the estimates for the time and space required to execute an algorithm.” • Direct Proof • Proof by Contradiction • Proof by Induction
Time Complexity • Θ(g(n)) = is g(n) • O(g(n)) = is at most g(n) • Ω(g(n)) = is at least g(n)
Examples • for (x=0; x<n; x++) a+=1; • for (x=0; x<n; x++){ for (y=0; y<n; y++) a+=1; }
Recurrence Relation • “A recurrence relation relates the nth element of a sequence to [a] certain of its predecessors” • In other words, how do you define An in terms of A1,A2,A3,… An-1 ?
Uses • Used for describing the time required by an algorithm • It can be used in defining a sequence
Fibonacci Sequence • Fn = Fn-1 + Fn-2 where n > 2, F1 = 1, and F2 = 1
Mandlebrot Set • Zn+1 = (Zn)² + C, Z0 = C
Example • Cn is the number of times the statement x=x+1 executes example(n) { if (n==1) return; for (i=1; x<n; x++) x = x+1; example(n/2); } C1 = 0 We get the recurrence relation Cn = n + C[n/2]
Example 2 • Solving a recurrence relation, or producing a solution without using Ci for any i. • Given: Cn = Cn-1 + n, n>=1 and C0 = 0 • Cn-1 = Cn-2 + n-1 -> Cn = (Cn-2+n-1)+n • Repeat…
Example 2 Cont. • Cn = Cn-1 + n • Cn = Cn-2 + (n-1) + n • Cn = Cn-3 + (n-2) + (n-1) + n • Which gives us… • Cn = C1 + 2 + 3 + … (n-2) + (n-1) + n • Cn = C0 + 1 + 2 + … (n-2) + (n-1) + n • Cn = 0 + 1 + 2 +…. n • Cn = n(n+1)/2
Main Recurrence Theorem • Let a, b and k be integers satisfying a>=1, b >= 2, and k >= 0. • The recurrence relation must have the form: T(n) = aT(n/b) + f(n), where f(n) = O(nk) / O(nk) if a < bk • T(n) = | O(nklogn) if a = bk \ O(nlogba) if a > bk This also applies to Θ and Ω
Using the MRT • Find O() for the relation • T(n) = 2T(n/2) + n • a = 2, b = 2, k = 1 • We can see that 2 = 2¹ • Therefore a = bk so we get O(nlogn)
Example 2 • Find O() for the relation • T(n) = 3T(n/2) + n² • a = 3, b = 2, k = 2 • We can see that 3 < 2² • Therefore a < bk so we get O(n²)
Putting it all Together • Recurrence relations can be used to define the time complexity of an algorithm • Recurrence relations can be used to define sequences • Time complexities are important because computers have limited memory. • The main recurrence theorem can be used to solve the time complexities of recurrence relations
Questions • What is one example of a sequence generated by a recurrence relation? • Using the MRT, what is the time complexity of the function: T(n) = T(n)+n ?
Works Cited • Johnsonbaugh, Richard and Marcus Schaefer. Algorithms. New Jersey: Pearson Education, Inc, 2004. • “Recurrence Equation.” http://mathworld.wolfram.com. 2008. 25 March 2008. <http://mathworld.wolfram.com/RecurrenceEquation.html>.