Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Hanoi Tower Problem

Similar presentations


Presentation on theme: "The Hanoi Tower Problem"— Presentation transcript:

1 The Hanoi Tower Problem
Problem: There are n disks of different diameters on three pegs, labeled as Peg 1, Peg 2, as Peg 3. At the beginning, all n disks are on Peg 1, arranged in an increasing order of their diameter from top down. The question is to move all disks to Peg 3 according to the following rules: 1. Each time, only one disk on top of a peg is moved to the top of another peg. 2. No disk can be put on top of a smaller disk.

2 Start Peg 1 Peg 2 Peg 3 End Peg 1 Peg 2 Peg 3

3 Solving Idea The base case: If there is no disk (n = 0), nothing has to be done. Reduction: Now suppose the number of disks is at least one. Ignore the largest disk, we have n – 1 disks. A smaller case. Recursion: The smaller case can be solved by a recursive call to the same algorithm since this reduction can eventually be reduced to the base case. Building up the solution: Move n – 1disks from the source peg to the intermediate peg, move one disk from the source peg to the destination peg, and finally, move n – 1 disks from the intermediate peg to the destination peg.

4 The Algorithm GIVEN: N (a positive integer)
S, I, D (the source peg, intermediate peg, and the destination peg) RESULT: Output the steps of movements HEADER Hanoi (N, S, I, D) BODY test: N > 0? false true Ø Hanoi (N - 1, S, D, I) output: "S -> D" Hanoi (N - 1, I, S, D)

5 Java Implementation public static void hanoi (int n, int s, int i, int d) { if (n > 0) hanoi (n - 1, s, d, i); System.out.println (s + " -> " + d); hanoi (n - 1, i, s, d); }

6 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3)

7 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2)

8 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

9 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

10 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

11 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

12 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

13 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

14 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

15 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

16 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

17 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

18 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)

19 How it works when n = 3, s = 1, i = 2, d = 3?
hanoi(3,1,2,3) hanoi(2,1,3,2) > 3 hanoi(2,2,1,3) hanoi(1,1,2,3) 1 -> 2 hanoi(1,3,1,2) hanoi(1,2,3,1) 2 -> 3 hanoi(1,1,2,3) hanoi(0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3) hanoi(0,1,3,2) 1 -> > > > 3 hanoi(0,2,1,3) hanoi (0,1,3,2) hanoi(0,3,2,1) hanoi(0,2,1,3)


Download ppt "The Hanoi Tower Problem"

Similar presentations


Ads by Google