An open API service indexing awesome lists of open source software.

https://github.com/murf-y/towers-of-hanoi

Recursive and Iterative solution for the Tower of Hanoi problem
https://github.com/murf-y/towers-of-hanoi

algorithms recursion tower-of-hanoi

Last synced: 8 months ago
JSON representation

Recursive and Iterative solution for the Tower of Hanoi problem

Awesome Lists containing this project

README

          


🗼Towers Of Hanoi🗼




About the problem :




  • There exists 3 Towers, numbered 1, 2 and 3 respectively.

    First Tower contain N disks, N is the starting number of disks, and they are always placed in the first tower in the beginning. The disks are placed in decreasing order, starting from the bottom. For example suppose N= 2:


    | | |
    *1* | |
    **2** | |
    --------- --------- ---------
    Tower number: (1) (2) (3)



  • Your goal is to move all disks from the first tower (1) to the last tower (3), you can use the middle tower (2) as a transit tower, but you need to obey the Rules.


  • Rules:

    • Move only one disk at a time.

    • You cannot Place a disk with size n, on top of a disk with size m, where m > n.

      i.e You cannot place a big disk on a smaller one.

    • There wont exists 2 disks a, b where
      SizeOf( a ) == SizeOf( b ) .




  • Read More about the problem and details here.


Solution


This repository, contains the solution for Tower of Hanoi recursively and Itertively.


I will be explaining my attempt for the iterative solution only, since the recursive one is literally too easy.



First, without loss of generality, lets assume that the number of disks is 3. And each disk is represented with a alphabetic character, such as the smallest disk start with A

disk 1 => A

disk 2 => B
disk 3 => C

We will initialize a variable to represent the number of moves done till now
move_counter = 0;

Now we will convert this move_counter into base-2, and include 3 bits
move_counter ==binary==> 000

Here the magic begins,
lets associate each bit to a disk, starting from the right most bit with the smallest disk.

0 0 0
| | |
C B A


Now we start incrementing move_counter till we reach 2^n - 1, which is the Max number represented with 3 bits.
move_counter += 1;

// In base 2
0 0 1

First rule, Each bit, that changes from 0 to 1, mean that we need to move the disk associated with that bit.The right most bit (i.e bit 1) changed from 0 to 1 when we incremented move_counter thus we need to move Disk A.To know the number of moves for disk A - and only for A - we investigate if the number of disks is even or not.

In this case, number of disks = 3 (odd), thus we move 2 steps to the right,
if the number of disks was even then we only move 1 step to the right.

After we moved disk A to the right 2 steps, this is the state of the towers.

| | |
**2** | |
***3*** | *1*
_________ _________ _________

Now we re increment move_counter by 1.
move_counter += 1; // moves_counter = 2

// In base 2
0 1 0

The right most bit (i.e Bit 1) changed from 1 to 0, we dont do anything about disk A, however, bit 2 changed from 0 to 1 after we incremented move_counter , thus we move disk B. Note that only disk A moves 2 (odd) steps or 1 (even) step, all other disk moves to the available tower respectively, thus we need to move disk B to the first available tower, which is tower (2).

| | |
| | |
***3*** **2** *1*
_________ _________ _________

Now we re increment move_counter by 1.
move_counter += 1; // moves_counter = 3

// In base 2
0 1 1

Bit 1 changed from 0 to 1,thus we need to move disk A two steps to the right, i.e to tower (2).

| | |
| *1* |
***3*** **2** |
_________ _________ _________

This algorithm continues till move_counter reaches 2^n - 1.


I will put the moves without explaining.

// move_counter = 4 => 1 0 0
| | |
| *1* |
| **2** ***3***
_________ _________ _________


// move_counter = 5 => 1 0 1
| | |
| | |
*1* **2** ***3***
_________ _________ _________


// move_counter = 6 => 1 1 0
| | |
| | **2**
*1* | ***3***
_________ _________ _________


// move_counter = 7 => 1 1 1
| | *1*
| | **2**
| | ***3***
_________ _________ _________

Done.








Code :


Solution is implemented in java, you can download the source code and compile it. then run it to test the code.


License:


MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.