"Algorithm"-backtracking algorithm

1 Introduction

\quad \quad The backtracking algorithm is actually based on a DFS (depth first search) search process similar to enumeration. It is mainly to find the solution of the problem during the search attempt. When it is found that the solution condition is not met, it “backtracks” to return and try Other paths. The backtracking method is a kind of optimal search method, which searches forward according to the optimal conditions to achieve the goal. But when you explore a certain step, you find that the original choice is not good or the goal is not achieved, so you will go back one step and choose again. This technique of going back and going again is the backtracking method, and the point of a certain state that satisfies the backtracking condition This is called the "backtrack point." Many complex and large-scale problems can use the backtracking method, which has the reputation of "universal problem-solving method".

The difference between DFS and backtracking algorithm

\quad \quad DFS is constantly searching in a certain direction until it reaches the lowest level, while the backtracking algorithm is based on DFS, but the difference is that in the search process, after the end condition is reached, the state is restored, backtracking to the previous level, and again search for. Therefore, the difference between the backtracking algorithm and DFS is whether there is a state reset. The depth-first generation method with bounding function is called backtracking method. (Backtracking method = exhaustive + pruning)

2. The basic idea

\quad \quad In simple terms, the backtracking method is to exhaust all possible algorithms in the order of depth first, but the better part of the backtracking algorithm than the brute-force exhaustive method is that the backtracking algorithm can determine whether the current state meets the conditions of the problem at any time. Once the conditions are not met, then return to the previous state, saving the time to continue to explore.

\quad \quad In other words, the backtracking method can be understood as finding the destination by choosing different forks, one fork at a time, and one fork at a time to try to find the destination. If you take the wrong road, continue to return to find another road that forks the road until you find your destination. It saves time to go down the wrong road.

3. Basic process and solution steps

\quad \quad In order to avoid generating the problem state that cannot produce the best solution, the backtracking method must continuously use the bounding function to execute (prune) those live nodes that are actually impossible to produce the required solution, so as to reduce the calculation of the problem. the amount.

Two commonly used pruning functions:

  • Constraint function: subtract the number of children that do not meet the constraint at the expansion node
  • Bound function: subtract the subtree that does not get the optimal solution

The general steps to solve the problem with backtracking method:

(1) Backtracking exit : For the given problem, determine the solution space of the problem:
(2) Backtracking subject : Determine the extended search rule of the node: traverse all the child nodes of the current state and determine whether the next state is satisfied For problem conditions, if the problem conditions are met, then enter the next state.
(3) State return : Search the solution space in a depth-first manner, and use the pruning function in the search process (if the current state does not meet the conditions, then return to the previous state.) to avoid invalid search.

Backtracking function universal template

def backtrack ( 参数 ):

	#回溯出口
	if ( 满足题意了 ):
		计数或进行其他操作
		return True   #有时可以省略
	
	#回溯主体
	for( 查找当前节点的周围的节点 )
		进行其他的操作;
		标记已经搜索过的节点
		backtrack( 下一次搜索的节点 )
	#状态返回
		取消标记;
	return False	#有时可以省略

4. Applicable conditions

That is, the characteristics of the backtracking method (to judge when the backtracking algorithm should be used)

1. The answer to the question has multiple elements

2. The answer must satisfy some constraints

3. The way to find the answer is the same in every step

If a problem is a search-to-solve problem, and the solution of the problem is a tree structure (constantly expanding vector), the problem can consider using the backtracking algorithm.

5. Classic example

Such as n queens, the coloring of graphs, subsetting, the arrangement of strings and so on.

Types of backtracking algorithm questions:

  • Subset and combination problems
  • Permutation problem
  • Search, N Queens Questions,

Guess you like

Origin blog.csdn.net/weixin_45666566/article/details/113795337