Ch04 Recurrences

Embed Size (px)

DESCRIPTION

Recurrences

Citation preview

  • Ch4 Recurrences*AlgorithmsCh 4: Recurrences

    Ming-Te Chi

    Ch4 Recurrences

  • RecurrencesWhen an algorithm contains a recursive call to itself, its running time can often described by a recurrence.A recurrence is an equation or inequality that describes a function in terms of its value of smaller inputs.Ch4 Recurrences*

    Ch4 Recurrences

  • Ch4 Recurrences*RecurrencesexamplesSummation

    Factorial

    Ch4 Recurrences

  • Ch4 Recurrences*RecurrencesexamplesSummation

    Factorial

    Ch4 Recurrences

  • Ch4 Recurrences*Termination conditions(boundary conditions)Summation

    Factorial

    Ch4 Recurrences

  • Ch4 Recurrences*RecurrencesCS oriented examplesFibonacci number

    Merge sort

    Ch4 Recurrences

  • Ch4 Recurrences*Influence of the boundary conditionsFibonacci number

    Merge sort

    Ch4 Recurrences

  • Ch4 Recurrences*Recurrences

    Substitution method Recursion-tree method Master method

    Ch4 Recurrences

  • Ch4 Recurrences*TechnicalitiesNeglect certain technical details when stating and solving recurrences.A good example of a detail that is often glossed over is the assumption of integer arguments to functions.Boundary conditions is ignored.Omit floors, ceilings.

    Ch4 Recurrences

  • The maximum-subarray problemCh4 Recurrences*

    Ch4 Recurrences

  • Ch4 Recurrences*

    Ch4 Recurrences

  • Ch4 Recurrences*

    Ch4 Recurrences

  • Matrix multiplicationCh4 Recurrences*

    Ch4 Recurrences

  • A simple divide-and-conquer algorithmCh4 Recurrences*

    Ch4 Recurrences

  • Ch4 Recurrences*

    Ch4 Recurrences

  • Strassens methodCh4 Recurrences*

    Ch4 Recurrences

  • Ch4 Recurrences*

    Ch4 Recurrences

  • Ch4 Recurrences*The substitution method: Mathematical inductionThe substitution method for solving recurrence has two steps: 1. Guess the form of the solution. 2. Use mathematical induction to find the constants and show that the solution works.Powerful but can be applied only in cases when it is easy to guess the form of solution.

    Ch4 Recurrences

  • Ch4 Recurrences*Can be used to establish either upper bound or lower bound on a recurrence.

    Ch4 Recurrences

  • Ch4 Recurrences*General principle of the Mathematical InductionTrue for the trivial case, problem size =1.If it is true for the case of problem at step k, we can show that it is true for the case of problem at step k+1.

    Ch4 Recurrences

  • Ch4 Recurrences*ExampleDetermine the upper bound on the recurrence

    (We may omit the initial condition later.) Guess

    Prove for some c > 0.

    Ch4 Recurrences

  • Ch4 Recurrences*Inductive hypothesis: assume this bound holds for

    The recurrence implies

    Ch4 Recurrences

  • Ch4 Recurrences*Initial condition

    However

    Ch4 Recurrences

  • Ch4 Recurrences*Making a good guess

    We guess

    Making guess provides loose upper bound and lower bound. Then improve the gap.

    Ch4 Recurrences

  • Ch4 Recurrences*

    Ch4 Recurrences

    Show that the solution to T(n) = 2T(( EQ

    EQ \F(n,2) ( + 17) + n is O(n lg n)

    Solution:

    assume a > 0, b > 0, c > 0 and T(n) an lg n blg n - c

    T(n) 2[( EQ

    EQ \F(n,2) + 17)lg( EQ

    EQ \F(n,2) +17) - blg( EQ

    EQ \F(n,2) + 17)-c]+n

    (an + 34a)lg( EQ

    EQ \F(n,2) +17) - 2blg( EQ \F(n,2) +17) - 2c + n

    anlg( EQ \F(n,2) +17) + anlg21/a + (34a-2b)lg( EQ \F(n,2) +17) - 2c

    anlg(n) 21/a+ (34a-2b)lg(n) - 2c

  • Ch4 Recurrences*

    Ch4 Recurrences

    anlg(n) 21/a+ (34a-2b)lg(n) - 2c

    (n EQ \F(n,2) +17n34

    (n ( EQ \F(n,2) +17) 21/a21/21.5n 12

    (34a-2b -bb 34a

    (c > 0-c > -2c

    ( T(n) anlgn - blgn - cT(n) anlgn

    ( T(n) = O(nlgn)

  • Ch4 Recurrences*SubtletiesGuess Assume

    However, assume

    Ch4 Recurrences

  • Ch4 Recurrences*Avoiding pitfalls Assume Hence

    (WRONG!) You cannot find such a c.

    Ch4 Recurrences

  • Ch4 Recurrences*Changing variables

    Ch4 Recurrences

    Let

    .

    Then

    .

    _925211299.unknown

    _925211400.unknown

    _925211449.unknown

    _925211331.unknown

    _925211262.unknown

  • Ch4 Recurrences*4.4 the Recursion-tree method

    Ch4 Recurrences

  • Ch4 Recurrences*

    Ch4 Recurrences

  • Ch4 Recurrences*Subproblem size for a node at depth i

    Total level of tree

    Number of nodes at depth i

    Cost of each node at depth i

    Total cost at depth i

    Last level, depth , has nodes

    Ch4 Recurrences

  • Ch4 Recurrences*Prove of

    We conclude,

    Ch4 Recurrences

  • Ch4 Recurrences*The cost of the entire tree

    Ch4 Recurrences

  • Ch4 Recurrences*

    Ch4 Recurrences

  • Ch4 Recurrences*Substitution methodWe want to Show that T(n) dn2 for some constant d > 0. Using the same constant c > 0 as before, we have

    Where the last step holds as long as d (16/13)c.

    Ch4 Recurrences

  • Ch4 Recurrences*

    Ch4 Recurrences

  • Ch4 Recurrences*Subproblem size for a node at depth i

    Total level of tree

    Ch4 Recurrences

  • Ch4 Recurrences*substitution methodAs long as d c/(lg3 (2/3)).

    Ch4 Recurrences

  • Ch4 Recurrences*4.5 Master methodMaster Theorem

    Ch4 Recurrences

  • Ch4 Recurrences*Remarks 1:In the first case, f(n) must be polynomailly smaller than

    That is, f(n) must be asymptotically smaller than by a factor of Similarly, in the third case, f(n) must be polynomailly larger than

    Also, the condition must be hold.

    Ch4 Recurrences

  • Ch4 Recurrences*Remarks 2:The three cases in the master theorem do not cover all the possibilities.There is a gap between cases 1 and 2 when f(n) is smaller than but not polynomailly smaller.

    Similarly, there is a gap between cases 2 and 3 when f(n) is larger than but not polynomailly larger (or the additional condition does not hold.)

    Ch4 Recurrences

  • Ch4 Recurrences*Example 1:

    Ch4 Recurrences

  • Ch4 Recurrences*Example 2:

    Ch4 Recurrences

  • Ch4 Recurrences*Example 3:

    Ch4 Recurrences

  • Ch4 Recurrences*Example 4:

    Ch4 Recurrences

  • Ch4 Recurrences*Example 5:

    Ch4 Recurrences

  • Ch4 Recurrences*Remarks 3:The master theory does not apply to the recurrence even though it has the proper form: a = 2, b=2, f(n)= n lgn, and It might seem that case 3 should apply, since f(n)= n lgn is asymptotically larger thanBut it is not polynomially larger.

    Ch4 Recurrences

  • Ch4 Recurrences*Remarks 3 (continue)However, the master method augments the conditions

    Case 2 can be applied.

    Ch4 Recurrences

  • Ch4 Recurrences*Example 6:

    Ch4 Recurrences

  • Ch4 Recurrences*Example 7:

    Ch4 Recurrences

  • Ch4 Recurrences*

    Ch4 Recurrences

  • Ch4 Recurrences*

    Ch4 Recurrences

    ** Partition problem: Index calculation vs copy entries*