Divide and conquer algorithm-solve the Tower of Hanoi problem

1: Introduction to divide and conquer algorithm

  • Divide and conquer is a very important algorithm. The literal explanation is "divide and conquer", that is, to 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)...
  • The problems that can be solved by divide and conquer generally have the following characteristics:

The problem can be easily solved by reducing the size of the problem to a certain extent;
the problem can be decomposed into several smaller-scale identical problems, that is, the problem has the optimal substructure property
, and the solutions of the subproblems decomposed by the problem can be combined It is the solution of
the problem ; the sub-problems decomposed by the problem are independent of each other, that is, the sub-problems do not contain common sub-problems.

This feature relates to the efficiency of the divide and conquer method. If the sub-problems are not independent, the divide and conquer method will do a lot of unnecessary work to solve the public sub-problems repeatedly. At this time, although the divide and conquer method can also be used, it is generally It is better to use dynamic programming.

  • 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 calendar
    Tower of Hanoi

2: Basic steps of divide and conquer algorithm

  • The divide-and-conquer method has three steps at each level of recursion:
    Decomposition : Decompose the original problem into several smaller, independent sub-problems
    with the same form as the original problem Solve: if the sub-problems are small and easy to solve Solve directly, otherwise solve each sub-problem recursively.
    Merge: merge the solution of each sub-problem into the solution of the original problem

3: Design idea of ​​divide and conquer algorithm

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(P) 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, the algorithm ADHOC(P) is used to solve it directly. The algorithm MERGE(y1,y2,...,yk) is the merge sub-algorithm in the divide and conquer method, which is used to merge the corresponding solutions y1,y2,...,yk of the subproblems P1,P2,...,Pk into P Solution.

People have found from a lot of practice that when designing algorithms with divide and conquer, it is best to make the sub-problems roughly the same size. The method of dividing a problem into k sub-problems of equal size is effective. This approach of making sub-problems roughly equal in size is derived from the idea of ​​balancing sub-problems, which is almost always better than the approach of unequal sub-problems.

4: Divide and conquer algorithm to solve the Tower of Hanoi problem

The legend of the
Tower of Hanoi The 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:

If there is a disk, A->C

If we have n >= 2, we can always regard it as two disks 1. The bottom disk 2. The upper disk

First, the top disk A->B,
the bottom disk A->C
, and all the disks of Tower B from B->C
are explained in the following figure.
Insert picture description here
I can get the following code

public class Hanoitower {
    
    

	public static void main(String[] args) {
    
    
		hanoiTower(3, 'A', 'B', 'C');
	}
	
	//汉诺塔的移动的方法
	//使用分治算法
	
	public static void hanoiTower(int num, char a, char b, char 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/qq_44891295/article/details/105908605