DFS 1440: [1] a few examples division of 1441: [2] birthday cake example 1442: [3] example small wooden 1443: example [4] Addition Chains

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

 

 

 Fifth, the optimization techniques of depth-first search
 
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 varies greatly
 
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 the branch where the search is performed
 
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 back
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."
 
4. optimality 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

 

Guess you like

Origin www.cnblogs.com/xiaoyezi-wink/p/10991527.html