Tower of Hanoi is a mathematical puzzle. Traditionally, It consists of three poles and a number of disks of different sizes which can slide onto any poles. The puzzle starts with the disk in a neat stack in ascending order of size in one pole, the smallest at the top thus making a conical shape. The objective of the puzzle is to move all the disks from one pole (say ‘source pole’) to another pole (say ‘destination pole’) with the help of third pole (say auxiliary pole).
The puzzle has the following two rules:
1. You can’t place a larger disk onto smaller disk
2. Only one disk can be moved at a time
We’ve already discussed recursive solution for Tower of Hanoi with time complexity O(2^n). Using 4 rods, same approach shows significant decrease in time complexity.
Input : 3 Output : Move disk 1 from rod A to rod B Move disk 2 from rod A to rod C Move disk 3 from rod A to rod D Move disk 2 from rod C to rod D Move disk 1 from rod B to rod D Input : 5 Output : Move disk 1 from rod A to rod C Move disk 2 from rod A to rod D Move disk 3 from rod A to rod B Move disk 2 from rod D to rod B Move disk 1 from rod C to rod B Move disk 4 from rod A to rod C Move disk 5 from rod A to rod D Move disk 4 from rod C to rod D Move disk 1 from rod B to rod A Move disk 2 from rod B to rod C Move disk 3 from rod B to rod D Move disk 2 from rod C to rod D Move disk 1 from rod A to rod D
Move disk 1 from rod A to rod D Move disk 2 from rod A to rod B Move disk 1 from rod D to rod B Move disk 3 from rod A to rod C Move disk 4 from rod A to rod D Move disk 3 from rod C to rod D Move disk 1 from rod B to rod C Move disk 2 from rod B to rod D Move disk 1 from rod C to rod D
Time Complexity: O(2^(N/2))
This article contributed by madHEYsia. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program for Tower of Hanoi
- Time Complexity Analysis | Tower Of Hanoi (Recursion)
- Cost Based Tower of Hanoi
- Twisted Tower of Hanoi Problem
- Count subtrees that sum up to a given value x only using single recursive function
- Maximum number on 7-segment display using N segments : Recursive
- Floor value Kth root of a number using Recursive Binary Search
- Recursive Functions
- Recursive Bubble Sort
- Recursive Insertion Sort
- Recursive Implementation of atoi()
- Recursive Programs to find Minimum and Maximum elements of array
- Recursive program to print formula for GCD of n integers
- Recursive solution to count substrings with same first and last characters
- Recursive function to delete k-th node from linked list
- Recursive insertion and traversal linked list
- Count consonants in a string (Iterative and recursive methods)
- First uppercase letter in a string (Iterative and Recursive)
- Recursive approach for alternating split of Linked List
- Recursive program for prime number