Divide and conquer algorithm and the Towers of Hanoi

Divide and Conquer Algorithm

  1. Divide and conquer is an important algorithm. Literal interpretation is "divide and rule" is to a complex problem into two or more of the same or similar sub-problems , then sub-problems into smaller sub-problems ...... until the last sub-problems can be simply solved directly the combined solution is the solution that is the problem child of the original problem. This technique is the basis for many efficient algorithms, such as sorting algorithm (quick sort, merge sort), Fourier transform (Fast Fourier Transform) ......
  2. Some of the classic divide and conquer algorithm can solve the problem of

    Binary search
    large integer multiplication
    chessboard covering
    merge sort
    quick sort
    of linear time to choose
    the closest point on the question
    round robin schedule
    HANOR

  • Partition algorithm steps
    divide and conquer has three steps on each layer recursion:
  1. Decomposition : the original problem into a number of smaller, independent of each other, the same as the original problem in the form of sub-problems
  2. Solve : if the smaller sub-problems easily solved directly addressed, recursive solution to the various sub-problems
  3. Merge : The de-merger of each sub-problem solutions for the original problem
  • Partition (Divide-and-Conquer§) following algorithm design pattern

    IF | P | ≤n0
    the then return (ADHOC§)
    // P will be broken down into smaller sub-problems P1, P2, ..., Pk
    for i ← 1 to k
    do yi ← Divide-and-Conquer (Pi) recursively resolve Pi
    T ← mERGE (y1, y2, ..., yk) merge sub-problem
    return (T)

    Where | P | P represents the scale of the problem; n0 is a threshold value that indicates the time when the scale of the problem P does not exceed n0, the problem has been solved directly and easily, without having to continue to break down. ADHOC§ is the basic algorithm of the sub-divide and conquer, direct solution for the problems of small-scale P. Therefore, when the size of solving ADHOC§ P does not exceed n0 directly algorithms. Algorithm MERGE (y1, y2, ..., yk) is a sub-algorithm of the combined sub-therapeutic method for subproblems P is P1, P2, ..., y1 Pk of the respective solution, y2, ..., yk is the combined P solution.

Tower of Hanoi problem

  • Tower of Hanoi Legend

    Tower of Hanoi: Tower of Hanoi (also known as the Tower of Hanoi) problem stems from an ancient Indian legend of educational toys. When Brahma created the world to do three diamond pillars, from the bottom up pile with 64 gold discs in order of size to a post. Brahma Brahman command to the disk in order of size from the bottom again placed on the other pillars. And predetermined, the disc can not be enlarged in a small disk, a disk can only be moved between the three pillars.

    If once per second, totaling How long does it? Shift completion of these gold pieces require more than 584.554 billion years, life expectancy of the solar system is also said to be tens of billions of years. Really over 584.554 billion years, all life on Earth, along with the Vatican towers, temples, etc., have already vanished.

Tower of Hanoi legend above said actual is the problem, as shown:
Here Insert Picture Description
Requirements :
disk 1. A column B column or C all moved to the column
can only be moved between a disk 2. The three columns
2. small round disc on the disc can not be magnified

We look at ideas to solve the Towers of Hanoi problem :

According to the idea of ​​divide and conquer algorithm, we split into several complex problems easier to solve minor problems.

  1. We can assume that A tower on only one disk, the disk directly to the A tower moved to the C: AC

  2. A column puck assumed that the number n> = 2, we can always be seen as two disks, i.e., a lowermost disk, and the remaining above disc, as a disc

    1. The uppermost first disc A-> B
    2. In the lowermost plate A-> C
    3. Finally, all the B column from the disc B-> C

    As can be seen from the above, no matter how many disks, as long as his number is greater than 1, we can be divided into three above.

Code implementation :
Tower of Hanoi code is very simple, the key is to understand his thinking

public class Hanoitower {

    public static void main(String[] args) {
        hanoitower(5, 'A', 'B', 'C');
    }

    /**
     * @param num 盘的数量
     * @param a a柱
     * @param b b柱
     * @param c c柱
     */
    public static void hanoitower(int num, char a, char b, char c) {
        // 如果只有一个盘,直接从A移动到C柱
        if (num == 1) {
            System.out.println("第一个盘从" + a + "->" + c);
            return;
        }
        if (num >= 2) {
            // 如果大于或者等于2个盘,我们看把盘看成最下面的一个盘,和上面的盘
            // 1. 先移动上面的盘到B即,A->B
            hanoitower(num - 1, a, c, b);
            // 2. 在把最下面的一个盘从A移动到C
            System.out.println("第" + num + "个盘从" + a + "->" +  c);
            // 3. 最后把B中的盘移动到C
            hanoitower(num - 1, b, a, c);
        }
    }
}
Published 83 original articles · won praise 3 · Views 9818

Guess you like

Origin blog.csdn.net/fyj13925475957/article/details/104745821