Perl: the Markov chain saw  
PerlMonks 
Re: Recursion: the Towers of Hanoi problemby blokhead (Monsignor) 
on Oct 19, 2004 at 01:39 UTC ( #400376=note: print w/ replies, xml )  Need Help?? 
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(n1). 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. blokhead
In Section
Tutorials

