Chapter V algorithm work - Backtracking

First, the understanding of backtracking

  Backtracking is found from the equivalent of a full array of the optimal solution, but not just full array traversal solution space tree issues, the depth-first search traversal starting from the root down through the middle of constraint functions pruning and bounding function, reduce the number of traversal, traverse down if the conditions are not met, it will traverse the solution space back on the tree, or to continue down the depth-first search for the optimal solution.

Second, the subset sum problem

  

 

   Solution space is (1, 1, 1, 0, 0), which is represented by a vector, this number represents a selected, 0 is not selected.

  Constraint function: sum + s [t] <= c. t is the current node to traverse, sum the current subset, s [t] is the value of the current node.

  code show as below:

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  #define MAX 1000
 . 4  
. 5  int n-;                    // set size 
. 6  int C;                    // target 
. 7  int S [MAX];               // set 
. 8  int SUM = 0 ;              / / current and 
. 9  int X [MAX] = { 0 };       // record subset 
10  int In Flag = 0 ;             //Whether the record to find a viable solution 
. 11  
12 is  void BackTrack ( int T) {
 13 is      IF (In Flag == . 1 ) return ;   // find a solution returns 
14      IF (T> n-) {
 15          // recording or output feasible solution 
16          IF ( && C == SUM! In Flag) {
 . 17              In Flag = . 1 ;
 18 is              for ( int I = . 1 ; I <= n-; I ++ )
 . 19                  IF (X [I] == . 1 )
 20 is                      COUT << S [I] << " " ;
 21 is              COUT << ' \ n- ' ;
 22 is              return ;
 23 is          }
 24      }
 25      the else {
 26 is          IF (SUM + S [T] <= C) {
 27              X [T] = . 1 ;
 28              SUM = + S [T ];
 29              BackTrack (T + . 1 );   // traverse the left subtree 
30              SUM - = S [T];
 31 is              X [T] = 0 ;
 32          }
 33 is         BackTrack (T + . 1 );    // traverse right subtree 
34 is      }
 35  }
 36  
37 [  int main () {
 38 is      CIN >> >> n- C;
 39      int A = 0 ;
 40      for ( int I = . 1 ; I <= n-; I ++ ) {
 41 is          CIN >> S [I];
 42 is          A + = S [I];
 43 is      }
 44 is      IF (A <C)    // if the set of all elements and are smaller than the target value, then certainly no solution 
45          COUT << " No Solution! \ the n-" ;
 46 is      the else {
 47          BackTrack ( . 1 );
 48          IF (! = In Flag . 1 )    // after backtracking solution is still not found 
49              COUT << " No Solution! " << ' \ n- ' ;
 50      }
 51 is      System ( " PAUSE " );
 52 is      return  0 ;
 53 is }

Third, learning problems and forces in programming issues

  I feel that learning backtracking when the most rare is the selection function and the constraints bounding function, but these two really little difficult, these two functions once did not do a good job, it will lead to the algorithm timeout, the end result will lead to error.

  We knot programming team did not encounter difficult problems, constraints on my teammates to understand the function of this one is better, always help me in programming, very good.

Guess you like

Origin www.cnblogs.com/jewfer-03-08/p/12060830.html