Download presentation
Presentation is loading. Please wait.
1
Java Lecture Recursion 29th feb 2005
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); } Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
3
Properties Calls the same function with smaller i/p.
Divide and conquer strategy Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
4
Example Little tricky Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
5
Application of recursion
? Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
6
Towers of Hanoi Let’s try this Hanoi Rules Input Output
Solution for 1, 2, 3, 4, … n Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
7
Define the problem Graphical interface What is input ?
NO What is input ? -Number of discs! What is output? -Final state ? Y/N -Sequence of moves ? -How to represent ? Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
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 Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
9
Steps Problem definition Input representation Output representation
Algorithm Implementation Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
10
Design We know Let’s try the algorithm for n Recursive condition
Terminating condition Let’s try the algorithm for n Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
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 ? Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
12
Skeleton code Solve_hanoi(integer n, towers t1, t2, t3) {
Do something…. Do something… solve_hanoi(some small n, t1, t2, t3) } Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
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…. } Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
14
Subdivsion N-1 discs Nth disc Start(X) Middle(Z) End(Y)
Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
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 ? Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
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). Solve_hanoi(n-1, t2, t1, t3). } Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
17
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) } Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
18
Reference Play the game or see the solutions here
Recursion: History of the game : Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
19
Thank you Thank you Sunday, September 16, 2018Sunday, September 16, 2018 CFILT java
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.