VIEWS: 143 PAGES: 24 CATEGORY: College POSTED ON: 2/14/2011
Recurrence Relation Substitution Method • Two steps – Guess the form of the solution – Use mathematical induction to find the constants and show the solution works • Useful when it is easy to guess the form of the answer • Can be used to establish either upper or lower bound on a recurrence • Example: T (n) 2T (n / 2) n T(n) = O(n lg n) ? 2 Substitution Method – Examples: solve T(n) = 2T(n/2) + n , T(1)=1 Step one Guess T(n)<=cn lgo n For some constant c (ie T(n)=O(nlog n)---using Big O notation) Step two Calculate constant c T(1)= 1 given T(2)=2T(2/2)+2 2*1+2 4 --(1) Substitute n=2 in the guess T(2)<= c*2 log 2 2C --(2) From (1) and (2) C=4/2=2 3 Substitution Method Using mathematical induction to show the solution is working Substitute n=n/2 Then T(n/2)=c n/2 log n/2 (guess) Prove that holds for n : T(n)<=c n log n T(n) =2T(n/2)+n = 2(c n/2 log n/2)+n -------substitute T(n/2)=c n/2 logn/2 = c n log n/2 +n = c n log n – c n log 2 +n ---- log a/b log a- log b =c n log n – c n + n ---- log 2 1 = c n log n – c n + n ---- when eliminating the –c n+ n we get <= c n log n T(n) < = c n log n (guess is correct) 4 Iterative method It does not required and guess like substitution method Here we iteratively unfold the recurrence until we get the pattern Example Solve T(n)= 8 T(n/2) + n2 and given T(1)=1 Find the value of T(n/2)T(n/2)=8 T(n/22)+(n/2)2 Substitute this T(n/2) T(n/2)=8 [8 T(n/22)+(n/2)2] + n2 = n2 + 82 T(n/22) + 8 (n/2)2 = n2 + 82 T(n/22) + 2n2 = n2 + 2n2 + 82 T(n/22) Find T(n/22) from the question and substitute = n2 + 2n2 + 82 [8 T(n/23) + (n/4)2] like this - . . .. = n2 + 2n2 + 22n2 + 23n3 + . . . . this can finally be represented by GP(geomertic progression) or in AP (arithmetic progression) 5 Changing Variable Method Sometimes recurrence can be reduced to simpler one by changing variables Example T(n) =2 T√n + log n Let n changed to m= log n n=2m (log n =log 2m log n= m (log22 =1)) Then T(2m) = 2 T (2m)1/2 + m T(2m) = 2 T (2m/2)+ m let S(m)= T(2m) then by substituting (ie T(2m/2)=s(m/2)) S(m)=2 S(m/2) + m 6 Masters Theorem • This method is used for solving Cookbook type recurrence T (n) aT (n / b) f (n) – Describe the running time of an algorithm that divides a problem of size n into ‘a’ sub problems, each of size n/b – a 1 and b > 1 (constant), f(n): asymptotically positive – The sub problems are solved recursively each in T(n/b) time – The cost of splitting the problem and combining of sub problems is given by f(n) – The no of leaves in the recursion tree is represented by nE – Where E= log a/ log b 7 Masters Theorem Let T(n) be defined on the non-negative integer fun T(n)=a T(n/b) + f(n) where a 1 and b > 1 are constants and f(n) be a function The solution is determined by three cases Case 1: if f(n) € O(nE-e) for some e>0, then T(n) € Q(nE) Case 2: if f(n) € Q(nE) then T(n) € Q(f(n).log n) Case 3: if f(n) € (nE-e) for some e>o and f(n) € O(nE+δ) for some δ >=e then T(n) € Q (f(n)) 8 Masters Theorem Case 1: T(n)=4T(n/2) + n E=log a/ log b ---- a=4, b=2 then E=log 4/log 2 and f(n)= n E=log (2)2/log 2 E=2 log 2/log 2 2 if f(n) € O(nE-e) for some e>0, then T(n) € Q(nE) e >0 assume e=1 f(n) € O(nE-e) O(n2-1) € O(n) Then T(n) € Q(nE) € Q(n2) Ie f(n) € O(n) then T(n) € Q(n2) 9 Masters Theorem Case 2: T(n)=4 T(n/2)+ n2 a=4 and b=2 f(n)=n2 E=log a/ log b log 4/log 2 log(2)2/log 2 2log 2/log 2 2 if f(n) € Q(nE) then T(n) € Q(f(n).log n) f(n) € Q(nE) € Q(n2) then T(n) € Q(f(n).log n) € Q(n2.log n) Ie f(n) € Q(n2) then T(n) € Q(n2.log n) 10 Masters Theorem Case 3: T(n)=4 T(n/2)+ n3 a=4 and b=2 f(n)=n3 E=log a/ log b log 4/log 2 log(2)2/log 2 2log 2/log 2 2 if f(n) € (nE-e) for some e>o and f(n) € O(nE+δ) for some δ >=e then T(n) € Q (f(n)) f(n) € (nE-e) € (n2-1) € (n) f(n) € O(nE+δ) € O(n2+1) (δ>=e) € O(n3) then T(n) € Q (f(n)) € Q (n3 ) ie f(n) € (n) and f(n) € O(n3) then T(n)=€ Q (n3 ) 11 Recursion-tree method •A recursion tree models of the costs (time) of a recursive execution of an algorithm. •The recursion tree method is good for generating guesses for the substitution method. •The recursion-tree method can be unreliable, just like any method 12 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: L2.13 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: T(n) L2.14 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 T(n/4) T(n/2) L2.15 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/4)2 (n/2)2 T(n/16) T(n/8) T(n/8) T(n/4) L2.16 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 (n/4)2 (n/2)2 (n/16)2 (n/8)2 (n/8)2 (n/4)2 Q(1) L2.17 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 n2 (n/4)2 (n/2)2 (n/16)2 (n/8)2 (n/8)2 (n/4)2 Q(1) L2.18 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 n2 (n/4)2 (n/2)2 5 n2 16 (n/16)2 (n/8)2 (n/8)2 (n/4)2 Q(1) L2.19 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 n2 (n/4)2 (n/2)2 5 n2 16 (n/16)2 (n/8)2 (n/8)2 (n/4)2 25 n 2 256 … Q(1) L2.20 Example of recursion tree Solve T(n) = T(n/4) + T(n/2) + n2: n2 n2 (n/4)2 (n/2)2 5 n2 16 (n/16)2 (n/8)2 (n/8)2 (n/4)2 25 n 2 256 … Q(1) Total = n = Q(n2) 2 5 5 2 1 16 16 geometric series 5 3 16 Nondeterministic Algorithm algorithm with more than one choice the outcomes are not uniquely defined creating many possible computational paths These algorithms do not arrive at a solution for every possible computational path guaranteed to arrive at a correct solution for some path Due to this, one cannot determine the next state of the machine running the algorithm Example would be a random function. Nondeterministic Algorithm there are 3 function choice(s)- arbitrarily select one element from a set S failure()- signals an unsuccessful completion success()- signals a successful completion x=choice(1,n)- x will assign any one of the integer in the range [1-n] the failure() and success() signals are used to define a computation of the algo Deterministic algorithm Algo that runs on a particular input, it will always produce the same correct output always pass through the same sequence of states ( computational path) One simple model for deterministic algorithms is the mathematical function; it always produces the same output given a certain input