Recursion
Tower of Hanoi (HANOI)
Proposition
There are three poles, the first root of the size of small to large total \ (n-\) a plate, requires the following three rules, all of the plates moved to a third pole on the first pole.
- You can only move a plate.
- You can only move the top of each pole plate.
- The plates on each large pole below, above small.
The minimum number of steps required problem.
example:
When \ (n = 3 \) , the moving method as shown in FIG.
The minimum number of movements of \ (7 \) , so \ (n = 3 \) Proposition solution is when \ (7 \) .
solve
Methods: Naming and solved
name
- Set \ (H (n) \) of \ (n-\) solution of a plate when the Towers of Hanoi.
- Three pole numbers are \ (A, B, C \) .
- The first \ (I \) layer plate as \ (H_i \) .
Solving
Obviously, \ (H (. 1). 1 = \)
Observation available, the \ (n-\) a plate from \ (A \) is moved to the \ (B \) corresponds to \ (h_1, h_2 \ cdots h_ {n-1} \) to move \ (B \) after the \ (H_n \) move \ (C \) , then \ (h_1, h_2 \ cdots h_ {n-1} \) move \ (C \) .
By definition, the \ (h_1, h_2 \ cdots h_ {n-1} \) from \ (A \) Move \ (B \) for an \ (H (n-1) \) step.
\(\therefore H(n)=2H(n-1)+1\qquad H(1)=1\)
test
Known \ (H (3) = 7 \)
\[\because H(n)=2H(n-1)+1\]
\[\therefore H(3)=2\times H(2)+1\]
\[\therefore H(3)=2\times (2\times H(1) + 1) + 1\]
\[\therefore H(3)=2\times (2\times 1+1)+1\]
\[\therefore H(3)=2\times 3+1\]
\[\therefore H(3)=7\]
Code
# python
A = [3, 2, 1]
B = []
C = []
def move(n, source, target, auxiliary):
if n > 0:
# Move n - 1 disks from source to auxiliary, so they are out of the way
move(n - 1, source, auxiliary, target)
# Move the nth disk from source to target
target.append(source.pop())
# Display our progress
print(A, B, C, '##############', sep='\n')
# Move the n - 1 disks that we left on auxiliary onto target
move(n - 1, auxiliary, target, source)
# Initiate call from source A to target C with auxiliary B
move(3, A, C, B)
Recursive
Recursive solution method is to call their own in the calculation.
Recursive configuration conditions should have,
Sub-problems to be the original problem is the same problem, and easier.
Can not be unlimited calling itself, must have borders, simplify the situation for non-recursive processing.
Tower of Hanoi is a typical formula for solving recursive.
Enclosed
In the previous recursion formula, easy to see, calculating \ (H (n) \) required \ (n-1 \) times the \ (H (n) \) is replaced with \ (2H (n-1) + 1 \) operations.
Therefore, although the recursive intuitive, but is not convenient calculation.
Closed can directly calculate the value of the function, need not be recursive.
We will try to convert the Tower of Hanoi recursive closed.
Act 1
Solving
\[\because H(n)=2H(n-1)+1\]
\[\therefore H(n)=4H(n-2)+3\]
\[\therefore H(n)=8H(n-3)+7\]
\[\vdots\]
\[\therefore H(n)=2^{n-1}H(1)+2^{n-1}-1\]
\[\therefore H(n)=2^{n-1}+2^{n-1}-1\]
\[\therefore H(n)=2^n-1\]
test
Known \ (H (3) = 7 \)
\[\because H(n)=2^n-1\]
\[\therefore H(3)=2^3-1\]
\[\therefore H(3)=7\]
Act 2
Solving
\ [\ because H (n)
= 2H (n-1) \] conjecture:
\ [H (n-). 1 = 2-n-^ \]
demonstrated:
\ [n = 1, when the I clearly established \.]
\ [. II established Suppose n = k, i.e., H (k) = 2 ^ k-1, then: \]
\[\because H(n)=2H(n-1)+1\]
\[\therefore H(n+1)=2H(n)+1\]
\[\because H(n)=2^k-1\]
\[\therefore H(n+1)=2\cdot (2^k-1)-1\]
\[\therefore H(n+1)=2^{k+1}-2+1\]
\[\therefore H(n+1)=2^{k+1}-1\]
QED.