Tuesday, July 13, 2010

Tower of Hanoi

Tower of Hanoi is an old and popular problem. The description of the problem is given here - http://en.wikipedia.org/wiki/Tower_of_Hanoi. The solution for this problem can be designed in various ways, few of which are listed in the above wiki page. I have coded a recursive solution to solve the problem. You can find many other solutions in the web as well.

The aim of coding for this particular solution is inspired by the SICP (Structure and Interpretation of Computer Programs) MIT video lectures by Prof. Abelson and Gerald Jay. The aim is to understand the recursive nature of the program.

The program is available at my bitbucket collection - click here (takes you to the page containing the code).

In order to understand the recursive nature of the program. you can compile it with debugging symbols -
gcc -g towers_of_hanoi.cpp


and then use the gdb debugger (learn it) to trace through the different stacks (a, b, c).
gdb a.out


Also, use backtrace (bt) command in gdb to trace through the system stack to get a better understanding of how the code implements the solution.

No comments:

Post a Comment