06-recursivealgorithms

Upload: thanh-hai

Post on 06-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 06-RecursiveAlgorithms

    1/8

    B.B. Karki, LSU0.1CSC 3102

    Recursive Algorithms

  • 8/2/2019 06-RecursiveAlgorithms

    2/8

    B.B. Karki, LSU0.2CSC 3102

    Definition and Examples

    Recursive algorithm invokes (makes reference to) itself repeatedly

    until a certain condition matches

    Examples:

    Computing factorial function

    Tower of Hanoi puzzle

    Digits in binary representation

    Non-recursive algorithm is executed only once to solve the problem.

  • 8/2/2019 06-RecursiveAlgorithms

    3/8

    B.B. Karki, LSU0.3CSC 3102

    Analyzing Efficiency of Recursive Algorithms

    Steps in mathematical analysis of recursive algorithms:

    Decide on parameter n indicating input size.

    Identify algorithms basic operation

    Determine worst, average, and bestcase for input of size n

    If the basic operation count also depends on other conditions.

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

    Alternatively count recursive calls.

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

  • 8/2/2019 06-RecursiveAlgorithms

    4/8

    B.B. Karki, LSU0.4CSC 3102

    Example 1: Recursive Evaluation ofn !

    Definition of factorial functionF(n)= n!: n ! = 1 2 (n-1) n

    0! = 1

    Recurrence relation for the number of multiplications:

    M(n) =M(n-1) + 1 forn > 0

    M(0) = 0 initial condition

    Solve the recurrence relation using the method of backward substitution:

    M(n) =M(n-1) + 1 =M(n-2) + 2 = M(n-3) +3 = .. =M(n-i) + i=.

    =M(n-n) + n

    M(n) = n

    Algorithm F(n) //Compute n!recursively

    //Input: A nonnegative integer n

    //Output: The value of n!if n= 0 return 1else return F(n-1) * n

  • 8/2/2019 06-RecursiveAlgorithms

    5/8

    B.B. Karki, LSU0.5CSC 3102

    Example 2: Tower of Hanoi Puzzle

    Given:n disks of different sizes andthree pegs. Initially all disks are on thefirst peg in order of size, the largestbeing on the bottom

    Problem: move all the disks to the thirdpeg, using the second one as an

    auxiliary. One can move only one disk at a time,

    and it is forbidden to place a larger diskon the top of a smaller one.

    Recursive solution: Three stepsinvolved are

    First move recursively n - 1 disks frompeg 1 to peg 2 (with peg 3 as auxiliary)

    Move the largest disk directly from peg1 to peg 3

    Move recursively n - 1 disks from peg 2and peg 3 (using peg 1 as auxiliary)

    Peg 1 Peg 2 Peg 3

    http://www.mazeworks.com/hanoi/

  • 8/2/2019 06-RecursiveAlgorithms

    6/8

    B.B. Karki, LSU0.6CSC 3102

    Tower of Hanoi: Recurrence Relation

    Recurrence for the number of moves

    M(n) =M(n-1) + 1 +M(n-1) forn > 1

    = 2M(n-1) + 1

    M(1) = 1 initial condition

    Solve the recurrence relation using themethod of backward substitution:

    M(n) = 2M(n-1) + 1 = 22M(n-2) + 22 - 1

    = 23M(n-3) + 23 - 1 == 2iM(n-i) +2i- 1 =.

    = 2n-1M(n - (n-1)) + 2n-1 - 1= 2n-1 + 2n-1 - 1

    M(n) = 2n - 1

    n

    n-1n-1

    n-2n-2 n-2n-2

    22 22

    11 11 11 11

    .. .. ..

    Tree of recursive calls made bythe recursive algorithm

  • 8/2/2019 06-RecursiveAlgorithms

    7/8

    B.B. Karki, LSU0.7CSC 3102

    Example 3: Binary Recursive

    Problem: Investigate a recursive version of binary algorithm, which findsthe number of binary digits in the binary representation of a positivedecimal integer n.

    Recursive relation for the number of additions made by the algorithm.

    A(n) =An/2 + 1 forn > 1A(1) = 0 initial condition

    For n = 1, no addition is made because no recursive call is executed.

    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

  • 8/2/2019 06-RecursiveAlgorithms

    8/8

    B.B. Karki, LSU0.8CSC 3102

    Binary Recursive: Solution

    Use backward substitution method to solve the problem only forn = 2k. Smoothness rule implies that the observed order of growth is valid for all values of n.

    A(2k) =A(2k-1) + 1

    = [A(2k-2) + 1] + 1 =A(2k-2) + 2

    = [A(2k-3) + 1] + 2 =A(2k-3) + 3

    =A(2k-i) + i

    =A(2k-k) + k

    = k A(20) = A(1) = 0

    = log2n n = 2k

    A(n) = log2 n (log n)Exact solution is A(n) = log2 n