Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tower of Hanoi: Recursive Algorithm

Although I have no problem whatsoever understanding recursion, I can't seem to wrap my head around the recursive solution to the Tower of Hanoi problem. Here is the code from Wikipedia:

procedure Hanoi(n: integer; source, dest, by: char); Begin     if (n=1) then         writeln('Move the plate from ', source, ' to ', dest)     else begin         Hanoi(n-1, source, by, dest);         writeln('Move the plate from ', source, ' to ', dest);         Hanoi(n-1, by, dest, source);     end; End;

I understand the base case and the concept of breaking the problem into smaller pieces until you are able to move a single disk. However, I can't figure out how the two recursive calls in the non-base case work together. Perhaps someone can help me out? Thanks.

like image 757
titaniumdecoy Avatar asked Aug 03 '09 16:08

titaniumdecoy


People also ask

What is the recursive function of Tower of Hanoi?

Solving the Tower of Hanoi program using recursion: Function hanoi(n,start,end) outputs a sequence of steps to move n disks from the start rod to the end rod. hanoi(3,1,3) => There are 3 disks in total in rod 1 and it has to be shifted from rod 1 to rod 3(the destination rod).

How do you write an algorithm for Tower of Hanoi?

To write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem with lesser amount of disks, say → 1 or 2. We mark three towers with name, source, destination and aux (only to help moving the disks). If we have only one disk, then it can easily be moved from source to destination peg.

What is the time complexity of Tower of Hanoi recursive algorithm?

Most of the recursive programs takes exponential time that is why it is very hard to write them iteratively . T(1) = 2k T(2) = 3k T(3) = 4k So the space complexity is O(n). Here time complexity is exponential but space complexity is linear .


2 Answers

Actually, the section from where you took that code offers an explanation as well:

To move n discs from peg A to peg C:

  1. move n−1 discs from A to B. This leaves disc #n alone on peg A
  2. move disc #n from A to C
  3. move n−1 discs from B to C so they sit on disc #n

It's pretty clear that you first have to remove n − 1 discs to get access to the nth one. And that you have to move them first to another peg than where you want the full tower to appear.

The code in your post has three arguments, besides the number of discs: A source peg, a destination peg and a temporary peg on which discs can be stored in between (where every disc with size n − 1 fits).

The recursion happens actually twice, there, once before the writeln, once after. The one before the writeln will move n − 1 discs onto the temporary peg, using the destination peg as temporary storage (the arguments in the recursive call are in different order). After that, the remaining disc will be moved to the destination peg and afterwards the second recursion compeltes the moving of the entire tower, by moving the n − 1 tower from the temp peg to the destination peg, above disc n.

like image 74
Joey Avatar answered Oct 05 '22 12:10

Joey


a year ago i had i functional programming course and draw this illustration for the algorithm. hope it helps!

(0)  _|_         |          |     __|__        |          |    ___|___       |          |   ____|____  ____|____  ____|____  (1.1) |          |          |     __|__        |          |    ___|___      _|_         |   ____|____  ____|____  ____|____ (A -> B)  (1.2) |          |          |       |          |          |    ___|___      _|_       __|__   ____|____  ____|____  ____|____ (A -> C)  (1.3) |          |          |       |          |         _|_    ___|___       |        __|__   ____|____  ____|____  ____|____ (B -> C)    (2.1) |          |          |       |          |         _|_       |       ___|___     __|__   ____|____  ____|____  ____|____ (A -> B)    (3.1) |          |          |       |          |          |      _|_      ___|___     __|__   ____|____  ____|____  ____|____ (C -> A)  (3.2) |          |          |       |        __|__        |      _|_      ___|___       |   ____|____  ____|____  ____|____ (C -> B)  (3.3) |         _|_         |       |        __|__        |       |       ___|___       |   ____|____  ____|____  ____|____ (A -> B) 

The 3 rings problem has been splited to 2 2-rings problem (1.x and 3.x)

like image 23
f0b0s Avatar answered Oct 05 '22 13:10

f0b0s