(4) Backtracking of the five major algorithms

Source: Internet
Author: User

Introduction

The backtracking algorithm is also called Heuristic, which is a method to search the solution of the problem systematically. The basic idea of backtracking algorithm is: Go forward from a road, can enter into, can not enter the back, change a road and try again. The general steps for solving problems with backtracking algorithms are:
1, define a solution space, it contains the solution of the problem.
2, the use of suitable search methods to organize the solution space.
3, the use of depth-first method to search the solution space.
4, use the limit function to avoid moving to the sub-space that cannot produce the solution.
The solution space of the problem is usually generated dynamically in the process of searching the solution of the problem, which is an important feature of backtracking algorithm.

Basic Ideas

The basic idea of backtracking algorithm is: Go forward from a road, can enter into, can not enter the back, change a road and try again.

The eight Queens problem is the typical backtracking algorithm, the first step in order to put a queen, and then the second step to meet the requirements of the 2nd Queen, if there is no position to meet the requirements, then you have to change the position of the first Queen, re-placed the 2nd Queen position, until the match to find the position of the condition.

Backtracking in the maze search is very common, that is, the road does not go through, and then go back to the previous intersection, continue the next road.

The backtracking algorithm is plainly a poor lifting method. However, the backtracking algorithm uses pruning functions to cut down some nodes that cannot reach the final state (i.e., the answer state), thus reducing the generation of the state space tree nodes.

Backtracking is a search algorithm with both a systematic and a jumping nature. It searches for the solution space tree from the root node in the solution space tree of all solutions that contain the problem, based on the strategy of depth first. When the algorithm searches to any node of the solution space tree, it always first determines whether the node does not contain the solution of the problem. If it is not, skip the system search for the subtree rooted in the node and backtrack from layer to node. Otherwise, go to the subtree and continue searching by a depth-first policy. Backtracking is used to find all the solutions to the problem, to go back to the root, and all the subtree of the root node has been searched for the end. When backtracking is used to solve the problem, a solution to the problem can be any as long as the search is done.

The algorithm of searching the solution of the problem in a depth-first way is called backtracking, which is suitable for solving some problems of large combinatorial numbers.

Classic Example1) Eight queen question

 #include <  stdio.h> #include <math.h>int a[9] = {0};int n = 8, count = 0;//position conflict algorithm bool Chongtu (int a[], int n)//a[] position array, n Queens number {int i = 0, j = 0;for (i = 2; I <= n; ++i)//i: position for (j = 1; j <= i-1; ++j)//j: Position if ((a[i] = = A[j]) | | (ABS (a[i]-a[j]) = = i-j))   1: on one line; 2: return false on the diagonal; Conflict return true;//conflict}//Eight queen problem: backtracking algorithm (recursive version) void Queens8 (int k)//Parameter K: recursive placement of k Queens {int i = 0;if (k > N)//k>n--i.e. K&G T;8 said that the last queen was placed {printf ("%d case:", ++count), for (i = 1; I <= n; ++i) printf ("%d", a[i]);//Print case printf ("\ n");} else//8 queens Not all placed {for (i = 1; I <= n; ++i)//When placing the K-Queens (Note: Go to the next line) {//start the search from the top of the column, until the bottom of the column, until you find the appropriate location, if not found, auto-return               Back upper recursion (backtracking) a[k] = i; if (Chongtu (a,k))//Conflict Queens8 (k+1);//recursively place next Queen}}return;} The main function int main () {QUEENS8 (1);//Parameter 1: Indicates placing the 1th Queen return 0;} 
more detailed description of the eight Queens question, you can refer to: Classic algorithm-Backtracking (http://blog.csdn.net/qingdujun/article/details/26282895)

(not to be continued ...) )

Reference documents:

1) Baidu Encyclopedia, backtracking algorithm, http://baike.baidu.com/view/6056523.htm

(4) Backtracking of the five major algorithms

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.