Friday, April 29, 2011

Bringing Down Towers of Hanoi - part9

As we promised in part8 of this series, In this post we are going to see few sample outputs from Tower4 program in action. Also we will discuss the algorithm behind how to determine the best breakdown of middle towers. (Remember, in four tower case, we have choices on how to distribute the disks between tower 2 and 3 when we want to move disks from tower 1 to 4)

Let's see the case of 5 disks :

6,5,4,3,2, :  : 1, : 
6,5,4,3, :  : 1, : 2,
6,5,4, : 3, : 1, : 2,
6,5,4, : 3,2, : 1, : 
6,5,4, : 3,2,1, :  : 
6,5, : 3,2,1, :  : 4,
6, : 3,2,1, : 5, : 4,
6, : 3,2,1, : 5,4, : 
 : 3,2,1, : 5,4, : 6,
4, : 3,2,1, : 5, : 6,
4, : 3,2,1, :  : 6,5,
 : 3,2,1, :  : 6,5,4,
 : 3,2, : 1, : 6,5,4,
2, : 3, : 1, : 6,5,4,
2, :  : 1, : 6,5,4,3,
 :  : 1, : 6,5,4,3,2,
 :  :  : 6,5,4,3,2,1,
17 Moves

If we look at step 8 above (in blue), we will see that the program has distributed top 5 disks as 3 : 2 in the middle 2 towers. It has then moved the 6th disk to 4th tower. It could have distributed them as 4 : 1, but has found that it will be more costly. Mini tower 3-2-1 it has created on tower2 was created using all 4 towers but the Mini tower it has created on tower3 (which has disks 4 and 5) was created using only 3 towers while keeping the already buildup content at tower 2 intact.

Once the biggest disk (disk 6) was moved to tower 4, program has created a mini tower 4-5 at top of tower4 while keeping tower 2 intact. After that step it has created a mini tower 3-2-1 on tower4 using all 4 towers.

Let's have a good look at again and make sure we understand the process! 

In general, to solve 4 tower case for n disks, we need to follow these steps :
1) find optimal numbers for x and y such that x+y=n-1

2) build a tower of hight x in tower2 using all 4 towers

3) build a tower of height y (starting from disk x+1) in tower3 while using only 3 towers (since the content we built on tower2 need to be preserved)

4) move the largest disk (which is n) from tower 1 to tower4

5) build a tower of height y on top of tower4 (starting from disk x+1) while using only 3 towers and keeping the content on tower2 intact.

6) build a tower of height x in tower4 using all 4 towers.

Above is a recursive algorithm because to build a tower of n using all 4 towers, we will have to build a tower of x using all 4 towers!

Now, only one problem remaining.
 How to decide on the best distribution between tower2 and 3 (in the example it was 3:2 and not 4:1), that is x was 3 and y was 2 how to find x and y ?

Here too we could use recursion!

Following recursive algorithm and the program is something I have come up with and haven't seen in anywhere else yet. If any of you find a similar algorithm or a better one to solve this puzzle please inform me by putting a comment here with your finding. The code for this will appear in the next post

We need some notations here to make things clearer :
Let T4(n) = 'steps to make a tower of height n using 4 towers'
Let T3(n) = 'steps to make a tower of height n using 3 towers'

Then we can express the steps needed for above 6 steps in this form :
T4(n) = 2*(T4(x)+T3(y)) +1

For instance, T4(6) = 2* (T4(3)+T3(2)) + 1 

but we know T3(m) is 2m-1, hence T3(2) above is 22-1 = 3
So, T4(6) = 2*T4(3)+7

To build a tower of height 1, we need just 1 step.

According to above algorithm (steps 1 to 6 given above), building a tower of 2 will need 3 steps, since n=2, x=1 and y=0 in this case. (you can work out how we arrived at number 3 as home work!)

Now that we know steps needed to build a tower of 1 and a tower of 2, can we find the steps needed to create a tower of 3 ?

We have 2 choices
n =3, x=1,y=1 and
n=3, x=2, y=0

In the first case, steps needed will be 5. (home work for readers) In the second, it will be 7! (more home work) So we go with option 1. That means T4(3) =5 !!

Now we could revisit the problem of T4(6).
Earlier we found T4(6) = 2 * T4(3) + 7
then T4(6) = 2*5 + 7 = 17  Which is exactly the number of steps our program has taken to solve!

So we could recursively find best x and y for any n

All we need now is to write the nice recursive code. We will give the readers few days to come up with that code before unraveling it in our last post of Towers Hanoi series.

After that we will move to a new game and a new problem !

Your feedback on the Hanoi Towers series so far will be greatly welcome!

Tuesday, April 19, 2011

Arrival of the Java Killer ?

The remarkable news of the impending arrival of the new programming language which is already tipped to be the Java Killer is just out.

I had a entire post covering this in araaya blog. What's more the new language is named as Ceylon !! Find out the reasons behing that naming and a pragmatic forcast of things in here.

(Image source http:/

Thursday, April 14, 2011

Towers of Hanoi - part 8

In the earlier post, we learned that still, (surprisingly)  there is no mathematical formula for the minimum number of steps needed to solve a Tower of Hanoi puzzle where there can be towers more than 3 and any number of rings. That is no formula exists which gives the number of steps as a function of  rings and towers.

In this post, we will do away with mathematical aspects of this puzzle and focus on what we know better. Yea, the algorithm!

Suppose we have 10 rings (all in the 1st tower) there are 4 towers and you want to move all to the 4th tower.

1) We have to place first 9 rings in the 2 middle towers (tower2 and tower3).
2) Move the 10th towers from tower 1 to tower 4
3) build a tower of 9 in tower4 by using the rings in 2 middle towers.

This will lead to a recursive algorithm. Trick is to get the step 1 above right, how best to distribute the first 9 rings among two middle towers ?

What I did was to write a separate method to determine just that! In that method I will calculate steps needed achieve to each of different distribution of 9 rings. Distributions goes like this :
tower2 have first 8 rings, tower 3 have the ring 9  Or 
tower2 have first 6 rings, tower 3 has 7 to 9
tower2 have first 5, tower 3 has 6 to 9
and so on....

So my method will calculate steps needed to create each of that distribution and then find the distribution which will need the least number of steps to create. Once we know that, we just call our recursion function to build towers. For instance, if the best configuration is 1-6 in tower2 and 7-9 in tower3, our recursive function will look like this.

buildtower4(tower1,tower2,tower3,tower4,6)  // use all 4 towers and build a tower of 1-6 in tower2)
buildtower3(tower1,tower4,tower3,9,7) // use only the give 3 towers and create a tower with rings 9 to 7 in the third tower)
moveRing(tower1, 10, tower4) // ok, we created the optimal distribution of first 9 rings, now move the 10th ring to final destination)
buildTower3(tower3,tower1,tower4,9,7) // build a tower of 9-7 in tower4. don't touch tower2)
buildTower4(tower1,tower4,tower2,tower3,6) // use all 4 towers and build a tower of 1-6 on top of tower4 

Implement something like that in any language and you will have a working code which solves the 4 tower puzzle!

Now, 3 challenges from today's post :
1) Write the algorithm to determine the optimal combination to distribute N rings in the two middle towers.
2) Implement that algorithm in code.
3) implement the given algorithm today to solve the 4 tower puzzle once and for all!

In the next post we will see few sample outputs from my code... and few screen shots to ram in what we discussed today, for those who have already got the message from today's post, good luck with the code!

See you soon.....