COMPSCI NOTES FRI 4/17

Overview of Recursion and Towers of Hanoi Algorithm

  • This section covers the recursive algorithm for the Towers of Hanoi problem, including how to define base cases and recursive cases.

Towers of Hanoi Overview

  • The Towers of Hanoi is a classic problem that involves moving a set of rings from one rod to another, adhering to specific rules.

  • The objective is to move all rings from the start rod to the goal rod using a temporary rod.

Base Case

  • A base case simplifies a problem to a point where it can be solved without further recursion.   - Definition: If the number of rings is one, the action required is simply to move the ring from the start to the goal rod.   - Example: Moving a single ring can be achieved without recursion.   

Recursive Case

  • The recursive case defines how to break down the problem into smaller, similar problems until reaching the base case.

  • Moving the largest disc (ring) from the start to the goal involves ensuring all other discs are out of the way.

  • Specific approach to solving the recursive case:   1. Move n-1 discs from the start rod to the temporary rod.   2. Move the largest disc from the start rod to the goal rod.   3. Move the n-1 discs from the temporary rod to the goal rod.

Steps to Solve Towers Using Recursion

  1. When moving four discs from the start to the goal:    - First, recursively move three discs from the start to the temporary rod.    - This pattern continues down until reaching the base case (one disc).

  2. Recursive Algorithm Steps:    - We call the function for three discs:      - Move from Start to Temp to move n-1 discs out of the way.      - The parameters for each call include:        - Number of rings to move: numRings - 1        - Starting rod: Start        - Target rod: Temp        - Temporary rod: Goal

Trusting Recursive Calls

  • The method is built on the trust that if the recursion works for smaller problems, it will work for larger problems:   - The instruction is printed once we trust that the recursion properly moved the smaller discs out of the way.

  • For example, to move the largest disc, the instruction is to move from Start to Goal.

Final Steps

  • After moving the largest disc to the goal, the smaller discs must be moved from the temporary rod to the goal rod:   1. Trust recursion to handle the next smaller problem (moving n-1 discs from Temp to Goal).   2. Starting point for this step is the temporary rod and the goal is the goal rod.

Debugging the Recursive Algorithm

  • The performance of the recursive implementation can be tested by tracking the steps taken:
      - If running the algorithm for a set number of rings, follow the output instructions to verify movements.

  • For 3 rings scenario:   1. Move ring from 1 (Start) to 3 (Goal)   2. Move ring from 1 (Start) to 2 (Temp)   3. Move ring from 3 (Goal) to 2 (Temp)   4. This continues recursively.   - Useful debugging technique includes tracing back through the parameters to ensure correctness.

Iterative vs Recursive Approach

  • Although Towers of Hanoi can be solved iteratively, recursion is more intuitive for this specific problem:   - Recursive calls simplify the handling of the problem structure by aligning the smaller problems in a direct pattern leading to the solution.   - Contrast with problems more suited to loops, where iteration is typically clearer, e.g., summing array elements, checking palindromes.

Conclusion

  • The algorithm itself has been carefully structured so it can easily handle any size of the Towers of Hanoi problem.

  • The core of recursion lies in solving smaller instances of the same problem and trusting that those solutions apply to the overall problem.

  • Final assignment is to practice writing recursive methods and tracing function calls efficiently.