Binary Search: Successful Case (Element Found in The List)
Binary Search: Successful Case (Element Found in The List)
NOTE: A new tower will be created with every move. Consequently, the pegs
which we initially called the source, auxiliary and destination pegs will constantly
change.
Preliminary Observations
Let q equal to the minimum number of moves necessary to
complete the relocation. Examining the case when d = 3 we
find q = 7.
d=4 q = 15
d=5 q = 31
d=6 q = 63
d=7 q = 127
d=8 q = 255
Can you see the pattern?
http://www.cut-the-knot.org/recurrence/hanoi.html
Observations Continued
The following equation gives q for any d:
q 2d 1
7
More Observations
• When looking at the case of d = 3 the first move must be
made to the destination peg to ensure a solution with minimal
q.
• Similarly, for d = 4 the first move must be made to the
auxiliary peg.
• It will be the case that for odd d the first move must always
be made to the destination peg. For even d the first move
must always be made to the auxiliary peg.
Recurrence Relations
Another way of approaching this problem is to look at the
first d – 1 disks. The goal would then be to first relocate
those disks to the auxiliary peg. Next one would simply
move the last disk to the destination peg. Finally, the solution
would be achieved by relocating the d – 1 disk tower to the
destination peg. This method gives rise to the notion of
recurrence.
0 Move Sequence for d = 3
1
7
Recurrence Relations Continued
Consider T(d), where T(d) = q and d = 3 and q = 7. We know
T(3) = 7. Using the recurrence relation we can see that
T (3) 2 T (3 1) 1
In this example T(3-1) is the number of moves to relocate the
top two disks. Clearly this must be done twice: once to
uncover the largest disk and again to re-cover it, therefore it is
multiplied by two. The “+1” comes from the single move of
the largest disk from the source to destination peg.