Recurrence Equations - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Recurrence Equations

Description:

Lemma. Lemma : The number of leaves in the recursion tree for recurrence equation T(n) ... Lemma. The recursion tree has depth lg n/lg c. The zero-th row-sum is f(n) ... – PowerPoint PPT presentation

Number of Views:288
Avg rating:3.0/5.0
Slides: 33
Provided by: IRE62
Category:

less

Transcript and Presenter's Notes

Title: Recurrence Equations


1
Recurrence Equations
  • Irena Pevac
  • CS463
  • CCSU

2
Recurrence Equations Use
  • To measure cost of recursive subroutines
  • To define mathematical functions
  • Fibonacci
  • F(1) 1
  • F(2) 1
  • F(n) F(n-1) F(n-2)
  • Choose
  • C(n,0) 1 C(k, k) 1 n gt m gt 0
  • C(n,m) C(n-1,m) C(n-1,m-1)

3
Building Recurrence equations
  • Specify some way to measure the size of the
    problem
  • Call that size n.
  • The left side of the recurrence equation will be
    T(n)
  • To make up the right side
  • Estimate how much the various blocks of the
    procedure will cost as a function of n.
  • All constants can be 1 if we are satisfied with
    an answer within the constant factor.

4
Building Recurrence equations
  • For linear sequence of blocks
  • Add cost of each block
  • For an alternation of blocks (if statement)
  • Add maximum of the alternatives
  • If a block contains method call that is not
    recursive with the one we analyze
  • Express actual parameters as function of n
    ns(n)
  • Cost of subroutine Ts( ns(n) )
  • If a block has recursive calls
  • Express actual parameters as function of n
    nR(n)
  • Cost of subroutine T( nR(n) ) T is the
    same T on the left

5
Example
  • Sequential search
  • Page 131 Example 3.8

6
Recurrence Equations Types
  • Divide and Conquer
  • T(n)bT(n/c) f(n)
  • Chip and Conquer
  • T(n)T(n-c) f(n)
  • Chip and Be Conquered
  • T(n) b T(n-c) f(n)

7
Divide and Conquer T(n)bT(n/c) f(n)
  • There are b subproblems
  • Each subproblem has size that is fixed fraction
    of the size of the current problem such as n/c

8
Divide and Conquer ExamplesT(n)bT(n/c) f(n)
  • b1 c2 T(n) T(n/2)1 binary search
  • T(n) lg n
  • b2 c2 T(n) 2 T(n/2)n merge sort
  • T(n) n lg n
  • b3 c2 T(n) 3 T(n/2)n Sierpinski
    triangles
  • T(n) 3 nlg3-2n

9
Chip and Conquer ExamplesT(n)T(n-c)f(n)
  • c1
  • T(n) T(n-1)1 linear search , factorial
  • T(n) n
  • c1
  • T(n) T(n-1) n Selection Sort
  • T(n) n(n1)/2

10
Chip and Be Conquered T(n) b T(n-c) f(n)
  • b2 c1 f(n) 1
  • T(0) 0
  • T(n) 2 T(n -1) 1 Tower of Hanoi
  • T(n) 2n-1

11
Recursion Tree
  • Tool for analyzing the cost of recursive methods
    for which we have formed recurrence equations.
  • Each node has two fields
  • The size field
  • The nonrecursive cost field

12
Expansion of a node
  • T(n) 2 T(n/2) n

13
Equation
  • Size field of the root
  • Sum of nonrecursive costs of expanded nodes
  • Sum of size fields of incomplete nodes
  • The same holds for root of any subtree.

14
Recursion TreeT(n) 2 T(n/2) n
15
Row sums
  • Row at level 0
  • Row at level 1
  • Row at level 2
  • Row at level depth
  • n/2depth1
  • where depthlg n
  • Sum n
  • Sum n
  • Sum n
  • Sum n
  • ________
  • Total sum of all row sums equals
  • n lg n

16
Divide and Conquer
  • T(n)bT(n/c) f(n)
  • Size n decreases by c at each next level
  • Size of the problem becomes n/c/c/c/c
  • We are done when we reach the size of a base case
  • n/cdepth 1 n cdepth ln n ln
    cdepth
  • depth ln n/ ln c in ? (log n)
  • The branching factor for recursion tree is b
  • How many leaves has the tree ?
  • At depth d there are bd nodes
  • Number of leaves L bd

17
  • lg(L) lg
  • lg(L) (lgn/lg c) lg b (lg b / lg c) lg n
  • Critical Exponent E lg b / lg c
  • lg(L) (lg b / lg c) lg n lg n (lg b / lg c)

18
Lemma
  • Lemma The number of leaves in the recursion
    tree for recurrence equation T(n)b(T(n/c)f(n)
    is nE, where E is critical exponent.
  • If nonrecursive cost is 1 in the leaves the cost
    of the tree is at least nE.

19
Lemma
  • The recursion tree has depth lg n/lg c
  • The zero-th row-sum is f(n).
  • The D-th row summ is nE assuming base case cost
    1 (or ?(nE) in any case).
  • The T(n) solution is the sum of the nonrecursive
    costs of all nodes in the recusive tree, which is
    the sum of row-sums.

20
Special cases
  • Row-sums form a geometric series
  • Ratio r is not 1.
  • The sum is in ? of its largest term

21
Little Master Theorem
  • CASE 1 If the row sums form an increasing
    geometric series starting at the root of the
    tree then T(n) is in ? (nE). That is, the cost is
    proportional to the number of leaves in the
    recursion tree.
  • CASE 2 If the row sums remain about constant,
    T(n) is in ? ( f(n) log n ).
  • CASE 3 If row sums form a decreasing geometric
    series then T(n) is in ? (f(n) ), which means
    that cost is proportional to the cost of the root.

22
CASE 3 T(n) T(n/2) n/2
  • Row sums form a decreasing geometric series.
  • Row 0 n/2
  • Row 1 n/4
  • Row 2 n/8
  • Row k n/2k1
  • where n2k klgn
  • Sum of all row sums n (1/21/41/81/2lgn)
    ?(n)

23
CASE 2 T(n)2T(n/2)n
  • Row sums are constant and equal to n
  • T(n) ?(f(n) log n) ?(n log n)

24
CASE 3 T(n)3T(n/2)n
25
Example CASE 3
  • T(n)3T(n/2)n Sierpinski triangle
  • Row sums form an increasing geometric series
  • Level 0 sum n
  • Level 1 sum 3n/2
  • Level 2 sum 32n/22
  • Level 3 sum 33n/23
  • .
  • Level k sum 3kn/2k
  • Where n2k and klgn.

26
T(n) Sum all the row sums
Sum all the row sums 3 n lg3 2n ? (n
(lg3/lg2) )
27
Master Theorem
  • Recurrence equation
  • T(n)bT(n/c) f(n)
  • has solutions as follows, where E is critical
    exponent
  • E lg b / lg c

28
Master Theorem
  • If f(n) in O(n E-e) for some positive e, then
    T(n) is in ? (nE), which is proportional to the
    number of leaves in the recursion tree.
  • If f(n) in ? (nE), then T(n) in ?(f(n) log n),
    since all levels contribute equally.
  • If f(n) in ?(n Ee) for some positive e, and
  • if f(n) O(n Ed) for some ? gt e then
  • T(n) is in ? (nE?),
  • which is proportional to the nonrecursive
    cost at the root of the recursion tree.

29
Chip and ConquerChip and Be Conquered
  • T(n) b T(n-c) f(n)
  • For b2 f(n)1

30
T(n) 2 T(n-c) 1
  • We are finished after k steps when we reach
    base case
  • n k c 0 n kc kn/c
  • 1248 2k2k1-1 22n/c-1 ?(2n/c)
  • Even with the most favorable assumption when
    f(n)1 solution is exponential.

31
T(n) b T(n-c) f(n)
  • In general
  • Where the second sum uses h (n/c) - d.
  • So, h is 0 at leaves and increases toward the
    root. When sum is ?(1), T(n) is in ?(bn/c).
    This is exponential growth.

32
T(n) T(n-c) f(n)
  • Special case when branching factor b1
  • If f(n) is a polynomial nk, then T(n) is in
    ?(nk1).
  • If f(n) log n then T(n) is in ?(n log n).
Write a Comment
User Comments (0)
About PowerShow.com