0% found this document useful (0 votes)
30 views

Java Lect 24

The document discusses recursion and provides an example of the Towers of Hanoi problem. It explains how to solve the Towers of Hanoi problem recursively by breaking the problem down into smaller subproblems until a base case is reached. The document outlines the steps to define the problem, represent the input and output, develop the algorithm using recursion, and implement the algorithm in Java code. It also provides references for learning more about recursion and the history of the Towers of Hanoi problem.

Uploaded by

Chander Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Java Lect 24

The document discusses recursion and provides an example of the Towers of Hanoi problem. It explains how to solve the Towers of Hanoi problem recursively by breaking the problem down into smaller subproblems until a base case is reached. The document outlines the steps to define the problem, represent the input and output, develop the algorithm using recursion, and implement the algorithm in Java code. It also provides references for learning more about recursion and the history of the Towers of Hanoi problem.

Uploaded by

Chander Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Java Lecture

Recursion
29th feb 2005
[email protected]

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);
}
}
Thursday, July 9,
2015

CFILT

java

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

Thursday, July 9,
2015

CFILT

java

Example

Little tricky
Thursday, July 9,
2015

CFILT

java

Application of recursion

?
Thursday, July 9,
2015

CFILT

java

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

Thursday, July 9,
2015

CFILT

java

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 ?
Thursday, July 9,
2015

CFILT

java

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
Thursday, July 9,
2015

CFILT

java

Steps
Problem definition
Input representation
Output representation
Algorithm
Implementation

Thursday, July 9,
2015

CFILT

java

Design
We know
Recursive condition
Terminating condition

Lets try the algorithm for n

Thursday, July 9,
2015

CFILT

java

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 ?

Thursday, July 9,
2015

CFILT

java

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

Thursday, July 9,
2015

CFILT

java

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

}
Thursday, July 9,
2015

CFILT

java

Subdivsion

N-1
discs
Nth disc
Start(X)
Thursday, July 9,
2015

Middle(Z)
CFILT

End(Y)
java

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 ?
Thursday, July 9,
2015

CFILT

java

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).

}
Thursday, July 9,
2015

CFILT

java

Complete java function


Public static void solve_hanoi(int n, String t1, t2, t3)
{
if (n == 1) then
System.out.println(move the disc+n+ from +t1+ to + t3);
else
{
solve_hanoi(n-1, t1, t3, t2)
System.out.println(move the disc+n+ from +t1+ to + t3);
solve_hanoi(n-1, t2, t1, t3)
}
}

Thursday, July 9,
2015

CFILT

java

Reference
Play the game or see the solutions here
http://www.mazeworks.com/hanoi/

Recursion:
http://www.cs.ucla.edu/~klinger/dorene/math4.htm

History of the game :


http://www.lawrencehallofscience.org/Java/Tower/towerhi
story.html

Thursday, July 9,
2015

CFILT

java

Thursday, July 9,
2015

CFILT

java

You might also like