Skip to content

Commit b9335a6

Browse files
committed
Tower of Hanoi problem solution added
1 parent 87d2bf9 commit b9335a6

File tree

6 files changed

+89
-1
lines changed

6 files changed

+89
-1
lines changed

Others/Tower of Hanoi/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Tower of Hanoi Problem
2+
3+
**Tower of Hanoi** is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
4+
1. Only one disk can be moved at a time.
5+
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
6+
3. No disk can be placed on top of a smaller disk.
7+
8+
![Tower of Hanoi](./images/tower_of_hanoi.jpg)
9+
10+
11+
---
12+
### Solution
13+
14+
To solve the problem, first we need to learn how to solve this problem with lesser amount of disks, say → 1 or 2. We mark three towers with name: **source**, **destination** and **aux** (only to help moving the disks). If we have only one disk, then it can easily be moved from source to destination peg.
15+
16+
If we have 2 disks −
17+
18+
- First, we move the smaller (top) disk to aux peg.
19+
- Then, we move the larger (bottom) disk to destination peg.
20+
- And finally, we move the smaller disk from aux to destination peg.
21+
22+
![Tower of Hanoi for two disks](./images/tower_of_hanoi_two_disks.gif)
23+
24+
So now, we are in a position to design an algorithm for Tower of Hanoi with more than two disks. We divide the stack of disks in two parts. The largest disk (nth disk) is in one part and all other (n-1) disks are in the second part.
25+
26+
Our ultimate aim is to move disk n from source to destination and then put all other (n - 1) disks onto it. We can imagine to apply the same in a recursive way for all given set of disks.
27+
28+
The steps to follow are −
29+
30+
- Step 1 − Move n-1 disks from **source** to **aux**
31+
- Step 2 − Move n<sup>th</sup> disk from **source** to **dest**
32+
- Step 3 − Move n-1 disks from **aux** to **dest**
33+
34+
Image illustration for 3 disks :
35+
36+
![Tower of Hanoi solution for 3 disks](http://mathforum.org/dr.math/faq/faq.disk3.gif)
37+
38+
With 3 disks, the puzzle can be solved in 7 moves. The minimal number of moves required to solve a Tower of Hanoi puzzle is 2<sup>n</sup> − 1, where n is the number of disks.
39+
40+
An animated visualization for 3 disks:
41+
42+
![Tower of Hanoi GIF](./images/tower_of_hanoi.gif)
43+
44+
### More on this topic
45+
- [Tower of Hanoi - Wikipedia](https://en.wikipedia.org/wiki/Tower_of_Hanoi)
46+
- [Tower of Hanoi - khanacademy](https://www.khanacademy.org/computing/computer-science/algorithms/towers-of-hanoi/a/towers-of-hanoi)
47+
- [Tower of Hanoi - Tutorialspoint](https://www.tutorialspoint.com/data_structures_algorithms/tower_of_hanoi.htm)
48+
- [Tower of Hanoi - GeeksforGeeks](http://www.geeksforgeeks.org/c-program-for-tower-of-hanoi/)
Loading
11.8 KB
Loading
Loading
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Tower of Hanoi implementation in Javascript */
2+
3+
// This function will recursively store all the moves and return the moves information as Array
4+
function hanoi(count, source, aux, destination, moves) {
5+
moves = moves || [];
6+
7+
// When there is only single disk move it from 'source' to 'destination'
8+
if (count === 1) {
9+
moves.push({
10+
'disk': 1,
11+
'from': source,
12+
'to': destination
13+
});
14+
}
15+
else {
16+
// Move 'n-1' disks from 'source' to 'aux'
17+
hanoi(count - 1, source, destination, aux, moves);
18+
19+
// Move n-th disk from 'source' to 'detination'
20+
moves.push({
21+
'disk': count,
22+
'from': source,
23+
'to': destination
24+
});
25+
26+
// Move (n-1) disks from 'aux' to 'destination'
27+
hanoi(count - 1, aux, source, destination, moves);
28+
}
29+
return moves;
30+
}
31+
32+
33+
34+
/****** Testing Tower of Hanoi code *******/
35+
var moves = hanoi(3, 'A', 'B', 'C');
36+
37+
moves.forEach(function(move){
38+
console.log('moved '+ move.disk+' from:' + move.from + ' to: ' + move.to);
39+
})

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
- Modular inverse
114114
- Probability
115115
- Chinese Remainder Theorem
116-
- Gaussian Elmination method
116+
- Gaussian Elimination method
117117
- Dilworth's Theorem
118118
- Matrix Exponentiation
119119

@@ -137,6 +137,7 @@
137137
- Others
138138
- BackTracking
139139
- N-Queen's Problem
140+
- [Tower of Hanoi Problem](./Others/Tower%20of%20Hanoi/)
140141

141142
---
142143

0 commit comments

Comments
 (0)