• Non ci sono risultati.

1 The Towers of Hanoi

N/A
N/A
Protected

Academic year: 2021

Condividi "1 The Towers of Hanoi"

Copied!
5
0
0

Testo completo

(1)

Recurrences I

This is the first of two lectures about solving recurrences and recurrent problems.

Needless to say, recurrent problems come up again and again. In particular, recurrences often arise in the analysis of recursive algorithms.

1 The Towers of Hanoi

In the Towers of Hanoi problem, there are three posts and seven disks of different sizes.

Each disk has a hole through the center so that it fits on a post. At the start, all seven disks are on post #1 as shown below. The disks are arranged by size so that the smallest is on top and the largest is on the bottom. The goal is to end up with all seven disks in the same order, but on a different post. This is not trivial because of two restrictions. First, the only permitted action is removing the top disk from a post and dropping it onto another post.

Second, a larger disk can never lie above a smaller disk on any post. (These rules imply, for example, that it is no fair to pick up the whole stack of disks at once and then to drop them all on another post!)

Post #1 Post #2 Post #3

It is not immediately clear that a solution to this problem exists; maybe the rules are so stringent that the disks cannot all be moved to another post!

One approach to this problem is to consider a simpler variant with only three disks.

We can quickly exhaust the possibilities of this simpler puzzle and find a 7-move solution such as the one shown below. (The disks on each post are indicated by the numbers immediately to the right. Larger numbers correspond to larger disks.)

(2)

12

3 ) 2

3 1 )

3 1 2

) 1

3 2 ) 1

3 2

) 1 3 2 ) 2

1 3

)

1 23

This problem was invented in 1883 by the French mathematician Edouard Lucas. In his original account, there were 64 disks of solid gold. At the beginning of time, all 64 were placed on a single post, and monks were assigned the task of moving them to another post according to the rules described above. According to legend, when the monks complete their task, the Tower will crumble and the world will end!

The questions we must answer are, “Given sufficient time, can the monks succeed?”

and if so, “How long until the world ends?” and, most importantly, “Will this happen before the 6.042 final?”

1.1 Finding a Recurrence

The Towers of Hanoi problem can be solved recursively as follows. Let Tn be the min- imum number of steps needed to move an n-disk tower from one post to another. For example, a bit of experimentation shows that T1= 1and T2= 3. For 3 disks, the solution given above proves that T3  7. We can generalize the approach used for 3 disks to the following recursive algorithm for n disks.

Step 1. Move the top n 1 disks from the first post to the third post. This can be done in Tn 1 steps.

12 ...

n

)

12 ...

n n 1

(3)

Step 2. Move the largest disk from the first post to the to the second post. This requires just 1 step.

12 ...

n n 1

)

12 ...

n n 1

Step 3. Move the n 1 disks from the third post onto the second post. Again, Tn 1steps are required.

1 2...

n n 1

)

1 2...

n

This algorithm shows that Tn, the number of steps required to move n disks to a dif- ferent post, is at most 2Tn 1+ 1. We can use this fact to compute upper bounds on the number of steps required for various numbers of disks:

T3 2 · T2+ 1

= 7

T4 2 · T3+ 1

 15

The algorithm described above answers our first question: given sufficient time, the monks will finish their task and end the world. (Which is a shame. After all that effort they’d probably want to smack a few high-fives and go out for burgers and ice cream, but nope— world’s over.)

1.2 A Lower Bound for Towers of Hanoi

We can not yet compute the exact number of steps that the monks need to move the 64 disks; we can only show an upper bound. Perhaps— having pondered the problem since the beginning of time— the monks have devised a better algorithm.

In fact, there is no better algorithm, and here is why. At some step, the monks must move the n-th disk from the first post to a different post. For this to happen, the n 1 smaller disks must all be stacked out of the way on the only remaining post. Arranging the n 1 smaller disks this way requires at least Tn 1 moves. After the largest disk is moved, at least another Tn 1moves are required to pile the n 1 smaller disks on top.

(4)

This argument shows that the number of steps required is at least 2Tn 1+ 1. Since we gave an algorithm using exactly that number of steps, we now have a recurrence for Tn, the number of moves required to complete the Tower of Hanoi problem with n disks:

T1= 1

Tn= 2Tn 1+ 1 (for n 2)

We can use this recurrence to conclude that T2 = 3, T3= 7, T4= 15, . . ..

1.3 Guess-and-Verify

Computing T64 from the recurrence would require a lot of work. It would be nice to have a closed form expression for Tn, so that we could quickly compute the number of steps required to solve the Towers of Hanoi problem for any given number of disks. (For example, we might want to know how much sooner the world would end if the monks melted down one disk to purchase burgers and ice cream before the end of the world.)

There are several different methods for solving recurrences. The simplest method is to guess the solution and then to verify that the guess is correct, usually with an induction proof. This method is called guess-and-verify or “substitution”. As a basis for a good guess, let’s tabulate Tnfor small values of n:

n Tn

1 1 2 3 3 7 4 15 5 31 6 63

Based on this table, a natural guess is that Tn= 2n 1.

Whenever you guess a solution to a recurrence, you should always verify it with a proof by induction or by some other technique; after all, your guess might be wrong. (But why bother to verify in this case? After all, if we’re wrong, its not the end of the. . . no, let’s check.)

Claim. If:

T1= 1

Tn = 2Tn 1+ 1 (for n 2)

then:

Tn= 2n 1

(5)

Proof. The proof is by induction on n. Let P (n) be the proposition that Tn= 2n 1.

Base case: P (1) is true because T1= 1 = 21 1.

Inductive step: Now we assume Tn = 2n 1to prove that Tn+1= 2n+1 1, where n 1.

Tn+1= 2Tn+ 1

= 2(2n 1) + 1

= 2n+1 1

The first equality is the recurrence relation, and the second equation follows by the as- sumption P (n). The last step is simplification.

Our guess is now verified. This shows, for example, that the 7-disk puzzle will require 27 1 = 127moves to complete. We can also now resolve our remaining questions about the 64-disk puzzle. Since T64 = 264 1, the monks must complete more than 18 billion billion steps before the world ends. Better study for the final.

1.4 The Plug-and-Chug Method

In general, guess-and-verify is a great way to solve recurrences. The only problem with the method is guessing the right solution. This was easy in the Towers of Hanoi example, but sometimes the solution has a strange form that is quite hard to guess. Practice helps, of course, but so can some other methods.

Plug-and-chug is one such alternative method for solving recurrences. Plug-and-chug is also sometimes called “expansion”, “iteration”, or “brute force”. The method consists of four calculation-intensive steps. These are described below and illustrated with the Tower of Hanoi examle.

Step 1: Plug and Chug

Expand the recurrence equation by alternately “plugging” (applying the recurrence equa- tion) and “chugging” (simplifying the resulting expression).

Tn= 1 + 2Tn 1

= 1 + 2(1 + 2Tn 2) plug

= 1 + 2 + 4Tn 2 chug

= 1 + 2 + 4(1 + 2Tn 3) plug

= 1 + 2 + 4 + 8Tn 3 chug

= 1 + 2 + 4 + 8(1 + 2Tn 4) plug

= 1 + 2 + 4 + 8 + 16Tn 4 chug

Riferimenti

Documenti correlati

Government Printing Office , Social Security Administration (US)..

Two families of non-overlapping coercive domain decomposition methods are proposed for the numerical approximation of advection dominated advection-di usion equations and

Solution proposed by Roberto Tauraso, Dipartimento di Matematica, Universit`a di Roma “Tor Vergata”, via della Ricerca Scientifica, 00133 Roma,

[r]

Moreover, if we remove from G all the edges of a single path, then the number of vertices of odd degree decreases at most by two (namely the vertices at the start and at the end of

The walk begins at vertex 0 and continues until every vertex has been visited and the walk returns to vertex 0.. Indeed the already visited verteces are contiguous, the starting

(American Mathematical Monthly, Vol.119, November 2012) Proposed by Mircea Merca (Romania). Let p be the Euler partition function, i.e., p(n) is the number of nondecreasing lists

The experimental using CFR engine and using fuel gasoline in the market with blended bioethanol start from 5% -20% and analysis the relationship octane number after