Current location - Trademark Inquiry Complete Network - Futures platform - How can we deeply understand recursion and backtracking?
How can we deeply understand recursion and backtracking?
Recursion is an algorithm structure, and backtracking is an algorithm idea. One recursion is to call the function itself in a function to solve the problem, and backtracking is to generate the solution of the problem through different attempts, which is somewhat similar to exhaustive, but unlike exhaustive, backtracking will "prune", that is, there is no need to enumerate the next answer for the wrong result, such as the ordered sequence of 1, 2,3,4,5. I chose 1, and then I chose 2, and then I found that the sum when I chose 3 was greater than expected, so 4 and 5 definitely didn't work. This is an optimization of the search process.

Retrospective analysis is one of the characteristics of tracking decision. Refers to the analysis of the original decision-making mechanism, decision-making content, subjective and objective environment, etc. From the starting point, reason, nature of the problem, degree of error, etc. Investigate those that lead to decision-making mistakes in order.

[Algorithm analysis]

In order to describe the state of a problem, we must use its previous state, and in order to describe its previous state, we must also use its previous state ... This method of defining ourselves is called recursive definition. For example, the function f(n) is defined as:

f(n)= n * f(n- 1)(n & gt; 0)

f(n)= 1 (n=0)

Then when 0, f(n) must be defined by f(n- 1), and f(n- 1) must be defined by f (n-1) ... When n=0, f (n) = 60.

From the above example, we can see that the recursive definition has two elements:

(1) recursive boundary conditions. That is, the simplest case of the described problem, which itself no longer uses recursive definitions.

As in the above example, when n=0, f(n)= 1 is not defined by f(n- 1).

(2) Recursive definition: a rule that transforms a problem into a boundary condition. Recursive definitions must make the problem easier and easier.

For example, f(n) is defined by f(n- 1), which is closer and closer to f(0), that is, the boundary condition. The simplest case is f(0)= 1.

Recursive algorithms are often inefficient, time-consuming and memory-consuming. However, recursion also has its advantages, which can make a program introduction with recursive relationship and complex structure concise and increase readability. Especially in the case that it is difficult to find the whole process from boundary to solution, if the problem is pushed further and the result still maintains the relationship of the original problem, then recursive algorithm programming is more suitable.

Recursively divided into: 1. Direct recursion, recursive process p directly calls itself; 2. indirect recursion, that is, p contains another process d, and d calls p.

Recursive algorithm is suitable for the following general occasions:

1. data is defined recursively.

For example, the definition of Peibonachi sequence: f (n) = f (n-1)+f (n-2); f(0)= 1; f( 1)=2。

The corresponding recursive program is:

Function fib(n: integer): integer;

begin

If n = 0, then fib:= 1 {recursive boundary}

Otherwise, if n = 1, then fib := 2.

else fib:= fib(n-2)+fib(n- 1){ recursive }

End;

This kind of recursive problem can be transformed into recursive algorithm, with recursive boundary as recursive boundary condition.

2. The relationship between data (i.e. data structure) is defined recursively, such as tree traversal and graph search.

3. The problem is solved by recursive algorithm, such as backtracking.

Starting from a certain possibility of the problem, search all the possibilities that can be achieved from this situation, when this road comes to an end.

Go back to the starting point, start from another possibility and continue the search. This method of constantly "backtracking" to find a solution is called

"retrospective method"

[Reference procedure]

Here is an algorithm framework for finding all paths by backtracking. The notes have been written clearly, please understand them carefully.

const max depth =;

type statetype =; {Status Type Definition}

operator type =; {operator type definition}

Node = record {node type}

state:state type; {Status field}

Operator:operator type {operator field}

End;

{Note: The data types of nodes can be simplified according to the needs of test questions}

defined variable

Stack: array [1... maxdepth]; {Save current path}

Total: integer; {number of paths}

Program make(l: integer);

Var i: integer;

begin

If the stack [L- 1] is the target node, then.

begin

Total: = total+1; {Number of paths+1}

Print the current path [1.. l-1];

export

End;

For i := 1 to the number of tree decomposition do

begin

Generate stack [l]. Operator;

Stack [l]. Operator acts on stack [l- 1]. State to generate a new state stack [l]. State;

If stack[l]。 Make (k+1) if the state satisfies the constraint conditions;

{If the constraints are not met, the operator extension will be changed through the for loop. }

{When recursively returning there, the system will automatically restore the stack pointer and operator before calling, and then change an operator extension through the for loop. }

{Note: If global variables are used when extending stack[l]. State, you should insert several statements to restore.

The value of stack [l- 1]. state.

End;

{There are no more operators available, backtracking}

End;

begin

Total: = 0; {Number of paths initialized to 0}

Initialization processing;

make(l);

Total number of print paths

End.