Examples of Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Examples of Recursion

Description:

Towers of Hanoi. Three pegs, one with n disks of decreasing diameter; two ... Towers of Hanoi ... Towers of Hanoi. public static void THanoi (int n, int source, int extra, ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 12
Provided by: iit1
Category:

less

Transcript and Presenter's Notes

Title: Examples of Recursion


1
Examples of Recursion
  • Instructor Mainak Chaudhuri
  • mainakc_at_cse.iitk.ac.in

2
Sum of natural numbers
  • class SumOfNaturalNumbers
  • public static void main (String arg)
  • int m 3, n 10
  • if (m gt n)
  • System.out.println (Invalid input!)
  • else
  • System.out.println (Sum of numbers from
    m to n is Sum(m, n))

3
Sum of natural numbers
  • public static int Sum (int m, int n)
  • int x, y
  • if (mn) return m
  • x Sum(m, (mn)/2)
  • y Sum((mn)/21, n)
  • return (xy)
  • // end class

4
GCD revisited
  • Recall that gcd (a, b) gcd (a-b, b) assuming a
    gt b.
  • Directly defines a recurrence
  • public static int gcd (int a, int b)
  • if ((a1) (b1)) return 1
  • if (ab) return a
  • if (a lt b) return gcd (a, b-a)
  • if (a gt b) return gcd (a-b, b)

5
GCD revisited
  • Refinement gcd (a, b) gcd (a-nb, b) for any
    positive integer n such that a gt nb, in
    particular n a/b, assuming a gt b
  • public static int gcd (int a, int b)
  • if (0a) return b
  • if (0b) return a
  • if (a lt b) return gcd (a, ba)
  • if (a gt b) return gcd (ab, b)

6
Towers of Hanoi
  • Three pegs, one with n disks of decreasing
    diameter two other pegs are empty
  • Task move all disks to the third peg under the
    following constraints
  • Can move only the topmost disk from one peg to
    another in one step
  • Cannot place a smaller disk below a larger one
  • An example where recursion is much easier to
    formulate than a loop-based solution

7
Towers of Hanoi
  • We want to write a recursive method THanoi (n, 1,
    2, 3) which moves n disks from peg 1 to peg 3
    using peg 2 for intermediate transfers
  • The first step is to formulate the algorithm
  • Observation THanoi (n, 1, 2, 3) ? THanoi (n-1,
    1, 3, 2) followed by transferring the largest
    disk to peg 3 from peg 1 and calling THanoi (n-1,
    2, 1, 3)
  • Stopping condition n 1

8
Towers of Hanoi
  • class TowersOfHanoi
  • public static void main (String arg)
  • int n 10
  • THanoi(n, 1, 2, 3)

9
Towers of Hanoi
  • public static void THanoi (int n, int source,
    int extra, int destination)
  • if (n gt 1)
  • THanoi (n-1, source, destination,
    extra)
  • System.out.println (Move disk n
    from peg source to peg destination)
  • if (n gt 1)
  • THanoi (n-1, extra, source,
    destination)
  • // How many moves needed? 2n-1
  • // end class

10
Towers of Hanoi
  • Total number of method calls
  • Let Tn be the number of method calls to solve for
    n disks
  • Tn 2Tn-1 1 for n gt 1 T1 1
  • Use generating function to solve the recurrence
    (worked out in class on board)

11
Fibonacci series
  • Number of method calls
  • Refer to the program in the last lecture
  • Let Tn be the number of method calls to find the
    nth Fibonacci number
  • Tn Tn-1 Tn-2 1 for n gt 2 T1 T2 1
  • Use generating function to solve the recurrence
  • Observe that Tn 2Fn 1 where Fn is the nth
    Fibonacci number
  • Tn (21-n/v5)((1v5)n (1-v5)n) 1
  • The number (1v5)/2 is called the golden ratio,
    which is lim n?8 (Fn1/Fn)
Write a Comment
User Comments (0)
About PowerShow.com