Back to the original starting point! - backtracking summary

Back to the original starting point, not just a path to the future

- The fifth chapter summarizes the algorithm soft three Yang Geng 20181003083

First, the adopted piecemeal, pliant workhorse

Backtracking - "general problem-solving law", that is, the systematic search of all solutions of a problem or any solution, it is with both a systemic and jumping with the algorithm of search elements - depth search!

In my own perception, backtracking template is like a tree, and then continue to select each layer node, does not meet the conditions encountered on the return on a node to select data for other nodes to continue. Depth of the search, through all the solution, there will always get positive solution! Short board: high time complexity! So this time we need to use "restraint function", "bounding function" and no need to reduce the number of repetitive tasks.

Second, the "subset sum" problem

5-1 subset sum problem (25 points)

'S set S = {x1, x2, ..., xn} is a set of positive integers, c is a positive integer, and the problem subset determines whether there is a subset S of S1, so that the elements of S1, and is c. Try to design a solution backtracking subset and problems.

Input formats:

Data input line 1 has two positive integers n and c, n denotes the size of S, c is the subset of target values. 1 in the next row, with n a positive integer, S represents an element in the collection. And is a subset of the target. 1 in the next row, with n a positive integer, S represents an element in the collection.

Output formats:

Solutions and output a subset of problems, separated by spaces, after the last output of the spaces. When a problem has no solution, output "No Solution!".

Sample input:

Here we are given a set of inputs. E.g:

5 10
2 2 6 5 4

Sample output:

Given here corresponding output. E.g:

2 2 6 

 

Solution Structure: the subset of the tree

      

Constraint function (backtracking algorithm is also given): flag (if you have to meet the subset and then quit)

                                                  When the sum == target, and a subset of satisfied, the output

                                                  sum> When the target || t == n, reaches the leaf node, end

                                                  sum + a [t] <= target constraint, prune

      

void backtrack(int t) {

    if(flag) return;

    if(sum == target) {

        flag = 1;

        for(int i = 0; i < n; i++) {

            if(x[i])

                cout<<a[i]<<" ";

        }

        cout<<endl;

        return;

    }

    if(sum > target || t == n) return;

    if(sum + a[t] <= target) {

        sum += a[t];

        x[t] = 1;

        backtrack(t+1);

        x[t] = 0;

        sum -= a[t];

    }

    backtrack(t+1);

}

 

Third, the pair programming leak filled

This chapter in the process of learning to understand the basic routines Backtracking, relatively easy to solve the problem, but also to digest the bounding function.

The beginning of the bounding function do not know, just know that simply pruning - so I have to spend a lot of time to think about the solution of structural problems, how to write code ...... with the help of his teammates, to understand some of the routines. Solution structure no more than two, or a subset of the tree or else arranged tree, which is the template on which the sets. Pruning will consider the choice of site conditions like, bounding function (strict pruning), row a general order, and take the order and conditions to consider almost came out.

Guess you like

Origin www.cnblogs.com/990924991101ywg/p/12038186.html