Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Recursion: the Towers of Hanoi problem

by blokhead (Monsignor)
on Oct 19, 2004 at 01:39 UTC ( #400376=note: print w/replies, xml ) Need Help??

in reply to Recursion: The Towers of Hanoi problem

Another related fun 'n' interesting problem is to start with the disks placed randomly onto the three pegs and then try to sort them all onto one peg. In this version, you are allowed to place any disk onto any other disk (no size restrictions). In fact, a question on a recent algorithms Ph.D qualifying exam here at UIUC was to show that disks can be sorted in O(n log n) moves, and to also show that in fact, &Omega(n log n) moves are required in general. I won't give away how to sort the disks in O(n log n) time, but here's a hint: adapt quicksort.

If we aren't allowed to put larger disks on top of smaller disks (as in the classic example above), you need an exponential number of moves. If T(n) is the number of moves needed to sort n disks, your recursion gives the recurrence T(n) = 2T(n-1). You can verify that T(n) = 2^n.

Towers of Hanoi problems are good examples in that they are easy to visualize, and their relationship to sorting with stacks is obvious.


  • Comment on Re: Recursion: the Towers of Hanoi problem

Replies are listed 'Best First'.
Visualizing (was Re^2: Visiting the Towers of Hanoi.)
by merlyn (Sage) on Oct 19, 2004 at 03:50 UTC
    Towers of Hanoi problems are good examples in that they are easy to visualize, and their relationship to sorting with stacks is obvious.
    However, if you are selecting problems for a syllabus, please be aware that "easy to visualize" can actually give an unfair disadvantage to some of your students. I've commented on my own affliction with regard to using GUIs and learning to program.

    I completely understand the algorithm for Towers of Hanoi, and why it works, and have translated it into a few languages. But I understood it only by playing with physical disks or marks (and erasing) on a piece of paper. I could never have constructed it by "visualization", since my brain doesn't work that way.

    So, beware when you pretend you know what skills are "universal" about thinking... you may have wrong thinking about that. {grin}

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://400376]
[Discipulus]: my warmest comprehension karlgoethebier ;=(
[Discipulus]: yes LanX.. is so good to be appreciated (I fortunately have a big self estimation that compensate)
[LanX]: you can manipulate a html tree by addressing and replacing so called document objects (DOM) and from my experience the same methods can be used with any XML, though I didn't test all browsers yet
LanX just joking, playing "ego judo"
[marto]: good morning all
[Corion]: Hi marto!
[marto]: the race to the bottom continues, many people lost their job yesterday as a client moves to another outsourcer
[LanX]: brexit or perlxit ?
[Corion]: Ow, marto :-/ I hope you aren't affected (except for the looming dread of more work for the same pay)

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2017-03-23 09:54 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (285 votes). Check out past polls.