Towers of Hanoi

Main paper: Optimality of an Algorithm Solving the
Bottleneck Tower of Hanoi Problem (1996) – Yefim Dinitz
& Shay Solomon
Presented by: Rotem Golan & Carmel Bregman
Department of Computer Science
Ben-Gurion University of the Negev
Problem definition
 The Tower of Hanoi is a mathematical game or puzzle. It
consists of three rods, and a number of disks of different
sizes which can slide onto any rod. The puzzle starts with
the disks in a neat stack in ascending order of size on one
rod, the smallest at the top, thus making a conical shape.
 The objective of the puzzle is to move the entire stack to
another rod, obeying the following rules:
 Only one disk may be moved at a time.
 Each move consists of taking the upper disk from one of the
rods and sliding it onto another rod, on top of the other disks
that may already be present on that rod.
 No disk may be placed on top of a smaller disk.
• Some Applications
• Classical version
• Graphical representation
• Ants solve the problem
• Relaxed version
• Further research & References
Some applications
 The Tower of Hanoi is used as a backup rotation scheme
when performing computer data backup where multiple
tapes/media are involved.
 For example, FIFO is the simplest backup scheme. It saves
new or modified files onto the oldest media in the set.
 By using the optimal solution for the Towers of Hanoi
problem, every tape is associated with a disk in the puzzle,
and every disk movement to a different peg corresponds
with a backup to that tape. So the first tape is used every
other day (1, 3, 5, 7, 9, … ), the second tape is used every
fourth day (2, 6, 10, … ), the third tape is used every eighth
day (4, 12, 20, … ).
Some applications (Cont.)
 It is a 'smart' way of archiving an effective number of
backups as well as the ability to go back over time
 The Tower of Hanoi is also used in psychological
research on problem solving.
 The Tower of Hanoi is popular for teaching recursive
algorithms to beginning programming students.
• Some Applications
• Classical version
• Graphical representation
• Ants solve the problem
• Relaxed version
• Further research & References
Recursive solution
 𝛾𝑛
 If 𝑛 = 0 then do nothing
 Otherwise



Perform 𝛾𝑛−1 (𝑠𝑜𝑢𝑟𝑐𝑒, 𝑎𝑢𝑥𝑖𝑙𝑖𝑎𝑟𝑦)
move disc 𝑛 from 𝑠𝑜𝑢𝑟𝑐𝑒 to 𝑡𝑎𝑟𝑔𝑒𝑡
Perform 𝛾𝑛−1 (𝑎𝑢𝑥𝑖𝑙𝑖𝑎𝑟𝑦, 𝑡𝑎𝑟𝑔𝑒𝑡)
Correctness & optimality
 We’ll use a simple induction
 Basis: If 𝑛 = 1 then we move the disk from peg 𝐴 to peg 𝐶 and it
is obviously correct and optimal.
 Induction hypothesis: if 𝑛 = 𝑘 − 1 we move all 𝑘 − 1 disks
from one peg to another using a minimal number of steps.
 We’ll prove that it is also true for 𝑛 = 𝑘 disks.
 Induction step: Before the first move of disk 𝑛 from source to
some peg, all 𝑘 − 1 other disks should be moved from source to
the spare peg. After the last move of disk 𝑛 from some peg to
target, all 𝑘 − 1 other disks should be moved from the spare peg
to target.
According to the induction hypothesis, we move all 𝑘 − 1 disks
from peg 𝐴 to peg 𝐵 and then from peg 𝐵 to peg 𝐶 using a
minimal number of steps. Therefore, the algorithm moves all 𝑘
disks from peg 𝐴 to peg 𝐶 in a minimal number of steps.
Complexity
 𝑇(𝑛) = 2 ∗ 𝑇(𝑛 − 1) + 1
 Lets prove that 𝑇 𝑛 = 2𝑛 − 1
 We’ll use a simple induction.
 If 𝑛 = 1: 𝑇(𝑛) = 21 − 1 = 1
 Lets assume 𝑇(𝑛 − 1) = 2𝑛−1 − 1 then according to
the above formula and the induction hypothesis:
𝑇(𝑛) = 2 ∗ (2𝑛−1 −1) + 1 = 2𝑛 − 1
• Some Applications
• Classical version
• Graphical representation
• Ants solve the problem
• Relaxed version
• Further research & References
Graphical representation
 Lets define 𝑛 to be the number of disks
 The graph edges correspond to possible moves
 For 𝑛 = 1
 For 𝑛 = 2
Graphical representation (Cont.)
 For 𝑛 = 3
Graphical representation (cont.)
 The diagram for 𝑛 + 1 disks is obtained by taking three
copies of the 𝑛-disk diagram—each one representing all
the states and moves of the smaller disks for one particular
position of the new largest disk—and joining them at the
corners with three new edges, representing the only three
opportunities to move the largest disk
 As more disks are added, the graph representation of the
game will resemble the Fractal figure, Sierpinski triangle.
 These graphs are exponential in the number of disks (𝑛)
from the TOH problem. To be more precise, it has 3𝑛
nodes.
Sierpinski triangle
• Some Applications
• Classical version
• Graphical representation
• Ants solve the problem
• Relaxed version
• Further research & References
Ants solve the TOH (2010)
 The graphical representation of the Towers of Hanoi
problem can be converted into a maze where living ants
can search though.
 Their goal will be to find the shortest path between their
entrance point and a food source.
 This shortest path will later be translated into a sequence
of moves which solves the TOH problem optimally.
 Prof. Paul K. Stockmeyer has collected about 369
mathematical papers on the Towers of Hanoi problem.
Adding this paper to his collection might be a good idea.
The results
 All ant colonies tested succeeded in solving the Towers of
Hanoi in the sense that they all constructed a trail between
the nest and the food source.
 93.3% of the colonies with exposure succeeded in finding a
shortest path solution by the end of the first hour, whereas
43% of the colonies without pre-exposure achieved this.
 When one of the minimal paths was then blocked: 90 to
93.3% of the colonies with exposure were able to find the
remaining minimal solution, whereas 73.3 to 78.6% of the
colonies without pre-exposure achieved this.
• Some Applications
• Classical version
• Graphical representation
• Ants solve the problem
• Relaxed version
• Further research & References
Relaxed definition
• We generalize the problem using the following
constraint:
• Let a constant 𝑘 be given.
• Disk 𝑗 may be placed higher then disk 𝑖 on the same
peg only if their size difference 𝑗 − 𝑖 is less then 𝑘.
• If 𝑘 = 1 the problem is equivalent to the classical
version.
Running example of Poole’s algorithm
𝑘 = 2 ,𝑛 = 5
Small(n-1)
𝐵𝑖𝑔(𝑛 − 1)
Disk n
Some definition
𝐵𝑖𝑔 𝑛 = 𝑛, … , 𝑛 − 𝑘 + 1
𝑆𝑚𝑎𝑙𝑙 𝑛 = 𝑛 − 𝑘 + 1, … , 1
𝐷𝑛 = 𝑛, … , 1
A move sequence 𝑃 of a disk set 𝐷 is called a packetmove of 𝐷 if it transfers the entire set 𝐷 from an initial
legal configuration on one peg to a final legal
configuration on another peg.
P|𝑆𝑚𝑎𝑙𝑙 𝑛 = omitting all moves of disks in 𝐷𝑛 ∖ 𝐵𝑖𝑔(𝑛)
from 𝑃
P|𝐵𝑖𝑔 𝑛 = omitting all moves of disks in 𝐷𝑛 ∖ 𝑆𝑚𝑎𝑙𝑙(𝑛)
from 𝑃
Poole’s algorithm (1992)
• 𝛼(𝑛)
• Perform 𝛽𝑛−1 (𝑠𝑜𝑢𝑟𝑐𝑒, 𝑎𝑢𝑥𝑖𝑙𝑖𝑎𝑟𝑦)
• Move disk 𝑛 from source to target
• Perform 𝛽𝑛−1 (𝑎𝑢𝑥𝑖𝑙𝑖𝑎𝑟𝑦, 𝑡𝑎𝑟𝑔𝑒𝑡)
• 𝛽(𝑛)
• If 𝑛 ≤ 𝑘, move all disks from source to target one by one
• Otherwise
•
•
•
Recursively perform 𝛽𝑛−𝑘 (𝑠𝑜𝑢𝑟𝑐𝑒, 𝑎𝑢𝑥𝑖𝑙𝑖𝑎𝑟𝑦)
Move disks in 𝐵𝑖𝑔(𝑛) from source to target one by one
Recursively perform 𝛽𝑛−𝑘 (𝑎𝑢𝑥𝑖𝑙𝑖𝑎𝑟𝑦, 𝑡𝑎𝑟𝑔𝑒𝑡)
Complexity
𝑛
 𝑏𝑛 =
2 ∗ 𝑏𝑛−𝑘 + 𝑘
 𝑏𝑛 = 𝑘 ∗ 2
𝑛
𝑘
𝑖𝑓 𝑛 ≤ 𝑘
𝑖𝑓 𝑛 > 𝑘
−1 +𝑟∗2
𝑛
𝑘
= 𝑘+𝑟 ∗2
𝑛
𝑘
−𝑘
 𝑟 = 𝑛 𝑚𝑜𝑑 𝑘
 Notice that the power of 𝑛 from the classical problem
decreases to the power of
𝑛
𝑘
Correctness & optimality of 𝛽𝑛
Fact: for 𝑛 > 𝑘, if some sequence of moves begins from a
configuration, where disk 𝑛 and 𝑆𝑚𝑎𝑙𝑙(𝑛) are gathered
on peg 𝑋, and finishes at a configuration, where disk 𝑛
and 𝑆𝑚𝑎𝑙𝑙(𝑛) are gathered on another peg 𝑌, then it
contains two disjoint packet-moves of 𝑆𝑚𝑎𝑙𝑙(𝑛): one
(from 𝑋) before the first move of disk 𝑛 and another
(to 𝑌) after its last move.
Hint: The disks in 𝑆𝑚𝑎𝑙𝑙(𝑛) are not allowed to be placed
below disk 𝑛 because the largest one of them is of size
𝑛 − 𝑘 and 𝑛 − 𝑛 − 𝑘 = 𝑘.
Correctness & optimality of 𝛽𝑛
Theorem: under the 𝑘-relaxed placement rule, the length of
any packet-move of 𝐷𝑛 is at least 𝑏𝑛
Proof: By a complete induction on 𝑛.
Basis: The case 𝑛 ≤ 𝑘 is trivial.
Induction step: For any 𝑛 > 𝑘, we consider an arbitrary
packet-move 𝑃 of 𝐷𝑛 , assuming the statement holds for all
lesser values of 𝑛.
By the fact above, P|𝑆𝑚𝑎𝑙𝑙(𝑛) contains two disjoint packetmoves of 𝑆𝑚𝑎𝑙𝑙(𝑛); by the induction hypothesis, their total
length is at least 2 ∗ 𝑏𝑛−𝑘 .
Every disk in 𝐵𝑖𝑔(𝑛) must move at least once, which sums to
at least k moves. Hence, 𝑃
= 𝑃 𝑆𝑚𝑎𝑙𝑙 𝑛 + 𝑝|𝐵𝑖𝑔 𝑛 | ≥ 2 ∗ 𝑏𝑛−𝑘 + 𝑘 = 𝑏𝑛
Optimality of 𝛼𝑛
 Poole suggested the following optimality proof for 𝛼𝑛 :
 Proof: Before the last move of disk 𝑛 from source to
target, all 𝑛 − 1 disks should be moved to auxiliary.
After that move of disk 𝑛, all 𝑛 − 1 disks should be
moved to target. As we proved, those transfers of 𝑛 − 1
disks using 𝛽𝑛 cannot take less than 𝑏𝑛 . Therefore, the
minimum possible number of moves is 2 ∗ 𝑏𝑛−1 + 1,
which is exactly the number of moves of 𝛼𝑛 .
Optimality of 𝛼𝑛 (Cont.)
 Poole assumed that before the unique move of disk 𝑛 to the
(empty) target peg, all other 𝑛 − 1 disks must be gathered
on the spare peg.
 This is not general, since there may be several moves of
disk n during the algorithm. Then, before the last move of
disk 𝑛, from some peg 𝑋 to the target peg, any of the disks
𝑛 − 1, 𝑛 − 2, … , 𝑛 − 𝑘 + 1 may be placed below disk 𝑛 on
peg 𝑋.
 As a result, Poole had overlooked a whole group of
algorithms which contained the above configuration.
 In the next hour, Carmel will give you a correct and
complete proof of Poole’s algorithm, suggested by Yefim
Dinitz and Shay Solomon.
A different strategy
 In order to get a sense of the group of algorithms Poole
has overlooked, lets examine the following strategy:
 Move 𝐷𝑛−𝑘 from 𝑠𝑜𝑢𝑟𝑐𝑒 to 𝑡𝑎𝑟𝑔𝑒𝑡 somehow
 Move disks 𝑛 − 𝑘 + 1, … , 𝑛 from 𝑠𝑜𝑢𝑟𝑐𝑒 to 𝑎𝑢𝑥𝑖𝑙𝑖𝑎𝑟𝑦
one by one
 Move 𝐷𝑛−𝑘 from 𝑡𝑎𝑟𝑔𝑒𝑡 to 𝑠𝑜𝑢𝑟𝑐𝑒 somehow
 Move disks 𝑛, … , 𝑛 − 𝑘 + 1 from 𝑎𝑢𝑥𝑖𝑙𝑖𝑎𝑟𝑦 to 𝑡𝑎𝑟𝑔𝑒𝑡 one
by one
 Move 𝐷𝑛−𝑘 from 𝑠𝑜𝑢𝑟𝑐𝑒 to 𝑡𝑎𝑟𝑔𝑒𝑡 somehow
A different strategy (Cont.)
 Some observations:
 Disk 𝑛 moves twice instead of once.
 When moving disk 𝑛 for the second and last time, it has
a number of other disks below it.
 The disks 𝑛 − 𝑘 + 1, … , 𝑛 arrive on 𝑡𝑎𝑟𝑔𝑒𝑡 in decreasing
order. So aiming to over perform 𝛼𝑛 , it would be enough
to find a sufficiently short triplet of consecutive packetmoves of 𝐷𝑛−𝑘 , which results in a 𝑝𝑒𝑟𝑓𝑒𝑐𝑡 configuration
of 𝐷𝑛−𝑘 on 𝑡𝑎𝑟𝑔𝑒𝑡.
 𝛼𝑛 contains four shortest packet-moves of 𝐷𝑛−𝑘−1 , so
this task does not seem hopeless.
• Some Applications
• Classical version
• Graphical representation
• Ants solve the problem
• Relaxed version
• Further research & References
Further research
 Is 𝛼𝑛 a unique optimal solution to 𝐵𝑇𝐻? If not,
what is the family of all optimal solutions? What is
their number?
 In the classic Tower of Hanoi problem, there are 𝑛
disks of different sizes, so that the exact size of
each disk is not significant. However, in 𝐵𝑇𝐻, it
plays an important role and influence the behavior
of the problem. It is interesting to allow the sizes
of the 𝑛 disks be an arbitrary set of distinct
integers.
Further research (Cont.)
 While the shortest perfect-to-perfect sequence of
moves had been found, we do not know what is
such a sequence for transforming one given (legal)
configuration to another given (legal) one, and
what is its length?
 In particular, what is the length of the longest one
among all shortest sequences of moves, over all
pairs of initial and final configuration?
References
 Optimality of an Algorithm Solving the Tower of
Hanoi Problem – Yefim Dinitz, Shay Solomon
 Optimization in a natural system: Argentine ants solve
the Towers of Hanoi – Chris R.Raid, David J. T.
Sumpter and Madeleine Beekman
 The Tower of Hanoi: A Bibliography - Paul K.
Stockmeyer