FIT1002 2006 1 FIT1002 Computer Programming Unit 20 Recursion.

Slides:



Advertisements
Similar presentations
Starting Out with Java: From Control Structures through Objects
Advertisements

Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 03 / 28 / 2008 Instructor: Michael Eckmann.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
Chapter 10: Recursion CS 201 Program Design with C Department of CS, Montana State University Mahmud Shahriar Hossain.
1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Recursion Apan Qasem Texas State University Spring 2011.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Recursion.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
Data Structures Recursion Phil Tayco Slide version 1.0 Mar. 8, 2015.
Recursion CMPE231, Spring 2012 Aleaxander G. Chefranov 1.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 8 Recursion Modified.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursive. Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
Recursion.
CS212: Data Structures and Algorithms
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Recursion.
Chapter 15 Recursion.
Chapter 15 Recursion.
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
CS1120: Recursion.
CSC 143 Recursion.
Chapter 19: Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Presentation transcript:

FIT FIT1002 Computer Programming Unit 20 Recursion

FIT Objectives By the end of this lecture, students should: understand recursive method calls understand the basic principles of solving problems with a recursive approach be able to write simple recursive programs

FIT What is Recursion? A definition in terms of itself: “Your parents are your ancestors and the parents of your ancestors are also your ancestors” To avoid infinite recursion we need a base case. We can define (problem solving) methods recursively: – Base case – Recursive definition – Convergence to base case

FIT Example: Factorial n! = n  (n - 1)  (n - 2) ...  2  1 0! = 1 Example: 5! = 5  4  3  2  1 = 120 Given n  0:

FIT Example: Factorial Recursion : n! = n  (n - 1)  (n - 2) ...  2  1 (n - 1)! If n > 1: Factorial(n) = n  Factorial(n - 1)

FIT int factorial ( int n ) { if ( n is less than or equal to 1 ) then return 1 else return n  factorial ( n - 1 ) } Example: Factorial

FIT function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial Base case

FIT function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial General Case

FIT function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial Recursion

FIT function Factorial ( n ) { if ( n is less than or equal to 1 ) then return 1 else return n  Factorial ( n - 1 ) } Example: Factorial Convergence

FIT Unary Recursion Functions calls itself once (at most) Usual format: function RecursiveFunction ( ) { if ( ) then return else return RecursiveFunction ( ) } Winding and unwinding the “stack frames”

FIT Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  Factorial(1)2  1

FIT Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  Factorial(1)2  1

FIT Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  12 

FIT Example: Factorial Factorial(4) Factorial(3)4  Factorial(2)3  12 

FIT Example: Factorial Factorial(4) Factorial(3)4  23 

FIT Example: Factorial Factorial(4) Factorial(3)4  23 

FIT Example: Factorial Factorial(4) 64 

FIT Example: Factorial Factorial(4) 64 

FIT Example: Factorial 24

FIT /* Compute the factorial of n */ int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial(n-1); } Computes the factorial of a number function Factorial ( n ) { if ( n is less than or equal to 1) then return 1 else return n  Factorial ( n - 1 ) } Example: factorial.java

FIT Example: “Frames” during calculation of factorial(4) int x = factorial(4));

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1 ); } Example: “Frames” during calculation of factorial(4) n:

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) n:

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1); } n:

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } n:

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } n: 4 34 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n - 1); } n:

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } n: 4 34 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } n:

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } n: 4 34 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n -1); } n: Base case: factorial(1) is 1

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 n: 4 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 2 n: factorial(1) is 1

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 n: 4 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * } 2 n: 2 21 factorial(1) is 1

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 n: 4 int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n } 2 n: 2 2

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } 3 n: factorial(2) is 2

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * } 3 n: 23 3 factorial(2) is 2

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 4 n: int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n } 3 n: 6 3

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n ); } Example: “Frames” during calculation of factorial(4) 4 n: factorial(3) is 6

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n * } Example: “Frames” during calculation of factorial(4) 4 n: 64 4 factorial(3) is 6

FIT int x = factorial(4)); int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n } Example: “Frames” during calculation of factorial(4) 4 n: 24 4

FIT int x = factorial(4)); Example: “Frames” during calculation of factorial(4) 24 factorial(4) is 24

FIT N -ary Recursion Sometimes a function can only be defined in terms of two or more calls to itself. Efficiency is often a problem.

FIT Example: Fibonacci A series of numbers which begins with 0 and 1 every subsequent number is the sum of the previous two numbers 0, 1, 1, 2, 3, 5, 8, 13, 21,... Write a recursive function which computes the n -th number in the series ( n = 0, 1, 2,...)

FIT Example: Fibonacci The Fibonacci series can be defined recursively as follows: Fibonacci(0) = 0 Fibonacci(1) = 1 Fibonacci(n) = Fibonacci(n - 2) + Fibonacci(n - 1)

FIT /* Compute the n-th Fibonacci number, when=0,1,2,... */ long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } function Fibonacci ( n ) { if ( n is less than or equal to 1 ) then return n else return Fibonacci ( n - 2 ) + Fibonacci ( n - 1 ) } Fibonacci

FIT Example: Computation of fib(4) + fib(2)fib(3) fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) fib(2)fib(3) + fib(4) + fib(0)fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) fib(2)fib(3) + fib(4) + fib(0)fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) fib(2)fib(3) + fib(4) + 0fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) fib(2)fib(3) + fib(4) + fib(1)0 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) fib(2)fib(3) + fib(4) + fib(1)0 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) fib(2)fib(3) + fib(4) + 10 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) fib(2)fib(3) + fib(4) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) 1fib(3) + fib(4) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(1)fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(1)fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(2) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(2) + fib(0)fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(2) + fib(0)fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(2) + 0 fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(2) + 0 fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(2) + 0fib(1) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(2) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) fib(2) + 01 long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + fib(3) fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + 2 fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) + 2 fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); }

FIT Example: Computation of fib(4) Thus, fib(4) returns the value 3.

FIT Towers of Hanoi A classic problem Three pegs N discs, arranged bottom to top by decreasing size Objective: Move the discs from peg 1 to peg 3 Two constraints: One disk is moved at a time No larger disc can be placed above a smaller disk Write a program which will print the precise sequence of peg-to- peg disc transfers.

FIT Towers of Hanoi Base case: N = 1 Peg 1Peg 2Peg 3 Peg 1  Peg 3

FIT Towers of Hanoi Base case: N = 1 Peg 1Peg 2Peg 3 Peg 1  Peg 3

FIT Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 1  Peg 2

FIT Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 1  Peg 2

FIT Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Largest disc: Peg 1  Peg 3

FIT Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Largest disc: Peg 1  Peg 3

FIT Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 2  Peg 3

FIT Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Top N-1 discs: Peg 2  Peg 3

FIT Towers of Hanoi Recursion: N > 1 Peg 1Peg 2Peg 3 Acted as temporary holding peg.

FIT Towers of Hanoi Q: But how do you move those N -1 discs from Peg 1 to the temporary holding peg? A: Recursively, of course! E.g., “move N -1 discs from Peg 1 to Peg 2 using Peg 3 as temporarily holding area,” and so on. Denote: fromPeg, toPeg Temporary holding peg: otherPeg

FIT Towers of Hanoi procedure ToH ( numDiscs, fromPeg, toPeg ) { if ( numDiscs is 1 ) then output fromPeg, “ -> ”, toPeg else { otherPeg = /* determine otherPeg */ ToH ( numDiscs - 1, fromPeg, otherPeg ) output fromPeg, “ -> ”, toPeg ToH ( numDiscs - 1, otherPeg, toPeg ) } Base case

FIT Towers of Hanoi procedure ToH ( numDiscs, fromPeg, toPeg ) { if ( numDiscs is 1 ) then output fromPeg, “->”, toPeg else { otherPeg = /* determine temporary holding peg here */ ToH ( numDiscs - 1, fromPeg, otherPeg ) output fromPeg, “->”, toPeg ToH ( numDiscs - 1, otherPeg, toPeg ) } Recursion

FIT Towers of Hanoi procedure ToH ( numDiscs, fromPeg, toPeg ) { if ( numDiscs is 1 ) then output fromPeg, “ -> ”, toPeg else { otherPeg = theOtherPeg(fromPeg, toPeg) ToH(numDiscs - 1, fromPeg, otherPeg) output fromPeg, “ -> ”, toPeg ToH(numDiscs - 1, otherPeg, toPeg) }

FIT Towers of Hanoi function theOtherPeg ( pegA, pegB ) { if ( pegA is 1 ) then { if ( pegB is 2 ) then return 3 else return 2 } else if ( pegA is 2 ) then { if ( pegB is 1 ) then return 3 else return 1; } else if ( pegA is 3 ) then { if ( pegB is 2 ) then return 1 else return 2; } Solution 1

FIT Towers of Hanoi otherPeg is always 6 - (fromPeg + toPeg)

FIT Towers of Hanoi function theOtherPeg (fromPeg, toPeg ) { return ( 6 - fromPeg - toPeg ) } Solution 2

FIT Towers of Hanoi procedure ToH ( numDiscs, fromPeg, toPeg ) { if ( numDiscs is 1 ) then output fromPeg, “ -> ”, toPeg else { otherPeg = theOtherPeg(fromPeg, toPeg) ToH(numDiscs - 1, fromPeg, otherPeg) output fromPeg, “ -> ”, toPeg ToH(numDiscs - 1, otherPeg, toPeg) }

FIT Towers of Hanoi procedure ToH ( numDiscs, fromPeg, toPeg ) { if ( numDiscs is 1 ) then output fromPeg, “ -> ”, toPeg else { otherPeg = theOtherPeg(fromPeg, toPeg) ToH(numDiscs - 1, fromPeg, otherPeg) output fromPeg, “ -> ”, toPeg ToH(numDiscs - 1, otherPeg, toPeg) } Convergence

FIT hanoi.java /* Given two pegs, this function determines the other peg. */ int theOtherPeg ( int fromPeg, int toPeg ) { return (6 - fromPeg - toPeg); } /* This functions prints out the precise sequence of peg-to-peg disc transfers needed to solve the Towers of Hanoi problem. */ void ToH ( int numDiscs, int fromPeg, int toPeg ) { int otherPeg; if ( numDiscs == 1 ) System.out.println(“move from “+fromPeg+“ to “ + toPeg); else { otherPeg = theOtherPeg(fromPeg, toPeg); ToH(numDiscs - 1, fromPeg, otherPeg); printf("%d -> %d\n", fromPeg, toPeg); ToH(numDiscs - 1, otherPeg, toPeg); }

FIT Reading Savitch, Chapter 11 for the keen: Thinking Recursively with Java Eric Roberts, Wiley 2006 (this will help you a lot with FIT1008)