Backtracking - subset sum problem

(1) Description of the problem: a subset of the problem and for instance <data, num>. {Wherein X = Data . 1 , X 2 , ......, X n- } is a set of positive integers, targetValue is a positive integer. Determining a subset sum problem if there data1 subset of data, such that

x1 + x2 + ...... + xn = targetValue (x € data1)

(2) designing the algorithm: backtracking algorithm is used to solve the subset of the tree, for a given set {X = data . 1 , X 2 , ......, X n- } targetValue and positive integers, calculating a subset of data data1 satisfy [X . 1  + X 2  + ...... + X n-  = targetValue (X € DATAl)]

(3) algorithm code:

public class SubsetSum {

    /**
     * Target
     */
    private static Integer targetValue;

    /**
     * Sum of the currently selected element
     */
    private static Integer sum = 0;

    /**
     * The number of data
     */
    private static Integer num;

    /**
     * Undetermined value
     */
    private static Integer indeterminacyValue = 0;

    /**
     * Data array
     */
    private static Integer[] data;

    /**
     * Data storage [0: 1 storage: Store]
     */
    private static Integer[] store;

    /**
     * Initialization data
     */
    private static void initData() {
        Scanner input = new Scanner(System.in);
        System.out.println ( "Please enter the target:" );
        targetValue = input.nextInt();
        
        System.out.println ( "Please enter the number of data:" );
        num = input.nextInt();

        data = new Integer[num];
        store = new Integer[num];
        System.out.println ( "Please enter the respective number:" );
         for ( int I = 0; I <data.length; I ++ ) {
            data[i] = input.nextInt();
            Store [I] = 0;                // initializes not stored 
            indeterminacyValue + = Data [I];
        }
    }

    /**
     * Find backtracking
     * / 
    Private  static Boolean BackTrack ( int I) {
         IF (SUM == targetValue) {    // find a feasible solution, directly back to true 
            return  to true ;
        }
        IF (data.length == I) {      // can not find a feasible solution, directly back to false 
            return  to false ;
        }
        indeterminacyValue - = Data [I];                   // calculate the sum of the number has not been determined 
        IF (sum + data [i] <= targetValue ) {              // current sum + data [i] <= targetValue directly into the left subtree 
            store [i ] = 1;                                // data i stored, included in the selected addend 
            SUM + = data [i];
             IF (BackTrack (i + 1)) {                   // layer depth determination continues summing 
                return  to true ;
            }
            sum - = Data [I];                              // solve depth completed, the solution required not satisfied, go back needs to restore the current start value sum 
        }
         IF (sum + indeterminacyValue> = targetValue) {   // pruning function if [ the current value + undetermined sum> = target, before entering the right subtree depth search; otherwise it has no meaning] 
            Store [i] = 0;                                // data is not stored i this time, included in the selected addend 
            if (BackTrack (I +. 1 )) {
                 return  to true ;
            }
        }
        indeterminacyValue + = Data [I];                   // solve depth completed, the solution required not satisfied, go back needs to restore the current start value indeterminacyValue 
        return  to false ;
    }

    /**
     * Output
     */
    private static void print() {
        System.out.println ( "\ n-data array:" );
        Stream.of(data).forEach(element -> System.out.print(element + " "));
        System.out.println();
        System.out.println ( "Data storage:" );
        Stream.of(store).forEach(element -> System.out.print(element + " "));
        System.out.println();
        System.out.println ( "the number of the target composition is:" );
         for ( int I = 0; I <store.length; I ++ ) {
             IF (Store [I] ==. 1 ) {
                System.out.print(data[i] + " ");
            }
        }
        System.out.println();
    }
    
    public  static  void main (String [] args) {
         // initialization data 
        initData ();

        // backtracking to find 
        BackTrack (0 );

        // output 
        Print ();
    }

}
And a subset of the core code issues

(4) input and output:

Please enter the target:
 10
Please enter the number of data:
5
Please enter each number:
2 2 6 5 4

Data array:
2 2 6 5 4 
Data storage:
1 1 1 0 0 
The composition of the target number is:
2 2 6 
input Output

(5) Summary: subset and also fully reflect the core idea of the subsets backtracking tree, the time complexity of O (2 n- ), by a storage solution and does not exist, determines whether or prune, penetration depth search, once searching for a solution to the immediate return to;

  Recommendation: If I do not understand the naked eye, according to my code idea, painting a picture in the paper again walk solving process, easy to understand the code, another way to master the core idea of ​​backtracking set tree;

Guess you like

Origin www.cnblogs.com/blogtech/p/12302565.html