0% found this document useful (0 votes)
24 views19 pages

Java Lect 24

The document discusses recursion and provides an example of the Towers of Hanoi problem. It explains recursion as a problem-solving technique where a function calls itself repeatedly to solve smaller instances of the same problem. The Towers of Hanoi is then used to illustrate recursion, where the goal is to move a stack of discs from one tower to another using a third tower, by moving one disc at a time and not placing larger discs over smaller ones. The document walks through defining the problem, representing the input and output, developing the algorithm using a recursive solve_hanoi function, and providing a complete Java implementation.
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)
24 views19 pages

Java Lect 24

The document discusses recursion and provides an example of the Towers of Hanoi problem. It explains recursion as a problem-solving technique where a function calls itself repeatedly to solve smaller instances of the same problem. The Towers of Hanoi is then used to illustrate recursion, where the goal is to move a stack of discs from one tower to another using a third tower, by moving one disc at a time and not placing larger discs over smaller ones. The document walks through defining the problem, representing the input and output, developing the algorithm using a recursive solve_hanoi function, and providing a complete Java implementation.
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);
}
}
Saturday, January
16, 2016

CFILT

java

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

Saturday, January
16, 2016

CFILT

java

Example

Little tricky
Saturday, January
16, 2016

CFILT

java

Application of recursion

?
Saturday, January
16, 2016

CFILT

java

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

Saturday, January
16, 2016

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 ?
Saturday, January
16, 2016

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
Saturday, January
16, 2016

CFILT

java

Steps
Problem definition
Input representation
Output representation
Algorithm
Implementation

Saturday, January
16, 2016

CFILT

java

Design
We know
Recursive condition
Terminating condition

Lets try the algorithm for n

Saturday, January
16, 2016

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 ?

Saturday, January
16, 2016

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

Saturday, January
16, 2016

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

}
Saturday, January
16, 2016

CFILT

java

Subdivsion

N-1
discs
Nth disc
Start(X)
Saturday, January
16, 2016

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 ?
Saturday, January
16, 2016

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

}
Saturday, January
16, 2016

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

Saturday, January
16, 2016

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

Saturday, January
16, 2016

CFILT

java

Saturday, January
16, 2016

CFILT

java

You might also like