Backtracking algorithms and greedy algorithms (all permutations)

Source: Internet
Author: User

A: Introduction

(1) Backtracking method also known as the Heuristic method

The basic method of backtracking is depth-first search , which is an organized and exhaustive search algorithm that avoids unnecessary duplication of searches ; The basic idea is: Go forward from a road, can enter into, not into the back, change a way to try again.
Application scenario: When encountering a certain type of problem, its problem can be decomposed, but can not draw a definite dynamic programming or recursive solution, at this time may consider the backtracking method solves this kind of problem. The advantage of backtracking is that its program structure is clear, readable and easy to understand, and through the analysis of the problem can greatly improve the operational efficiency. However, for the problem that can be derived from the iterative solution of the recursive formula, do not use backtracking, because it takes a long time (not the last resort, no backtracking, time-consuming)--sometimes, sensory backtracking, very similar to the previous blog dfs+ pruning

(2) Greedy algorithm also known as greedy algorithm

When solving a problem, always make the best choice at the moment . In other words, not considering the overall optimality, he makes only the local optimal solution in a certain sense.
Greedy algorithm is not to all problems can get the overall optimal solution, the key is the choice of greedy strategy, the choice of greedy strategy must have no effect, that a state of the previous process will not affect the future state, only related to the current state. compared with dynamic programming, dynamic programming is the search for sub-problems, the sub-problem has great similarity with the original problem, and the solution is the global optimal

(3) Recursion and backtracking

Recursion is a kind of algorithm structure, backtracking is an algorithm idea; a recursion is a function that calls the function itself to solve the problem; backtracking is a different attempt to generate the solution of a problem , a bit like a poor lift, but unlike the poor, backtracking is "pruning", The meaning is to know the result of the error is not necessary to enumerate the next answer, such as an ordered sequence of 1,2,3,4,5, I want to find and for 5 of all sets, the previous search I chose 1, then 2, and then select 3 when found and already greater than expected, then 4,5 certainly not, This is an optimization of the search process.

Second: detailed case

(1) retrospective approach

the first thing to do is to transform the problem into a state space tree. each complete path of this tree represents a possible solution. Search the tree by depth first, enumerate the circumstances of each possible solution, and derive the result. However, by constructing the constraint function in the backtracking method, the program efficiency can be greatly improved, because in the process of depth-first searching, each solution is constantly (not necessarily complete, in fact this is the meaning of the tectonic constraint function) and the constraint function to delete some impossible solutions, This saves part of the time by not having to continue to list the remainder of the solution.
Three Concepts in backtracking:
(a) constraint function: the constraint function is determined according to test instructions. by describing the general characteristics of the legal solution to remove the illegal solution, so as to avoid continuing to search out the remainder of this illegal solution . Therefore, the constraint function is valid and equivalent for the nodes on any state space tree.
(ii) state space tree: Just already mentioned, the state space tree is a graphical description of all solutions . Only one part of the solution for each child node on the tree is different from the parent node.
(c) Expansion node, Slipknot Point, Knot Point: The so-called extension node, is the node that is currently finding its child nodes, in the depth-first search, only one extension node is allowed. Slipknot Point is through the control with the constraint function, the node itself and its parent node satisfy the constraint function requirements of the node; It is easy to know that the knot point is not to find its child nodes (meaningless).
The concrete steps of solving problems by backtracking method

(2) Steps to achieve the backtracking method

Complete the following three steps by reading a question:
1) Describe the form of the solution, define a solution space, which contains all the solutions to the problem.
2) constructs a state space tree.
3) construct constraint functions (for killing nodes).
Then it is necessary to complete the backtracking through the deep first search idea, the complete process is as follows:
1) Set the initialization scheme (assign the initial value to the variable, read into the known data, etc.).
2) Change the way to temptation, if all the test finished then turn (7).
3) Determine if the method is successful (through a constraint function), and then turn (2) if unsuccessful.
4) test the success before further testing.
5) The correct solution has not been found then turn (2).
6) A scheme has been found to record and print.
7) Return one step (backtracking) and turn (2) If the head is not returned.
8) retired to the end or printed without solution


(3) Backtracking example (together with the number of plots)

#include <iostream> #include <cstring>using namespace std; #define Isbound (A, b) (a<0 | | a>=b) CONST INT MAXN = 21;char map[maxn][maxn];bool visit[maxn][maxn];int w,h,ans;void dfs (int row,int col) {if (Isbound (row,h) | | Isbound (col,w) | | map[row][col]== ' # ' | |    Visit[row][col]) return;    Visit[row][col] = true;    Ans + +;    DFS (ROW,COL-1);    DFS (ROW-1,COL);    DFS (ROW,COL+1); DFS (ROW+1,COL);}    int main () {int i,j,row,col;    Char FLG = ' @ '; while (cin>>w>>h&& (w| |        h) {ans = 0;        memset (visit,false,sizeof (visit));                for (I=0;iCan remember to search, set a avilable array record can be reached.

If a location avilable is true, prove that you have searched for this point and no longer repeat the search. The position that can be reached by this point must have been marked as avilable.

(4) Backtracking Example II (full array)

# include <stdio.h>void swap2 (char *x, char *y) {    char temp;    temp = *x;    *x = *y;    *y = temp;} /* Print the full array of strings */void Permute (char *str, int i, int n) {   int J;   if (i = = N)     printf ("%s\n", str);   else   {        for (j = i; J <= N; j + +)       {          swap ((Str+i), (str+j));          Permute (str, i+1, n);          Swap ((Str+i), (str+j)); Backtracking       }   }}/* Test */int main () {   char str[] = "ABCD";   int len = sizeof (str)/sizeof (char)-2;   Permute (str, 0, Len);   GetChar ();   return 0;}

(4) Greedy algorithm

(1) The basic idea of greedy algorithm:
1. Build a mathematical model to describe the problem.
2. Divide the problem of solving into several sub-problems.
3. To solve each sub-problem, the local optimal solution of sub-problem is obtained.
4. The solution of the problem is solved by the local optimal solution to the original solution problem.

(2) The problem that greedy algorithm applies

The premise of greedy strategy is that local optimal strategy can lead to global optimal solution.
In fact, greedy algorithms are rarely used. In general, whether a problem analysis is applicable to greedy algorithm, you can choose the problem of a few real data analysis, you can make judgments.

(3) The implementation framework of greedy algorithm
Starting from an initial solution of a problem;
While (one step forward for a given total target)
{
A solution element of the feasible solution is obtained by using the feasible decision;
}
A feasible solution to the problem of the combination of all solution elements;

(4) Choice of greedy strategy
Because the greedy algorithm can only solve the global optimal solution by solving the local optimal solution, we must pay attention to whether the problem is suitable for the greedy algorithm and whether the solution found is the best solution.

Backtracking algorithms and greedy algorithms (all permutations)

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.