Sunday, March 14, 2010
Pokemon Roms Heart Gold
recursive algorithm
The Tower of Hanoi, often appearing as an example to illustrate the concept of recursion in computer programming courses, as there is a surprisingly simple recursive algorithm that solves it (in case anyone does not know, an algorithm is recursive if it calls itself in one of its steps.) Suppose we want to move the eight albums post A to post C. As the disc 8 is always the bottom, the only way is to move first seven-disc tower 1 ... 7 to pole B. Then we can take the disc 8 from A to C, and finally again have to move the tower 1 ... 7 runs from B to C.
But discs 1 ... 7 form a tower similar to the initial full well that in two of the three previous steps we face a problem similar to the original. In fact it can be solved in the same way, moving Now the tower 1 ... 6. For example, the first step (move the tower 1 ... 7 from A to B) can be decomposed into three steps.
Of course, in two of these three steps we meet again with the original problem, now with n = 6. The process is not infinite, since it comes in a tower that is equivalent to moving a single disc (this occurs when the tower is a single disc, of course). In summary, the recursive algorithm is as follows.
algorithm to move the tower 1 ... n X pole to pole Z, using as auxiliary pole Y.
1.-If n = 1, Disc 1 takes X to Z and ends. 2.-Move
Tower 1 ... n-1 using this algorithm, from X to Y, using as auxiliary Z. 3.-Take
disk n from X to Z. 4.-Move
Tower 1 ... n-1 using this algorithm, Y to Z, using as auxiliary X.
This algorithm has always seemed surprising, it seems the definition of a problem resolution. In reality, all it does is specify a few steps inevitable. For example, as we saw before, to solve the puzzle is required to bring the disk n from A to C, and it is compulsory to wear before tower 1 ... n B, etc. The sequence of motions produced by this algorithm is the only optimal solution (ie minimum length) as possible. The number of movements is M3 (n) = 2n-1, as can be shown easily by induction on the number records.
is true for n = 1
M3 (1) = 1 = 21-1.
If true for n, holds for d +1
In implementing the algorithm for n +1 calls itself twice for no more than a movement of the disk n +1. So queM3 (n +1) = 2 × M3 (n) + 1 = 2 × (2n-1) + 1 = 2n +1 = 2n +1-1 +1-2.
For n = 8 the number of movements is 28-1 = 255. For n = 64, 264-1 = 18,446,744,073,709,551,615. If the Brahmins of Benares site change a disc every second would take more than five hundred eighty billion years to complete their task, some forty times the age of Universe.
recursive algorithms work well with computers, but are difficult to apply to a human. If you try to solve the tower of eight disks using the method described is easy to miss unless you go taking notes of where to go. Even for computers recursive algorithms are often wasteful in the sense that they consume enough memory (and they also need. The alternative to recursive algorithms are iterative, in which no calls himself, but a or more loops. Very often there is no known or iterative alternative to a recursive algorithm, and when it is known, often much more complicated version recursively. However, in the case of the Tower of Hanoi, there are several very simple iterative algorithm.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment