DFS and pruning techniques
First, the basic idea of DFS
The basic idea of the depth-first search is:
In order to obtain the solution of the problem, to explore possibilities to select one of forward, in the process of exploring, and found that a choice is wrong, it is a step back to re-select, continue to explore forward, so repeatedly, until a proof solution or no solution
Two, DFS algorithm framework
A framework
int DFS ( int K) { for ( int I = . 1 ; I <= operators that species; I ++ ) IF (condition is satisfied) { save the result; IF (to destination) output solution; the else DFS (K + . 1 ); Recovery : saving a state before the backtracking step results {}; } }
Frame two
int DFS ( int K) { IF (to destination) output solution; the else for ( int I = . 1 ; I <= operators that species; I ++ ) IF (condition satisfied) { save the result; DFS (K + . 1 ); recovery: saving a state before the backtracking step results {}; } }
Third, what is pruning
The search process can be seen from the root, traverse the inverted tree a search tree, the so-called pruning, as the name suggests, is through some kind of judgment, avoid unnecessary traversal process , figuratively speaking, that is, cut the tree in search of some "branches", said pruning
Fourth, the principle of pruning
1. validity
2. Accuracy
3. efficiency
tracing, which is like when we were walking on the road, saw the front is a dead end should immediately exhumation detour rather than go
to the end of the road and then return
some of the topics range limit is a range of conditions, this time pruning feasibility also called "upper and lower bounds pruning."
in the search process optimization problems, if the cost of the current cost of the current search to the optimal solution has been exceeded, then no matter adopted
to take how good recursive strategy to reach the border, you can not update answer. At this point you can stop searching for the current branch, perform
back
The memory of the
can record the state of each search result, in direct repeat the retrieval and return when traversing a state, which is like FIG we feed
when the depth-first traversal of the row, a flag whether the node has been accessed
Six examples
1. 1440: divide the number of [example 1]
2. 1441: example [2] birthday cake
3. 1442: [3] example a small stick
4. 1443: [example 4] Addition Chains