Friday, 31 January 2014

Tower of Cheeses

Hanoi Solutions

Recursive solutions are easy to understand, but hard to implement. The reason is because it is easy to create an infinite recursion, or to get confused by nested conditional statements. The solution in Python is to use a list comprehension and perhaps a ternary else. This allows for compact and easily digestible code.

Tower of Hanoi

The solution to a 3-pegged tower of Hanoi is easy for the first n's, but as pegs increase, or as the amount of stackable objects increase, the complexity increases. A recursive solution is ideal for the Tower of Hanoi because the idea of moving a small amount of n objects, isn't much different from moving n+1,n+2,...,n+m objects, this is because the steps are the same for an increasing amount of objects.

Here is how to solve a 3-peg, 3 object Tower: A to C, A to B, C to B, A to C, B to A, B to C, A to B. It follow from an induction proof that n*n - 1 steps is the least possible for a 3-pegged Tower. We can use this idea to create a recursive solution and extend it to 4 pegs, or n pegs.

No comments:

Post a Comment