Presentation is loading. Please wait.

Presentation is loading. Please wait.

2008. 2. 12 : 1 Recurrences. Contents of Table Recurrence solution methods The substitution method The recursion-tree method 2 Relationship between asymptotic.

Similar presentations


Presentation on theme: "2008. 2. 12 : 1 Recurrences. Contents of Table Recurrence solution methods The substitution method The recursion-tree method 2 Relationship between asymptotic."— Presentation transcript:

1 : 1 Recurrences

2 Contents of Table Recurrence solution methods The substitution method The recursion-tree method 2 Relationship between asymptotic notations A. Growth of function B. Recurrence

3 3 3. Relationship between this notations Analogy f(n) = Θ(g(n)) f(n) = g(n). f(n) = O(g(n)) f(n) g(n). f(n) = (g(n)) f(n) g(n). f(n) = o(g(n)) f(n) < g(n). f(n) = ω(g(n)) f(n) > g(n).

4 4 3. Relationship between this notations Transitivity Transpose symmetry f(n) = Θ(g(n)) and g(n) = Θ(h(n)) imply f(n) = Θ(h(n)), f(n) = O(g(n)) and g(n) = O(h(n)) imply f(n) = O(h(n)), f(n) = (g(n)) and g(n) = (h(n)) imply f(n) = (h(n)), f(n) = o(g(n)) and g(n) = o(h(n)) imply f(n) = o(h(n)), f(n) = ω(g(n)) and g(n) = ω(h(n)) imply f(n) = ω(h(n)). f(n) = O(g(n)) if and only if g(n) = (f(n)), f(n) = o(g(n)) if and only if g(n) = ω(f(n)).

5 5 3. Relationship between this notations Reflexivity f(n) = Θ(f(n)) f(n) = O(f(n)) f(n) = (f(n)) Symmetry f(n) = Θ(g(n)) if and only if g(n) = Θ(f(n)).

6 Recurrences 6 Recurrence When should we use the recurrence ? When algorithm contains a recursive call to itself, Its running time can often be described by a recurrence.

7 Recurrences 7 Recurrence - Definition Equation or inequality that describes a function in terms of its value on smaller inputs.

8 Recurrences 8 Recurrence Example Worst-case running time of T(n) could be described by the recurrence Merge-Sort procedure Show that the solution of T(n) = Θ(n log n) Merge-Sort procedure Show that the solution of T(n) = Θ(n log n) if n=1 if n>1

9 Recurrences 9 Recurrence Solution method to example 1) The substitution method guess a bound and prove our guess correct. 2) The recursion-tree method convert the recurrence into a tree whose nodes represent the costs incurred at various level of recursion 3) The master method determining asymptotic bounds for many simple recurrences of the form ( a 1, b 1, and f(n) is given function )

10 1. Technicalities for abbreviation Technicalities 1) assumption of integer The running time T(n) of algorithm is only defined when n is an integer 2) floors / ceilings We often omit floors and ceilings 3) boundary condition The running time of an algorithm on a constant-sized input is a constant that we typically ignore.

11 1. Technicalities for abbreviation Technicalities Example The recurrence describing the worst-case running time of Merge-Sort assumption of integer Omit boundary condition if n=1 if n>1 if n=1 if n>1 if n=1 if n>1 Omit floors and ceilings

12 2. Recurrence solution methods 12 Substitution Method - Definition - It can be used to establish either upper or lower bounds on a recurrence. 1. Guess the form of the solution 2. Use mathematical induction to find the constant and show that the solution works. 1. Guess the form of the solution 2. Use mathematical induction to find the constant and show that the solution works.

13 2. Recurrence solution methods 13 Substitution Method Example Let us determine an upper bound on the recurrence Let us determine an upper bound on the recurrence

14 2. Recurrence solution methods 14 Substitution Method Solution to example 1. guess that the solution is 2. prove that ( c > 0 ) 1. guess that the solution is 2. prove that ( c > 0 ) We need to find constant c and n 0 to meet the requirement about assumption T(n) cn lg n ( c>0 ) We using the Mathematical induction to show that our assumption holds for the boundary condition.

15 2. Recurrence solution methods 15 Substitution Method Solution to example Mathematical induction We can prove something stronger for a given value by assuming something stronger for smaller values. k means n/2 k+1 means n T(1), T(2), T(3), … ….. T( n/2 ), T(n) kk+1

16 2. Recurrence solution methods 16 Substitution Method Solution to example Recurrence : Guess : T(n)= O(n) Prove : T(n) cn k means k+1 means n -> Substitution T ( n/2 ) c n/2 lg ( n/2 ) T(n) 2(c n/2 lg( n/2 )) + n cn lg(n/2) + n ( because, n/2 < n/2 ) = cn lg n - cn lg 2 + n = cn lg n -(c+1)n ( Its our assumed fact )

17 2. Recurrence solution methods 17 Substitution Method Solution to example Assumed Fact : cn lg n –(c+1)n Prove : T(n) cn T(n) cn lg n – (c+1)n cn lg n (as long as c 1 ) Assumed Fact Prove

18 2. Recurrence solution methods 18 Substitution Method Solution to example - Find the constant n 0 - we can take advantage of asymptotic notation, we can replace base case T(1) cn lg n T(1) = 1 but, c1 lg1 = 0 - So, we only have to prove T (n) = cn lg n for n n 0 for n (n 0 =2) Replace T(1) by T(2) and T(3) as the base cases by letting n 0 =2.

19 2. Recurrence solution methods 19 Substitution Method Solution to example - Find the constant c - choosing c large enough T (2) = 4 and T (3) = 5. T (2) = 4 c ( 2 lg 2 ) T (3) = 5 c ( 3 lg 3 ). Any choice of c 2 suffices for base case of n=2 and n =3

20 2. Recurrence solution methods 20 Substitution Method Skill - Subtleties We can prove something stronger for given value by assuming something stronger for a smaller values. Overcome difficulty by subtracting a lower-order term from our previous guess

21 2. Recurrence solution methods 21 Substitution Method Skill - Subtleties example Recurrence : Guess : T(n)= O(n) Prove : T(n) cn Substitution k means k+1 means n

22 So, we now prove T(n) cn-b 2. Recurrence solution methods 22 Substitution Method Skill - Subtleties example ( Its Impossible. Then T(n)= O(n) is wrong? ) Subtracting a lower-order term from our previous guess T(n) cn

23 2. Recurrence solution methods 23 Substitution Method Skill - Subtleties example ( b1, c must be chosen large enough ) T(n) Recurrence : Guess : T(n)= O(n) Prove : T(n) cn-b

24 2. Recurrence solution methods 24 Substitution Method Skill - Avoid pitfalls We need to prove the exact form of the inductive hypothesis T(n) cn T(n) (c+1)n

25 2. Recurrence solution methods 25 Substitution Method Skill - Avoid pitfalls example Recurrence : Guess : T(n)= O(n) Prove : T(n) cn ( Wrong !!! exact form :T(n) cn )

26 2. Recurrence solution methods 26 Substitution Method Skill - Simplify this recurrence by changing variable Renaming m = lg n S(m) = T(2 m ) This new recurrence has same solution : S(m) = O(m log m)

27 2. Recurrence solution methods 27 Substitution Method Skill - Simplify this recurrence by changing variable This new recurrence has same solution : S(m) = O(m log m) Changing back from S(m) to T(n) T(n) = T(2 m ) = S(m) = O(m lg m) = O( lg n lg(lg n) )

28 2. Recurrence solution methods 28 Substitution Method Skill - Making a good guess prove loose upper and lower bounds on the recurrence and then reduce the range of uncertainty. - Guessing a solution takes : experience, occasionally, creativity We can make a good guess by using recursion tree !!

29 2. Recurrence solution methods Recursion-tree - Definition For come up with good guess, convert the recurrence into a tree that each node represents the cost of a single subproblem somewhere in the set of recursive function invocations. We sum all the per-level costs to determine the total cost of all levels of the recursion 29

30 2. Recurrence solution methods Recursion-tree Example 30 The recurrence is T (n) = 3T ( n/4 ) + Θ(n 2 ) = 3T (n/4) + cn 2 Use the recursion tree to making a good guess for upper bound for the solution. The recurrence is T (n) = 3T ( n/4 ) + Θ(n 2 ) = 3T (n/4) + cn 2 Use the recursion tree to making a good guess for upper bound for the solution.

31 2. Recurrence solution methods Recursion-tree Solution to example Draw recursion tree 31 cn 2 T(n)T(n) 3T (n/4) + cn 2

32 2. Recurrence solution methods Recursion-tree Solution to example 32 cn 2 c(n/4) 2 T(1) … log 4 n When depth i then subprogram size is n/4 i = 1 so, i = log 4 n and it means trees height and tree has log 4 n + 1 levels. The number of nodes at depth i is 3 i and finally When depth i then subprogram size is n/4 i = 1 so, i = log 4 n and it means trees height and tree has log 4 n + 1 levels. The number of nodes at depth i is 3 i and finally

33 2. Recurrence solution methods 33 Recursion-tree Solution to example Total Cost Sum of cost for root to i-1 depth Sum of cost for i depth

34 2. Recurrence solution methods 34 Recursion-tree Solution to example Total Cost is… ( When 0 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/238882/1/slides/slide_33.jpg", "name": "2.", "description": "Recurrence solution methods 34 Recursion-tree Solution to example Total Cost is… ( When 0

35 2. Recurrence solution methods 35 Recursion-tree Solution to example Prove T (n) = O(n 2 ) by the substitution method Recurrence : Guess : T (n) = O(n 2 ) Prove : T (n) dn 2 (for some d > 0 and for the same c > 0) T (n) = 3T ( n/4 ) + Θ(n 2 ) = 3T( n/4 ) + cn 2

36 2. Recurrence solution methods 36 Recursion-tree Solution to example Find constant d T(n) 3T( n/4 ) + cn 2 3d n/4 2 + cn 2 3d(n/4) 2 + cn 2 = 3/16 dn 2 + cn 2 dn 2 (where the last step holds as long as d (16/13)c ) T (n) = 3T( n/4 ) + cn 2

37 2. Recurrence solution methods Recursion-tree Example 37 The recurrence T (n) = 3T (n/3)+T(2n/3)+O(n) Use the recursion tree to making a good guess for upper bound for the solution. The recurrence T (n) = 3T (n/3)+T(2n/3)+O(n) Use the recursion tree to making a good guess for upper bound for the solution.

38 2. Recurrence solution methods Recursion-tree Solution to example Draw recursion tree 38 cn log 3/2 n cn Total : O(n log n) T (n) = 3T (n/3)+T(2n/3)+ cn This recursion tree is not a complete binary tree. Not all leaves contribute a cost of exactly cn

39 2. Recurrence solution methods Recursion-tree Solution to example Draw recursion tree 39 The total cost = Cost of each level x Height = cn x log 3/2 n = O(cnlog 3/2 n) = O(n lg n) The longest path from root to a leaf is n(2/3)n (2/3) 2 n … 1. Since, (2/3) k n = 1 when k=log 3/2 n and it means trees height is log 3/2 n. The longest path from root to a leaf is n(2/3)n (2/3) 2 n … 1. Since, (2/3) k n = 1 when k=log 3/2 n and it means trees height is log 3/2 n. T (n) = 3T (n/3)+T(2n/3)+ cn cn log 3/2 n cn

40 2. Recurrence solution methods Recursion-tree Solution to example Prove T (n) = O(n lg n) by the substitution method 40 Recurrence : Guess : T (n) = O(n lg n) Prove : T (n) d n lg n (for some d > 0 and for the same c > 0) T(n/3) + T(2n/3) + cn

41 2. Recurrence solution methods 41 Recursion-tree Solution to example Find constant d T(n) T(n/3) + T(2n/3) + cn d(n/3)lg(n/3) + d(2n/3)lg(2n/3) + cn = (d(n/3)lgn - d(n/3)lg 3) + (d(2n/3)lgn – d(2n/3)lg(3/2)) + cn = dnlgn - d((n/3)lg3 + (2n/3)lg(3/2)) + cn = dnlgn - d((n/3)lg3 + (2n/3)lg3 - (2n/3)lg2) + cn = dnlgn - dn(lg3 - 2/3) + cn dnlgn (as long as d c/(lg 3 - (2/3)) ) T(n)=T(n/3) + T(2n/3) + cn

42 Content of next week Recurrence solution methods The substitution method The recursion-tree method The master method 42 Ch4. Recurrence


Download ppt "2008. 2. 12 : 1 Recurrences. Contents of Table Recurrence solution methods The substitution method The recursion-tree method 2 Relationship between asymptotic."

Similar presentations


Ads by Google