Tower of Hanoi

Context: FIT1008_MOC · the classic binary Recursion (Recursion Notation) · a Divide and Conquer-style problem with exponential cost

Quick Revision

  • 🎯 Objective: move discs source→destination (never larger-on-smaller) ➔ canonical binary recursion whose exponential cost is intrinsic.
  • 📦 Core Components: move top aside ➔ move bottom disc ➔ restack the .
  • ⚡ Key Constraint: exactly moves (provably optimal for 3 pegs); only stack.

📝 Core

1. The Problem & Recursive Solution

  • Rules ➔ move discs source→destination, never larger-on-smaller; 3 pegs, spare = “the other peg”.
  • Binary recursion ➔ clear top to spare ➔ move bottom disc ➔ restack the .

2. Cost: and Optimality

  • Recurrence.
  • Provably minimal ➔ moving the bottom disc forces all onto the spare first () then onto it after () ➔ .

3. Binary but Not Overlapping

  • Distinct subproblems ➔ two calls per level (like naive Fibonacci), but they use different pegs.
  • No memoisation moves are genuinely required output; space is (one live path), not .

⚙️ Core Implementation

🔹 tower_Hanoi (binary, direct recursion)

⚖️ Core Decision Matrix

AspectTower of Hanoinaive Fibonacci
Recursion shapebinary, directbinary, direct
Subproblemsdistinct (different pegs)overlapping (recomputed)
Time (= output size)
Memoisation helps?No — output is exponentialYes
Space (stack)

When It Flips: vs Divide and Conquer — Hanoi divides into two subproblems + a combine, but the subproblems are size (not ), hence exponential not log-linear.

📊 Exam Execution Trace

Manual Execution Trace

tower_Hanoi(3, A, C) (7 moves = ):

Step / StateTrigger OpMoveSub-call
0 (Init)callclear top 2 (A→B uses C)
1moveA → C
2moveA → B
3moveC → B
4moveA → Cmove bottom disc
5moveB → Arestack 2 (B→C uses A)
6moveB → C
7moveA → C

Applied Exercise

Problem: Solve the move-count recurrence and confirm optimality. Derivation Proof / Hand-Calculation Walkthrough:

Final Extracted Output: exactly moves — the provable minimum for 3 pegs (Q.E.D.).

🧠 Active Recall