Backtracking framework law

Backtracking : GM has said that the problem-solving method, you can search all the solutions of a problem and a solution to any system, both with a systematic, but also with a leap of search algorithms.

The basic idea of ​​the algorithm:

  After determining the solution space

  From the start node to a depth-first search the entire solution space.

  If the current node can no longer expand the depth direction, the current node is node death. At this point, you should move back to the nearest node a live. And a node or the node becomes the current node is extended.

Ways to improve the algorithm (pruning function) :

  1 with the constraint functions cut out in the extension does not satisfy the constraint node subtree

  2 are not cut with optimum subtree bounding function.

Backtracking solving steps:

  1 solution space defined problem

  2 Determine easily searchable solution space structure

  3 in a depth-first search the solution space, and a function to prevent invalid pruning search using the search process.

Recursive backtracking:

void Backtrack(int t)
{
    if(t>n)
        Output(x);
    else
        for(int i=f(n,t);i<=(g,t);i++)
        {
            x[t] = h(i);
            if(Constraint(t) && Bound(t))
                Backtrack(t+1);
        }
}

Subset of the tree:

When the problem is to find from all the set S n elements satisfying certain properties of the subset, a corresponding solution space called tree subset of the tree.

Pseudo code is:

void Backtrack(int t)
{
    if(t>n)
        Output(x);
    else
        for(int i=f(n,t);i<=(g,t);i++)
        {
            x[t] = h(i);
            if(Constraint(t) && Bound(t))
                Backtrack(t+1);
        }
}

Tree arrangement:

When the problem is to determine the n elements are arranged to meet some properties, the corresponding number called solution space tree arrangement.

Pseudo code is:

void Backtrack(int t)
{
    if(t>n)
        Output(x);
    else
        for(int i=f(n,t);i<=(g,t);i++)
        {
            Swap(x[t],x[i]);
            if(Constraint(t) && Bound(t))
            {
                Backtrack(t+1);
            }
            Swap(x[t],x[i]);
        }
}

Reproduced in: https: //my.oschina.net/u/204616/blog/545346

Guess you like

Origin blog.csdn.net/weixin_34319640/article/details/91990076
law
Recommended