analysis recursive algorithm

36
1 Recursive Algorithm Analysis Dr. Ying Lu [email protected] RAIK 283: Data Structures & RAIK 283: Data Structures & Algorithms Algorithms September 13, 2012

Upload: lini-ickpnn

Post on 15-Sep-2015

266 views

Category:

Documents


0 download

DESCRIPTION

a

TRANSCRIPT

  • *Recursive Algorithm Analysis

    Dr. Ying [email protected]

    RAIK 283: Data Structures & AlgorithmsSeptember 13, 2012

  • *Giving credit where credit is due:Most of the lecture notes are based on the slides from the Textbooks companion websitehttp://www.aw-bc.com/info/levitin

    Several slides are from Hsu Wen Jing of the National University of Singapore

    I have modified them and added new slides

    RAIK 283: Data Structures & Algorithms

  • *Example: a recursive algorithm

    Algorithm:if n=0 then F(n) := 1else F(n) := F(n-1) * nreturn F(n)

  • *Algorithm F(n)// Compute the nth Fibonacci number recursively//Input: A nonnegative integer n//Output: the nth Fibonacci numberif n 1 return nelse return F(n-1) + F(n-2)Example: another recursive algorithm

  • *Recurrence RelationRecurrence Relation

  • *Recurrence RelationRecurrence Relation: an equation or inequality that describes a function in terms of its value on smaller inputs

  • *Example: a recursive algorithmAlgorithm:if n=0 then F(n) := 1else F(n) := F(n-1) * nreturn F(n)

    What does this algorithm compute?

  • *Example: a recursive algorithmAlgorithm:if n=0 then F(n) := 1else F(n) := F(n-1) * nreturn F(n)

    What does this algorithm compute?

    Whats the basic operation of this algorithm?

  • *Example: recursive evaluation of n !Recursive definition of n!:Algorithm:if n=0 then F(n) := 1else F(n) := F(n-1) * nreturn F(n)

    M(n): number of multiplications to compute n! with this recursive algorithm

  • *Example: recursive evaluation of n !Recursive definition of n!:Algorithm:if n=0 then F(n) := 1else F(n) := F(n-1) * nreturn F(n)

    M(n): number of multiplications to compute n! with this recursive algorithm

    Could we establish a recurrence relation for deriving M(n)?

  • *Example: recursive evaluation of n !Definition: n ! = 1*2**(n-1)*n Recursive definition of n!:Algorithm:if n=0 then F(n) := 1else F(n) := F(n-1) * nreturn F(n)

    M(n) = M(n-1) + 1Initial Condition: M(0) = ?

  • *Example: recursive evaluation of n !Recursive definition of n!:Algorithm:if n=0 then F(n) := 1else F(n) := F(n-1) * nreturn F(n)

    M(n) = M(n-1) + 1Initial condition: M(0) = 0Explicit formula for M(n) in terms of n only?

  • *Time efficiency of recursive algorithmsSteps in analysis of recursive algorithms: Decide on parameter n indicating input sizeIdentify algorithms basic operationDetermine worst, average, and best case for inputs of size n

    Set up a recurrence relation and initial condition(s) for C(n)-the number of times the basic operation will be executed for an input of size n

    Solve the recurrence to obtain a closed form or determine the order of growth of the solution (see Appendix B)

  • *EXAMPLE: tower of hanoiProblem:Given three pegs (A, B, C) and n disks of different sizes Initially, all the disks are on peg A in order of size, the largest on the bottom and the smallest on top The goal is to move all the disks to peg C using peg B as an auxiliaryOnly 1 disk can be moved at a time, and a larger disk cannot be placed on top of a smaller one

    ACBn disks

  • *EXAMPLE: tower of hanoiDesign a recursive algorithm to solve this problem:Given three pegs (A, B, C) and n disks of different sizes Initially, all the disks are on peg A in order of size, the largest on the bottom and the smallest on top The goal is to move all the disks to peg C using peg B as an auxiliaryOnly 1 disk can be moved at a time, and a larger disk cannot be placed on top of a smaller one

    ACBn disks

  • *EXAMPLE: tower of hanoiStep 1: Solve simple case when n
  • *EXAMPLE: tower of hanoiStep 2: Assume that a smaller instance can be solved, i.e. can move n-1 disks. Then?ACBACBACB

  • *EXAMPLE: tower of hanoi

    ACBACBACBACB

  • *EXAMPLE: tower of hanoi

    ACBACBACBACBTOWER(n, A, B, C)

  • *EXAMPLE: tower of hanoi

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

  • *EXAMPLE: 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

  • *EXAMPLE: tower of hanoiAlgorithm analysis: Input size? Basic operation?

  • *EXAMPLE: tower of hanoiAlgorithm analysis: Do we need to differentiate best case, worst case & average case for inputs of size n?

  • *EXAMPLE: tower of hanoiAlgorithm analysis: Set up a recurrence relation and initial condition(s) for C(n)

  • *EXAMPLE: tower of hanoiAlgorithm analysis: C(n) = 2C(n-1)+1

  • *In-Class ExerciseP. 76 Problem 2.4.1 (c): solve this recurrence relation: x(n) = x(n-1) + n for n>0, x(0)=0P. 77 Problem 2.4.4: consider the following recursive algorithm: Algorithm Q(n)// Input: A positive integer nIf n = 1 return 1else return Q(n-1) + 2 * n 1A. Set up a recurrence relation for this functions values and solve it to determine what this algorithm computesB. Set up a recurrence relation for the number of multiplications made by this algorithm and solve it. C. Set up a recurrence relation for the number of additions/subtractions made by this algorithm and solve it.

  • *Example: BinRec(n) Algorithm BinRec(n)//Input: A positive decimal integer n//Output: The number of binary digits in ns binary representationif n = 1 return 1else return BinRec( n/2 ) + 1

  • *Smoothness ruleIf T(n) (f(n)) for values of n that are powers of b, where b 2, then T(n) (f(n))

  • *Example: BinRec(n) Algorithm BinRec(n)//Input: A positive decimal integer n//Output: The number of binary digits in ns binary representationif n = 1 return 1else return BinRec( n/2 ) + 1If C(n) (f(n)) for values of n that are powers of b, where b 2, then C(n) (f(n))

  • *Fibonacci numbersThe Fibonacci sequence:0, 1, 1, 2, 3, 5, 8, 13, 21,

    Fibonacci recurrence:F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1

    2nd order linear homogeneous recurrence relation with constant coefficients

  • *Solving linear homogeneous recurrence relations with constant coefficientsEasy first: 1st order LHRRCCs:C(n) = a C(n -1) C(0) = t Solution: C(n) = t anExtrapolate to 2nd orderL(n) = a L(n-1) + b L(n-2) A solution?: L(n) = ?

    Characteristic equation (quadratic)Solve to obtain roots r1 and r2 (quadratic formula)General solution to RR: linear combination of r1n and r2nParticular solution: use initial conditions

  • *Solving linear homogeneous recurrence relations with constant coefficientsEasy first: 1st order LHRRCCs:C(n) = a C(n -1) C(0) = t Solution: C(n) = t anExtrapolate to 2nd orderL(n) = a L(n-1) + b L(n-2) A solution?: L(n) = ?

    Characteristic equation (quadratic)Solve to obtain roots r1 and r2 (quadratic formula)General solution to RR: linear combination of r1n and r2nParticular solution: use initial conditions

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

  • *1. Definition based recursive algorithm

    Computing Fibonacci numbersAlgorithm F(n)// Compute the nth Fibonacci number recursively//Input: A nonnegative integer n//Output: the nth Fibonacci numberif n 1 return nelse return F(n-1) + F(n-2)

  • *2. Nonrecursive brute-force algorithm

    Computing Fibonacci numbersAlgorithm Fib(n)// Compute the nth Fibonacci number iteratively//Input: A nonnegative integer n//Output: the nth Fibonacci numberF[0] 0; F[1] 1for i 2 to n doF[i] F[i-1] + F[i-2]return F[n]

  • *Computing Fibonacci numbers

    3. Explicit formula algorithm

  • *In-Class Exercises What is the explicit formula for A(n)?A(n) = 3A(n-1) 2A(n-2) A(0) = 1 A(1) = 3

    P.83 2.5.3. Climbing stairs: Find the number of different ways to climb an n-stair stair-case if each step is either one or two stairs. (For example, a 3-stair staircase can be climbed three ways: 1-1-1, 1-2, and 2-1.)

    ***Note the difference between the two recurrences. Students often confuse these!

    F(n) = F(n-1) nF(0) = 1

    ------------

    M(n) =M(n-1) + 1M(0) = 0(# recursive calls is the same)**In mathematics, a recurrence relation is an equation that recursively defines a sequence: each term of the sequence is defined as a function of the preceding terms.

    Note the difference between the two recurrences. Students often confuse these!

    F(n) = F(n-1) nF(0) = 1

    ------------

    M(n) =M(n-1) + 1M(0) = 0(# recursive calls is the same)*In mathematics, a recurrence relation is an equation that recursively defines a sequence: each term of the sequence is defined as a function of the preceding terms.

    Note the difference between the two recurrences. Students often confuse these!

    F(n) = F(n-1) nF(0) = 1

    ------------

    M(n) =M(n-1) + 1M(0) = 0(# recursive calls is the same)*In mathematics, a recurrence relation is an equation that recursively defines a sequence: each term of the sequence is defined as a function of the preceding terms.

    Note the difference between the two recurrences. Students often confuse these!

    F(n) = F(n-1) nF(0) = 1

    ------------

    M(n) =M(n-1) + 1M(0) = 0(# recursive calls is the same)*In mathematics, a recurrence relation is an equation that recursively defines a sequence: each term of the sequence is defined as a function of the preceding terms.

    Note the difference between the two recurrences. Students often confuse these!

    F(n) = F(n-1) nF(0) = 1

    ------------

    M(n) =M(n-1) + 1M(0) = 0(# recursive calls is the same)*In mathematics, a recurrence relation is an equation that recursively defines a sequence: each term of the sequence is defined as a function of the preceding terms.

    Note the difference between the two recurrences. Students often confuse these!

    F(n) = F(n-1) nF(0) = 1

    ------------

    M(n) =M(n-1) + 1M(0) = 0(# recursive calls is the same)*In mathematics, a recurrence relation is an equation that recursively defines a sequence: each term of the sequence is defined as a function of the preceding terms.

    Note the difference between the two recurrences. Students often confuse these!

    F(n) = F(n-1) nF(0) = 1

    ------------

    M(n) =M(n-1) + 1M(0) = 0(# recursive calls is the same)*Note the difference between the two recurrences. Students often confuse these!

    F(n) = F(n-1) nF(0) = 1

    ------------

    M(n) =M(n-1) + 1M(0) = 0(# recursive calls is the same)*Note the difference between the two recurrences. Students often confuse these!

    F(n) = F(n-1) nF(0) = 1

    ------------

    M(n) =M(n-1) + 1M(0) = 0(# recursive calls is the same)*******************http://en.wikipedia.org/wiki/Quadratic_equation

    r1=(-b+sqrt(b^2-4ac))/2a; r2=(-b-sqrt(b^2-4ac)/2a.*****