recurrences hand out

Upload: rajendra-kumar-rayala

Post on 02-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Recurrences Hand Out

    1/2

    Chapter 4: Recurrences

    Recurrences

    Recurrence Trees

    Master Method

    Recurrences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Examples of Recurrences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3More Examples of Recurrences. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Recursion Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Recursion Tree Continued. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Master Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Master Method Continued . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

    1

    Recurrences

    A recurrence describes a function in terms of its values on smaller inputs.

    The general form of a recurrence is:

    T(n) = aT(s(n)) +f(n)

    where we assume T(1) (1). T(n) = aT(s(n)) +f(n) can be interpreted as:

    a= Number of subproblemss(n) = Size of the subproblemsf(n) = Time to divide into subproblems and combine results

    CS 5633 Analysis of Algorithms Chapter 4: Slide 2

    Examples of Recurrences

    Merge-Sort: T(n) = 2T(n/2) +c n2 subproblemsn/2 = subproblem size(n)time to merge results

    Insertion-Sort: T(n) = T(n 1) +c nI.e., first sort n 1 elts., then insert nth elt.1 subproblemn 1 = subproblem size(n)time to insert nth element

    CS 5633 Analysis of Algorithms Chapter 4: Slide 3

    More Examples of Recurrences

    Bit-Multiply: T(n) = 3T(n/2) +c n3 subproblems (3 multiplications)n/2 = subproblem size

    (n)time to add/sub. results Strassens Alg.: T(n) = 7T(n/2) +c n2

    Matrices are n n7 subproblemsn/2 n/2= subproblem size(n2) time to form submatrices and add/sub. results

    CS 5633 Analysis of Algorithms Chapter 4: Slide 4

    2

  • 8/11/2019 Recurrences Hand Out

    2/2

    Recursion Trees

    Recursion tree for T(n) = 2T(n/2) +n

    T(n)

    n

    Tn

    2

    T

    n2

    n

    n/2 n/2

    Tn4

    Tn4

    Tn4

    Tn4

    CS 5633 Analysis of Algorithms Chapter 4: Slide 5

    Recursion Tree Continued

    n

    n/2 n/2

    n/4 n/4 n/4 n/4

    ...

    log2n

    n

    n

    n

    ...

    Total: (n lg n)

    CS 5633 Analysis of Algorithms Chapter 4: Slide 6

    3

    Master Method

    For recurrences of the form:

    T(n) = aT(n/b) +f(n)

    T(n) (f(n))follows immediately. T(n) (nlogba) because the recursion tree has alogbn =nlogba leaves.

    Height = logbn.Branching factor = a.

    CS 5633 Analysis of Algorithms Chapter 4: Slide 7

    Master Method Continued

    For recurrences of the form:

    T(n) = aT(n/b) +f(n)

    T(n) (f(n))iff(n) is poly. larger than n logba (and is regular)

    T(n) (nlogba)iff(n) is poly. smaller than n logba

    T(n) (nlogba lg n)

    iff(n) (nlog

    ba

    )CS 5633 Analysis of Algorithms Chapter 4: Slide 8

    4