[Math] Modeling the maximum number of moves in Tower of Hanoi problem

algorithmsrecursion

What would be the recursive algorithm for solving the Tower of Hanoi problem (with n disks and 3 pegs) in maximal number of moves (i.e. going through all possible disks/pegs combinations).

Best Answer

In the initial position there are three towers, that I will call A,B,C, and assume the problem we want to solve recursively is:

Problem: Move all disks located in tower $X$ to tower $Y$ (for $X,Y\in \{A,B,C\}$ and $X\neq Y$).

The recursive solution is as follows:

  • For $n=1$ disks: move the disk from $X$ to $Y$.
  • For $n=k+1$ disks:

    1. Use the solution for $k$ disks to move the first $k$ disks from tower $X$ to the tower $Z$, namely the only tower in the set $\{A,B,C\}\setminus \{X,Y\}$.
    2. Move the $(k+1)$-th disk to tower $Y$.
    3. Use the solution for $k$ disks to move the first $k$ disks from tower $Z$ to tower $Y$.

With this, the number of moves is recursively defined as:

$$\begin{cases}M_1=1\\ M_{k+1}=2M_k+1\end{cases}$$ which have the following general formula:

$$M_n=1+2+\ldots+2^{n-1}=2^n-1$$