DFS and pruning

[Depth-first search]

Depth-first search, starting from the initial state, the predetermined rule to generate a search tree, looking at either one node, checks whether there is a target state, If it is not, in this state by using rules to generate the next layer any of a junction point, recheck process is repeated until a leaf node (i.e., node can generate a new state), when it is still not certain state, the back layer of the results may take an extended search of the other branch. The same approach has been adopted to proceed until you find the target state.

[Pruning]

Pruning is to reduce the size of the search tree, as a means to exclude search tree branch unnecessary as soon as possible. The image look as if cut off branches of the search tree, so called "pruning." In depth-first search, the following categories of common pruning method:

1. Optimize search order

  In some searching questions in each level of the search tree, between the various branches of the order is not fixed. Different search order will produce different search tree form, its size is also very different.

  In most cases, the search priority fewer branch node.

2. exclude equivalent redundancy

  In the search process, if we can determine from the current subtree node along the search tree reaches a few different branches are equivalent, then only one of which performs a search for the branch.

3. Feasibility pruning

  In the search process, the current status can be checked in time, if we find recursive branch has been unable to reach the border, on the implementation of backtracking. It's like when we were walking on the road, I saw the front is a dead end, you should immediately exhumation detour, not end of the road and back again.

  Some limit the scope of the subject condition is a range, when the pruning feasibility also called "pruning bounds."

4. optimality pruning

  In the search process optimization problems, if the cost of current spending has exceeded current search to the optimal solution, then no matter how good the strategy adopted to reach the border can not update the answer recursive. At this point you can stop searching for the current branch, perform back.

The memory of

  The search results may be recorded for each state, and returns the retrieved directly upon repeated traversing a state. This is like the time of our depth-first traversal of the map, mark whether a node has been visited.

【example】

  • Codeup question A: full array (entry): Click here
  • AcWing 842. aligned digital (entry): Click here
  • HDU 2553 N Queens problem (DFS pruning, multiple sets of sample playing table): Click here
  • AcWing 843. n- queens problem (output board): Click here
Published 844 original articles · won praise 135 · Views 150,000 +

Guess you like

Origin blog.csdn.net/qq_42815188/article/details/105100128