Question

Using induction how do you prove that two algorithm implementations, one recursive and the other iterative, of the Towers of Hanoi perform identical move operations? The implementations are as follows.

Hanoi(n, src, dst, tmp):
  if n > 0
    hanoi(n-1, src, dst, tmp)
    move disk n from src to dst
    hanoi(n-1, tmp, dst, src)

And iteratively,

If n is even, swap pegs 1 and 2. At the ith step, make the only legal move that avoids peg i mod 3. If there is no legal move, then all disks are on peg i mod 3, and the puzzle is solved.

 hanoi(n, s, t, d)
  pegs[s = [1,2,..,n], t, d]
  if n is even
    swap pegs[1], pegs[2]
  i <- 1
  while(pegs[d].length < n.length)
    avoid <- i mod 3
    if the top of pegs[(avoid + 1) mod 3] > the top of pegs[(avoid + 2) mod 3]
      move the top of pegs[(avoid + 1) mod 3] to pegs[(avoid + 2) mod 3]
    else
      move the top of pegs[(avoid + 2) mod 3] to pegs[(avoid + 1) mod 3]
    i++

I'm having a hard time figuring out how to put this into mathematical expressions or a recurrence-relation which I can then perform induction on to show their equivalence.

My searching has only brought me problems that deal with the complexity or correctness of algorithms, I'm interested in showing that every $i$th move the performed are identical.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with cs.stackexchange
scroll top