Java Lecture Recursion 29th feb 2005 - PowerPoint PPT Presentation

About This Presentation
Title:

Java Lecture Recursion 29th feb 2005

Description:

... of recursion. 9/27/09. CFILT java. Towers of Hanoi. Let's ... History of the game : http://www.lawrencehallofscience.org/Java/Tower/towerhistory.html ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 20
Provided by: CFI9
Category:

less

Transcript and Presenter's Notes

Title: Java Lecture Recursion 29th feb 2005


1
Java LectureRecursion29th feb 2005
  • AshishFA_at_cse.iitb.ac.in

2
Recursion
  • solve(problem)
  • If problem is simple
  • Return the answer
  • Else
  • Break the problem as p1,p2,p3 pn
  • For each problem P
  • solve (P)

3
Properties
  • Calls the same function with smaller i/p.
  • Divide and conquer strategy

4
Example
  • Little tricky

5
Application of recursion
  • ?

6
Towers of Hanoi
  • Lets try this
  • Hanoi
  • Rules
  • Input
  • Output
  • Solution for 1, 2, 3, 4, n

7
Define the problem
  • Graphical interface
  • NO
  • What is input ?
  • -Number of discs!
  • What is output?
  • -Final state ?
  • Y/N
  • -Sequence of moves ?
  • -How to represent ?

8
Analogy with recursion
  • Solution for 4 discs
  • Can the solution for 4 be broken?
  • Broken problem must be small.
  • It should be same as the main problem.
  • When to stop ?
  • Solution for x discs is very simple. Solve it
    directly

9
Steps
  • Problem definition
  • Input representation
  • Output representation
  • Algorithm
  • Implementation

10
Design
  • We know
  • Recursive condition
  • Terminating condition
  • Lets try the algorithm for n

11
Input/Output
  • Name the function solve_hanoi
  • Input argument (integer n)
  • Output
  • Assume 3 towers three names..
  • 3 variables t1 ,t2, t3
  • t1 left
  • t2 middle
  • t3 right
  • Are the t1, t2, t3 also arguments ?

12
Skeleton code
  • Solve_hanoi(integer n, towers t1, t2, t3)
  • Do something.
  • Do something
  • solve_hanoi(some small n, t1, t2, t3)
  • Do something.
  • Do something

13
Temination
  • Solve_hanoi(integer n, towers t1, t2, t3)
  • If (n 1) then
  • move the discn from t1 to t3
  • Do something
  • solve_hanoi(some small n)
  • Do something.
  • Do something

14
Subdivsion
N-1 discs
Nth disc
Start(X)
End(Y)
Middle(Z)
15
Contd.
  • Solve_hanoi(integer n, towers t1, t2, t3)
  • 1. Move the n-1 discs from t1 to t2 using t3
  • 2. Now move only remaining disc from t1 to t3
  • 3. Move the n-1 discs from t2 to t3 using t1
  • Which steps are recursive ?

16
Formulate recursive call
  • Solve_hanoi(integer n, towers t1, t2, t3)
  • If (n 1) then
  • move the discn from t1 to t3.
  • Else
  • Solve_hanoi(n-1, t1, t3, t2).
  • move the discn from t1 to t3.
  • Solve_hanoi(n-1, t2, t1, t3).

17
Complete java function
  • Public static void solve_hanoi(int n, String t1,
    t2, t3)
  • if (n 1) then
  • System.out.println(move the discn from
    t1 to t3)
  • else
  • solve_hanoi(n-1, t1, t3, t2)
  • System.out.println(move the discn from
    t1 to t3) solve_hanoi(n-1, t2, t1, t3)

18
Reference
  • Play the game or see the solutions here
  • http//www.mazeworks.com/hanoi/
  • Recursionhttp//www.cs.ucla.edu/klinger/dorene/m
    ath4.htm
  • History of the game http//www.lawrencehallofsci
    ence.org/Java/Tower/towerhistory.html

19
Thank you
Thank you
Write a Comment
User Comments (0)
About PowerShow.com