Blogs

Clay Breshears

See more from this author

Rebuilding the Tower of Hanoi

Sometimes seemingly serial computations may be coded with parallel algorithms if you can figure out how to present the final results in the proper order.


The Tower of Hanoi is a puzzle that consists of three pegs and a set of disks. Each disk has a different diameter and a hole in the middle so that the disk can fit onto any of the pegs. The initial puzzle setup has two of the pegs empty and all the disks on the third peg (source) in monotonically decreasing order of diameter from bottom to top, which forms a structure that is reminiscent of a tower. The goal of the puzzle is to move the tower from the source peg to a specified peg (destination) using the other peg to temporarily hold disks. The two rules of the Tower of Hanoi puzzle are that only one disk at a time can be moved from the top of a stack of disks on a peg to some other peg, and disks with larger diameter cannot be placed on top of smaller diameter disks.

French mathematician Edouard Lucas developed the puzzle in 1883. He also created the myth for his puzzle that claimed monks have been working non-stop on a 64-disk version from the beginning of time. Once they have solved this problem, the tower will crumble and the world will end. It's a good story and, I'm sure, it helped to promote the puzzle, but everyone knows that the world ends at 03:14:07 on Tuesday, January 19, 2038.

A programmed solution can be implemented using recursion in a most elegant way. (Again with the recursion? Just one more to finish up, I swear. I'll start my next post with something completely different.) Here's one way to code up a solution.

void tower(char source, char dest, char temp, int level)
{
  if (level > 0) {
    tower(source, temp, dest, level-1);
    printf("Move from %c to %c\n", source, dest);
    tower(temp, dest, source, level-1);
  }
  else
    printf("Move from %c to %c\n", source, dest);
}
. . .
  tower('A', ‘C', 'B', numDisks-1);
. . .

I've used single characters to denote the pegs ('A' is the initial source peg, 'C' is the final destination, and 'B' is the temporary peg). The recursion is halted when the level parameter reaches zero. This corresponds to the source peg having one disk remaining on it, so the code simply prints the move of a disk from the source to the destination peg without further recursive calls. As you can see, the purpose of the code is to print out the set of instructions that can be mechanically followed by someone trying to solve the puzzle in the fewest moves.





Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

DrDobbs encourages readers to engage in spirited, healthy debate, including taking us to task. However, DrDobbs moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing/SPAM. DrDobbs further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Features

  • Booting an Intel Architecture System, Part II: Advanced Initialization

    Once the processor is running and memory has been initialized, timers and devices must be started up and a memory map laid out. Only then, can the OS be loaded.

  • Booting an Intel Architecture System, Part I: Early Initialization

    The boot sequence today is far more complex than it was even a decade ago. Here's a detailed, low-level, step-by-step walkthrough of the boot up.

  • How Data Dependence Affects Performance

    Data dependence between statements is a straightjacket on the compiler's ability to optimize code for parallelism. So to get the maximum benefit from parallel code, data dependence must be carefully managed

  • The Intel Threading Building Blocks Flow Graph

    User feedback inspired the flow graph feature in Intel Threading Building Blocks, which allows programmers to express static and dynamic dependency graphs, as well as reactive or event-based graphs.

More Features >>

Go Parallel Video

Go Parallel Twitter Feed

More Twitter >>

Our Facebook Community