|
ADS
Algorithms and Data Structures
|
The towers of Hanoi. More...
Files | |
| file | ADS/algorithm/Hanoi.h |
| The towers of Hanoi: moving \( n \) disks from the peg source to the peg dest, one at a time and never on a smaller one, with the peg middle as a spare. | |
Functions | |
| template<class F> | |
| void | ADS::hanoi_recursive (int n, int source, int dest, int middle, F &&f) |
| the moves of \( n \ge 1 \) disks from source to dest, by the recursion of the book (its hanoi-recursive()) | |
| template<class F> | |
| void | ADS::hanoi_iterative (int n, int source, int dest, int middle, F &&f) |
| the moves of hanoi_recursive() with the calls on a stack (the first hanoi-iterative() of the book): the calls (1) are pushed, and each pop resumes the call left open, moving a disk and simulating the call (2) | |
| template<class F> | |
| void | ADS::hanoi_iterative_labels (int n, int source, int dest, int middle, F &&f) |
| the moves of hanoi_recursive() by the systematic rules (the second hanoi-iterative() of the book), saving on the stack the parameters and the label from which the call resumes | |
The towers of Hanoi.
The definitions, the invariants and the costs of the area are in Theory of other algorithms.
| void ADS::hanoi_iterative | ( | int | n, |
| int | source, | ||
| int | dest, | ||
| int | middle, | ||
| F && | f ) |
the moves of hanoi_recursive() with the calls on a stack (the first hanoi-iterative() of the book): the calls (1) are pushed, and each pop resumes the call left open, moving a disk and simulating the call (2)
In pseudocode (section 4.4), the call (2) being the last one, a tail call turned into a change of the parameters:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{hanoiIterative}( n , \mathit{src} , \mathit{dest} , \mathit{mid} ) \cr 2 & \quad S \gets \text{an empty stack of frames } ( n , \mathit{src} , \mathit{dest} , \mathit{mid} ) \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{while}\ n > 1\ \textbf{do}\quad \triangleright \ \text{down the calls (1)} \cr 5 & \qquad\quad \mathrm{push}( S , ( n , \mathit{src} , \mathit{dest} , \mathit{mid} ) );\ ( n , \mathit{dest} , \mathit{mid} ) \gets ( n - 1 , \mathit{mid} , \mathit{dest} ) \cr 6 & \qquad \mathrm{f}( \mathit{src} , \mathit{dest} )\quad \triangleright\ \text{one disk} \cr 7 & \qquad \textbf{if}\ S = \emptyset\ \textbf{then return} \cr 8 & \qquad ( n , \mathit{src} , \mathit{dest} , \mathit{mid} ) \gets \mathrm{pop}( S );\ \mathrm{f}( \mathit{src} , \mathit{dest} ) \cr 9 & \qquad ( n , \mathit{src} , \mathit{mid} ) \gets ( n - 1 , \mathit{mid} , \mathit{src} )\quad \triangleright \ \text{the call (2), a tail call: no frame} \end{array}} \]
| void ADS::hanoi_iterative_labels | ( | int | n, |
| int | source, | ||
| int | dest, | ||
| int | middle, | ||
| F && | f ) |
the moves of hanoi_recursive() by the systematic rules (the second hanoi-iterative() of the book), saving on the stack the parameters and the label from which the call resumes
In pseudocode (section 4.4), a call pushing the label after it and a return popping it:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{hanoiLabels}( n , \mathit{src} , \mathit{dest} , \mathit{mid} ) \cr 2 & \quad S \gets \text{an empty stack};\ \mathit{pos} \gets \text{START}\quad \triangleright\ \mathit{pos} \text{ the point of the recursive body} \cr 3 & \quad \textbf{while}\ \mathit{pos} \ne \text{STOP}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ \mathit{pos} = \text{START}\ \textbf{and}\ n = 1 \ \textbf{then}\ \mathrm{f}( \mathit{src} , \mathit{dest} ); \ \mathit{pos} \gets \text{END} \cr 5 & \qquad \textbf{else if}\ \mathit{pos} = \text{START} \ \textbf{then}\quad \triangleright\ \text{the call (1)} \cr 6 & \qquad\quad \mathrm{push}( S , ( n , \mathit{src} , \mathit{dest} , \mathit{mid} , \text{AFTER1} ) );\ ( n , \mathit{dest} , \mathit{mid} ) \gets ( n - 1 , \mathit{mid} , \mathit{dest} ) \cr 7 & \qquad \textbf{else if}\ \mathit{pos} = \text{AFTER1} \ \textbf{then}\quad \triangleright \ \text{the move, then the call (2)} \cr 8 & \qquad\quad \mathrm{f}( \mathit{src} , \mathit{dest} ); \ \mathrm{push}( S , ( n , \mathit{src} , \mathit{dest} , \mathit{mid} , \text{END} ) ) \cr 9 & \qquad\quad ( n , \mathit{src} , \mathit{mid} , \mathit{pos} ) \gets ( n - 1 , \mathit{mid} , \mathit{src} , \text{START} ) \cr 10 & \qquad \textbf{else if}\ S \ne \emptyset\ \textbf{then}\ ( n , \mathit{src} , \mathit{dest} , \mathit{mid} , \mathit{pos} ) \gets \mathrm{pop}( S )\quad \triangleright\ \text{the return} \cr 11 & \qquad \textbf{else}\ \mathit{pos} \gets \text{STOP} \end{array}} \]
| void ADS::hanoi_recursive | ( | int | n, |
| int | source, | ||
| int | dest, | ||
| int | middle, | ||
| F && | f ) |
the moves of \( n \ge 1 \) disks from source to dest, by the recursion of the book (its hanoi-recursive())
In pseudocode (section 4.4):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{hanoi}( n , \mathit{src} , \mathit{dest} , \mathit{mid} ) \cr 2 & \quad \textbf{if}\ n > 1\ \textbf{then}\ \mathrm{hanoi}( n - 1 , \mathit{src} , \mathit{mid} , \mathit{dest} )\quad \triangleright \ \text{(1): the } n - 1 \text{ smaller disks aside} \cr 3 & \quad \mathrm{f}( \mathit{src} , \mathit{dest} )\quad \triangleright \ \text{the largest disk} \cr 4 & \quad \textbf{if}\ n > 1\ \textbf{then}\ \mathrm{hanoi}( n - 1 , \mathit{mid} , \mathit{dest} , \mathit{src} )\quad \triangleright \ \text{(2): back on top of it} \end{array}} \]