Python: Iterative Towers of Hanoi

This is an implementation of an iterative solution to the Towers of Hanoi Puzzle (THP).

The recursive solution to THP is very common:

Being recursive, this is terribly inefficient spacewise at runtime. Hence, an iterative solution.

The iterative solution goes:

Note that in either case, the algorithm still runs in O(2n) time. The iterative solution only reduces the space complexity of the algorithm.

Alternatively, the iterative solution may be stated as:

This implementation follows the first form of the solution given above.

Downloads