The four most common algorithms: Backtracking method

Source: Internet
Author: User

1. Concept

The backtracking algorithm is actually a kind of enumeration-like search attempt process, mainly in search attempts to find the solution of the problem, when the discovery has not satisfied the solution condition, the "backtracking" return, try another path.

The backtracking method is an optimal search method, which is searched forward according to the selection criteria to achieve the goal. However, when the exploration of a step, found that the original choice is not good or not reach the goal, then return to the one-step re-selection, such a failure to return to go back to the technology as backtracking, and meet the backtracking condition of a state point of the "backtracking point."

Many complex, large-scale problems can use backtracking, there is a "common problem-solving method" laudatory.

2. Basic Ideas

in the solution space Tree of all solutions containing the problem, the solution space tree is explored in depth from the root node based on the strategy of depth-first search . When exploring a node, it is necessary to determine whether the node contains the solution of the problem, if it is included, to continue the exploration from the node, if the node does not contain the solution of the problem, it will go back to its ancestor node by layer. (In fact, the backtracking method is the depth-first search algorithm for implicit graphs).

If you use backtracking to find all the solutions to the problem, go back to the root, and all the viable subtrees of the root node are searched and finished.

If you use backtracking to find any solution, you can end up searching for a solution to the problem.

3. The general steps of solving problems by backtracking method:

(1) For the given problem, determine the solution space for the problem:

First, the solution space of the problem should be clearly defined, and the solution space of the problem should contain at least one (optimal) solution of the problem.

(2) Define the extended search rules for the nodes.

(3) Searching the solution space in depth first, and using pruning function to avoid invalid search during the searching process.

4. Algorithm Framework

(1) Problem framework

The solution to the problem is an n-dimensional vector (a1,a2,........., an), the constraint is that the AI (i=1,2,3,....., N) satisfies a certain condition, recorded as f (AI).

(2) Non-recursive backtracking framework

inta[n],i; Initialize an array a[];i=1; while(i>0(There is a way to go) and (not reaching the target))//not back to the head.{    if(i > N)//Search to leaf node{Search for a solution, output;}Else                                                   //working with Element i{A[i] the first possible value; while(A[i] The next possible value of {A[i] in the search space that does not satisfy the constraint);}if(A[i] in the search space) {identifies the resource occupied;= i+1;//extend the next node.         }         Else{The state space occupied by the cleanup;//Backtrackingi = i –1; }}

(3) Recursive algorithm framework

Backtracking is a depth-first search of the solution space, and in general it is simpler to use recursive functions to achieve backtracking, where I is the depth of the search, and the framework is as follows:

intA[n];Try(inti) {     if(i>n) output results; Else     {         for(j = Nether; J <= Upper bound; j=j+1)//enumerate all possible paths of I        {           if(Fun (j))//satisfies the limit function and the constraint condition{A[i]=J; ...                         //Other Operations                Try(i+1);              Pre-backtracking cleanup work (e.g. A[i] empty values, etc.); }         }     }}

(The above is transferred from the five most commonly used algorithm four: Backtracking method)

The four most common algorithms: Backtracking method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.