Java programming: divide and conquer algorithm

Introduction to divide and conquer algorithm

  1. Divide and conquer is a very important algorithm. The literal explanation is "divide and conquer", that is, divide a complex problem into two or more identical or similar sub-problems, and then divide the sub-problems into smaller sub-problems... until the final sub-problems can be solved simply and directly , The solution of the original problem is the combination of the solutions of the sub-problems. This technique is the basis of many efficient algorithms, such as sorting algorithms (quick sort, merge sort), Fourier transform (fast Fourier transform)...
  2. Some classic problems that can be solved by the divide-and-conquer algorithm
    ① Binary search
    ② Large integer multiplication
    ③ Checkerboard coverage
    ④ Merge sort
    ⑤ Quick sort
    ⑥ Linear time selection
    ⑦ Closest point pair problem
    ⑧ Round robin schedule
    ⑨ Tower of Hanoi

Basic steps of divide and conquer algorithm

The divide and conquer method has three steps at each level of recursion:

  1. Decomposition: Decompose the original problem into several smaller, independent sub-problems in the same form as the original problem
  2. Solve: if the sub-problem is small and easy to solve, solve it directly, otherwise solve each sub-problem recursively
  3. Combine: Combine the solutions of each sub-problem into the solution of the original problem.

The divide and conquer (Divide-and-Conquer§) algorithm design pattern is as follows:

Insert picture description here
Where |P| represents the scale of the problem P; n0 is a threshold, which means that when the scale of the problem P does not exceed n0, the problem can be solved directly without further decomposition. ADHOC§ is the basic sub-algorithm in the divide-and-conquer method, used to directly solve small-scale problems P. Therefore, when the scale of P does not exceed n0, it is directly solved by the algorithm ADHOC§. The algorithm MERGE(y1,y2,...,yk) is the merge sub-algorithm in the divide-and-conquer method, used to merge the corresponding solutions y1,y2,...,yk of the subproblems P1,P2,...,Pk into P Solution.

Best Practice of Divide and Conquer Algorithm-Tower of Hanoi

The legend of the Tower of Hanoi

Tower of Hanoi: The Tower of Hanoi (also known as the Tower of Hanoi) is an educational toy derived from an ancient Indian legend. When Brahma created the world, he made three diamond pillars. On one pillar, 64 golden discs were stacked in order of size from bottom to top. The Brahma ordered the Brahmin to re-place the disc on another pillar in order of size from below. It also stipulates that the disk cannot be enlarged on the small disk, and only one disk can be moved at a time between the three pillars.
If once every second, how long does it take? It takes more than 584.554 billion years to move these gold pieces, and the life expectancy of the solar system is said to be tens of billions of years. After 584.554 billion years, all life on earth, including the Vatican Pagodas and temples, have long since disappeared.

Demo and analysis of the Tower of Hanoi game:

  1. If there is a disk, A->C
  2. If we have n >= 2, we can always regard it as two disks 1. The bottom disk 2. The upper disk
    ① First put the top disk A->B
    ② Put the bottom disk A-> C
    ③ Change all the disks of Tower B from B->C

Code

package dac;

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

    // 汉诺塔移动方案——分治算法
    public static void hanoiTower(int num, char a, char b, char c) {
    
    
        // 如果只有一个盘,直接从a移动到c
        if (num == 1) {
    
    
            System.out.println("第1个盘从 " + a + "->" + c);
        } else {
    
    
            // 如果我们有n>=2情况,我们总是可以看做是两个盘 1)最下边的一个盘 2)上面的盘
            // 1. 先把最上面的盘A->B,中间的移动过程会使用到c塔
            hanoiTower(num - 1, a, c, b);
            // 2. 把最下面的盘A->C
            System.out.println("第" + num + "个盘从 " + a + "->" + c);
            // 3. 把B塔所有的盘从B->C,中间的移动过程使用到a塔
            hanoiTower(num - 1, b, a, c);
        }
    }
}

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/109064937