Analysis Re Cursive Algorithm

Embed Size (px)

Citation preview

  • 8/8/2019 Analysis Re Cursive Algorithm

    1/31

    1

    Recursive Algorithm Analysis

    Dr. Ying [email protected]

    CSCE 310: Data Structures & AlgorithmsCSCE 310: Data Structures & Algorithms

    September 8, 2010

  • 8/8/2019 Analysis Re Cursive Algorithm

    2/31

    2

    Giving credit where credit is due:Giving credit where credit is due:

    Most of the lecture notes are based on the slidesMost of the lecture notes are based on the slides

    from the Textbooks companion websitefrom the Textbooks companion website

    http://www.awhttp://www.aw--bc.com/info/levitinbc.com/info/levitin

    Several slides are from Hsu Wen JingSeveral slides are from Hsu Wen Jing of theof the

    National University ofS

    ingaporeNational University ofS

    ingapore

    I have modified them and added new slidesI have modified them and added new slides

    CSCE 310: Data Structures & AlgorithmsCSCE 310: Data Structures & Algorithms

  • 8/8/2019 Analysis Re Cursive Algorithm

    3/31

    3

    Example:Example: a recursive algorithma recursive algorithm

    Algorithm:Algorithm:

    ififnn=0 then=0 then FF((nn) := 1) := 1

    elseelse FF((nn) :=) :=FF((nn--1) *1) * nn

    returnreturn FF((nn))

  • 8/8/2019 Analysis Re Cursive Algorithm

    4/31

    4

    Algorithm F(n)

    // Compute the nth Fibonacci number recursively

    //Input: A nonnegative integer n

    //Output: the nth Fibonacci number

    ifn e 1return n

    else return F(n-1) + F(n-2)

    Example:Example: another recursive algorithmanother recursive algorithm

  • 8/8/2019 Analysis Re Cursive Algorithm

    5/31

    5

    Example: recursive evaluation ofExample: recursive evaluation of nn !!

    Definition:Definition: nn != 1*2!= 1*2**(n**(n--1)*1)*nn

    Recursive definition ofRecursive definition ofnn!:!:

    Algorithm:Algorithm:

    ififnn=0 then=0 then FF((nn) := 1) := 1

    elseelse FF((nn) :=) :=FF((nn--1) *1) * nn

    returnreturn FF((nn))

    M(n): number of multiplications to computeM(n): number of multiplications to compute nn!!

    Could we establish aCould we establish a recurrence relationrecurrence relation forforderiving M(n)?deriving M(n)?

  • 8/8/2019 Analysis Re Cursive Algorithm

    6/31

    6

    Example: recursive evaluation ofExample: recursive evaluation of nn !!

    M(n) = M(nM(n) = M(n--1) + 1 for n > 01) + 1 for n > 0

    AA recurrencerelationrecurrencerelation is an equation oris an equation or

    inequality that describes a function in termsinequality that describes a function in terms

    of its value on smaller inputsof its value on smaller inputs

    Explicit formula for the sequence (e.g. M(n))Explicit formula for the sequence (e.g. M(n))in terms of n onlyin terms of n only

  • 8/8/2019 Analysis Re Cursive Algorithm

    7/31

    7

    Example: recursive evaluation ofExample: recursive evaluation of nn !!

    Definition:Definition: nn != 1*2!= 1*2**(n**(n--1)*1)*nn

    Recursive definition ofRecursive definition ofnn!:!:

    Algorithm:Algorithm:

    ififnn=0 then=0 then FF((nn) := 1) := 1

    elseelse FF((nn) :=) :=FF((nn--1) *1) * nn

    returnreturn FF((nn))

    M(n) = M(nM(n) = M(n--1) + 11) + 1

    Initial Condition: M(0) = ?Initial Condition: M(0) = ?

  • 8/8/2019 Analysis Re Cursive Algorithm

    8/31

    8

    Example: recursive evaluation ofExample: recursive evaluation of nn !!

    Recursive definition ofRecursive definition ofnn!:!:

    Algorithm:Algorithm:

    ififnn=0 then=0 then FF((nn) := 1) := 1

    elseelse FF((nn) :=) :=FF((nn--1) *1) * nn

    returnreturn FF((nn))

    M(n) = M(nM(n) = M(n--1) + 11) + 1 Initial condition: M(0) = 0Initial condition: M(0) = 0

    Explicit formula for M(n)?Explicit formula for M(n)?

  • 8/8/2019 Analysis Re Cursive Algorithm

    9/31

    9

    Time efficiency of recursive algorithmsTime efficiency of recursive algorithms

    Steps in analysis of recursive algorithms:Steps in analysis of recursive algorithms: Decide on parameterDecide on parameter nn indicatingindicating inputsizeinputsize

    Identify algorithmsIdentify algorithms basicoperationbasicoperation

    DetermineDetermine worstworst,, averageaverage, and, and bestbestcase for inputs of sizecase for inputs of size nn

    Set up a recurrence relation and initial condition(s) forSet up a recurrence relation and initial condition(s) forCC((nn))--the number of times the basic operation will bethe number of times the basic operation will beexecuted for an input of sizeexecuted for an input of size nn

    Solve the recurrence to obtain a closed form orSolve the recurrence to obtain a closed form orestimate the order of magnitude of the solution (seeestimate the order of magnitude of the solution (seeAppendix B)Appendix B)

  • 8/8/2019 Analysis Re Cursive Algorithm

    10/31

    10

    EXAMPLE: tower of hanoiEXAMPLE: tower of hanoi

    Problem:Problem: Given three pegs (A, B, C) and n disks of different sizesGiven three pegs (A, B, C) and n disks of different sizes

    Initially, all the disks are on peg A in order of size, theInitially, all the disks are on peg A in order of size, thelargest on the bottom and the smallest on toplargest on the bottom and the smallest on top

    The goal is to move all the disks to peg C using peg B as anThe goal is to move all the disks to peg C using peg B as anauxiliaryauxiliary

    Only 1 disk can be moved at a time, and a larger disk cannotOnly 1 disk can be moved at a time, and a larger disk cannotbe placed on top of a smaller onebe placed on top of a smaller one

    A C

    B

    n disks

  • 8/8/2019 Analysis Re Cursive Algorithm

    11/31

    11

    EXAMPLE: tower of hanoiEXAMPLE: tower of hanoi

    Step 1: Solve simple case when n

  • 8/8/2019 Analysis Re Cursive Algorithm

    12/31

    12

    EXAMPLE: tower of hanoiEXAMPLE: tower of hanoi

    Step 2: Assume that a smaller instance can beStep 2: Assume that a smaller instance can besolved, i.e. can move nsolved, i.e. can move n--1 disks. Then?1 disks. Then?

    A C

    B

    A C

    B

    A C

    B

  • 8/8/2019 Analysis Re Cursive Algorithm

    13/31

    13

    EXAMPLE: tower of hanoiEXAMPLE: tower of hanoi

    A C

    BA C

    B

    A C

    B

    A C

    B

  • 8/8/2019 Analysis Re Cursive Algorithm

    14/31

    14

    EXAMPLE: tower of hanoiEXAMPLE: tower of hanoi

    A C

    BA C

    B

    A C

    B

    A C

    B

    TOWER(n, A, B, C)

  • 8/8/2019 Analysis Re Cursive Algorithm

    15/31

    15

    EXAMPLE: tower of hanoiEXAMPLE: tower of hanoi

    A C

    BA C

    B

    A C

    B

    A C

    B

    TOWER(n, A, B, C)

    TOWER(n-1, A, C, B)

    Move(A, C)

    TOWER(n-1, B, A, C)

  • 8/8/2019 Analysis Re Cursive Algorithm

    16/31

    16

    EXAMPLE: tower of hanoiEXAMPLE: tower of hanoi

    TOWER(n, A, B, C) {

    TOWER(n-1

    , A, C, B);Move(A, C);

    TOWER(n-1, B, A, C)

    }

    if n

  • 8/8/2019 Analysis Re Cursive Algorithm

    17/31

    17

    EXAMPLE: tower of hanoiEXAMPLE: tower of hanoi

    TOWER(n, A, B, C) {

    TOWER(n-1, A, C, B);

    Move(A, C);TOWER(n-1, B, A, C)

    }

    if n

  • 8/8/2019 Analysis Re Cursive Algorithm

    18/31

    18

    EXAMPLE: tower of hanoiEXAMPLE: tower of hanoi

    TOWER(n, A, B, C) {

    TOWER(n-1, A, C, B);

    Move(A, C);TOWER(n-1, B, A, C)

    }

    if n

  • 8/8/2019 Analysis Re Cursive Algorithm

    19/31

    19

    EXAMPLE: tower of hanoiEXAMPLE: tower of hanoi

    TOWER(n, A, B, C) {

    TOWER(n-1, A, C, B);

    Move(A, C);TOWER(n-1, B, A, C)

    }

    if n

  • 8/8/2019 Analysis Re Cursive Algorithm

    20/31

    20

    InIn--Class ExerciseClass Exercise

    P. 76 Problem 2.4.1 (c): solve this recurrence relation:P. 76 Problem 2.4.1 (c): solve this recurrence relation:

    x(n) = x(nx(n) = x(n--1) + n1) + n

    P. 76 Problem 2.4.4: consider the following recursiveP. 76 Problem 2.4.4: consider the following recursive

    algorithm:algorithm:

    Algorithm Q(n)Algorithm Q(n)// Input: A positive integer n// Input: A positive integer n

    If n = 1 return 1If n = 1 return 1

    else return Q(nelse return Q(n--1) + 2 * n1) + 2 * n 11

    A.S

    et up a recurrence relation for this functions values andA.S

    et up a recurrence relation for this functions values andsolve it to determine what this algorithm computessolve it to determine what this algorithm computes

    B. Set up a recurrence relation for the number ofB. Set up a recurrence relation for the number of

    multiplications made by this algorithm and solve it.multiplications made by this algorithm and solve it.

    C. Set up a recurrence relation for the number ofC. Set up a recurrence relation for the number of

    additions/subtractions made by this algorithm and solve it.additions/subtractions made by this algorithm and solve it.

  • 8/8/2019 Analysis Re Cursive Algorithm

    21/31

    21

    Example: BinRec(n)Example: BinRec(n)

    Algorithm BinRec(n)

    //Input: A positive decimal integer n

    //Output: The number of binary digits in ns binary representation

    ifn = 1return 1

    else return BinRec( -n/2 ) + 1

  • 8/8/2019 Analysis Re Cursive Algorithm

    22/31

    22

    Smoothness ruleSmoothness rule

    If T(nIf T(n)) 55(f(n))(f(n))

    for values of n that are powers of b, where bfor values of n that are powers of b, where b uu 2,2,

    thenthen

    T(n)T(n) 55(f(n))(f(n))

  • 8/8/2019 Analysis Re Cursive Algorithm

    23/31

    23

    Example: BinRec(n)Example: BinRec(n)

    Algorithm BinRec(n)

    //Input: A positive decimal integer n

    //Output: The number of binary digits in ns binary representation

    ifn = 1return 1

    else return BinRec( -n/2 ) + 1

  • 8/8/2019 Analysis Re Cursive Algorithm

    24/31

    24

    Fibonacci numbersFibonacci numbers

    The Fibonacci sequence:The Fibonacci sequence:

    0, 1, 1, 2, 3, 5, 8, 13, 21, 0, 1, 1, 2, 3, 5, 8, 13, 21,

    Fibonacci recurrence:Fibonacci recurrence:

    F(F(nn) = F() = F(nn--1) + F(1) + F(nn--2)2)

    F(0) = 0F(0) = 0

    F(1) = 1F(1) = 1

    2nd2nd orderlinearhomogeneousorderlinearhomogeneousrecurrencerelationrecurrencerelation

    with constantcoefficientswith constantcoefficients

  • 8/8/2019 Analysis Re Cursive Algorithm

    25/31

    25

    Solving linear homogeneous recurrenceSolving linear homogeneous recurrence

    relations with constant coefficientsrelations with constant coefficients

    Easy first: 1Easy first: 1stst order LHRRCCs:order LHRRCCs:CC((nn) =) =aCaC((nn --1)1) CC(0) =(0) =tt Solution: Solution: CC((nn) =) = tatann

    Extrapolate to 2Extrapolate to 2ndnd orderorder

    LL((nn) =) =aa LL((nn--1) +1) + bb LL((nn--2) A solution?:2) A solution?: LL((nn) =) =??

    Characteristic equation (quadratic)Characteristic equation (quadratic)

    Solve to obtain rootsSolve to obtain roots rr11 andand rr22 ((quadratic formulaquadratic formula))

    General solution to RR: linear combination ofGeneral solution to RR: linear combination ofrr11nn andand rr22

    nn

    Particular solution: use initial conditionsParticular solution: use initial conditions

  • 8/8/2019 Analysis Re Cursive Algorithm

    26/31

    26

    Solving linear homogeneous recurrenceSolving linear homogeneous recurrence

    relations with constant coefficientsrelations with constant coefficients

    Easy first: 1Easy first: 1stst order LHRRCCs:order LHRRCCs:CC((nn) =) =aCaC((nn --1)1) CC(0) =(0) =tt Solution: Solution: CC((nn) =) = tatann

    Extrapolate to 2Extrapolate to 2ndnd orderorder

    LL((nn) =) =aa LL((nn--1) +1) + bb LL((nn--2) A solution?:2) A solution?: LL((nn) =) =??

    Characteristic equation (quadratic)Characteristic equation (quadratic)

    Solve to obtain rootsSolve to obtain roots rr11 andand rr22 ((quadratic formulaquadratic formula))

    General solution to RR: linear combination ofGeneral solution to RR: linear combination ofrr11nn andand rr22

    nn

    Particular solution: use initial conditionsParticular solution: use initial conditions

    Explicit Formula forExplicit Formula for Fibonacci Number: F(n) = F(nFibonacci Number: F(n) = F(n--1) +F(n1) +F(n--2)2)

  • 8/8/2019 Analysis Re Cursive Algorithm

    27/31

    27

    1. Definition based recursive algorithm1. Definition based recursive algorithm

    Computing Fibonacci numbersComputing Fibonacci numbers

    Algorithm F(n)

    // Compute the nth Fibonacci number recursively

    //Input: A nonnegative integer n

    //Output: the nth Fibonacci number

    ifn e 1return n

    else return F(n-1) + F(n-2)

  • 8/8/2019 Analysis Re Cursive Algorithm

    28/31

    28

    2. Nonrecursive brute2. Nonrecursive brute--force algorithmforce algorithm

    Computing Fibonacci numbersComputing Fibonacci numbers

    Algorithm Fib(n)

    // Compute the nth Fibonacci number iteratively

    //Input: A nonnegative integer n

    //Output: the nth Fibonacci number

    F[0] n 0; F[1] n 1

    for i n 2 to n do

    F[i] n F[i-1] + F[i-2]

    return F[n]

  • 8/8/2019 Analysis Re Cursive Algorithm

    29/31

    29

    Computing Fibonacci numbersComputing Fibonacci numbers

    3. Explicit formula algorithm3. Explicit formula algorithm

    integernearestthetorounded)2

    51(

    5

    1)( nnF

    !

    Special care in its implementation:Special care in its implementation:

    Intermediate results are irrational numbersIntermediate results are irrational numbers

    Their approximations in the computer are accurate enoughTheir approximations in the computer are accurate enough

    Final roundFinal round--off yields a correct resultoff yields a correct result

  • 8/8/2019 Analysis Re Cursive Algorithm

    30/31

    30

    InIn--Class ExercisesClass Exercises

    Another example:Another example:A(A(nn) = 3A() = 3A(nn--1)1) 2A(2A(nn--2) A(0) = 1 A(1) = 32) A(0) = 1 A(1) = 3

    P.83 2.5.4.

    Climbing stairs: Find the number of

    P.83 2.5.4.

    Climbing stairs: Find the number ofdifferent ways to climb an ndifferent ways to climb an n--stair stairstair stair--case if eachcase if each

    step is either one or two stairs. (For example, a 3step is either one or two stairs. (For example, a 3--

    stair staircase can be climbed three ways: 1stair staircase can be climbed three ways: 1--11--1, 11, 1--

    2, and 22, and 2--1.)1.)

  • 8/8/2019 Analysis Re Cursive Algorithm

    31/31

    AnnouncementAnnouncement

    Reading List of this WeekReading List of this Week Chapter 3Chapter 3