Recursive Algorithms and Their Applications

Source: Internet
Author: User
Recursive Algorithms and Their Applications
Http://www.mydrs.org 2002-7-4 big banyan

[Recursive description]
As shown in the preceding example, recursion requires boundary conditions, recursive forward segments, and recursive return segments. If the boundary condition is not met, recursive advances. If the boundary condition is met, recursive returns. Therefore, when writing a program using a recursive algorithm, two points must be met: 1) the problem can be described in a recursive form; 2) there is a boundary condition for Recursive termination.
The ability to recursion lies in the use of limited statements to define an infinite set of objects. Programs Written with recursive thinking are often very simple and easy to understand.

[Example 2]The middle and back order of a binary tree is given. Find its first order.
[Analysis]By comparing the middle and back order of the binary tree, we can find the root node and the Left and Right Subtrees. Similarly, you can find the root node of the Left subtree by comparing the middle and back order of the Left subtree ...... This issue can be recursively described. When the last root node is found, recursion cannot proceed. This is the boundary condition for Recursive termination. It can be seen that Recursive Algorithms often imply the idea of divide and conquer. The procedure is as follows:
Program chu01_3;
VaR Z, H: string;
Procedure find (A, B: string );
VaR
S, L: integer;
Begin
L: = length (B );
If l = 1 then write (B) {boundary condition and recursive return segment}
Else
Begin {recursive forward segment}
Write (B [l]);
S: = pos (B [L], );
If S-1> 0 then find (copy (A, 1, s-1), copy (B, 1, s-1); {recursive left subtree}
If l-S> 0 then find (copy (A, S + 1, l-S), copy (B, S, l-S); {recursive right subtree}
End;
End;
Begin
Readln (z );
Readln (h );
Find (z, H );
Readln;
End.
[Recursive application]

1. Classic Recursion
For example, the Hanoi Tower problem: Classic recursion, the original problem contains sub-problems. Some problems or data structures are described recursively, and recursion is natural.

2. recursion and Recursion
Use recursive ideas to establish recursive relationships, for example, the series of Fiber-ACCI generated by rabbits. Recurrence is easier because there is no return segment. Sometimes it can be implemented directly using a loop.

3. Sub-Governance
Many Sub-governance methods are derived from recursive thinking, or recursive decomposition + merge processing.

4. Backtracking
It is natural to solve small-scale problems with backtracking. Ensure that the on-site storage and recovery should be ensured before and after recursion, that is, the correct conversion problem.

5. Dynamic Planning
The subproblem overlapping nature of dynamic planning has some similarities with recursion. Recursion + dynamic table modification is a good way to build a dynamic planning model.

6. Miscellaneous
Other words are difficult to classify. Such as Expression Processing and arrangement and combination. It is convenient to use recursion to deal with the printing scheme.

[Example 3]Calculates the total number of methods that divide an integer n unordered into the sum of positive integers with different K portions.
[Analysis]This is a dynamic planning question. The dynamic equation is as follows:
F [I-1, J] + F [I, j-I] + 1 (J MoD I = 0) and (J Div I = 1 ))
F [I, j]: = f [I-1, J] (I> = J)
F [I-1, J] + F [I, j-I] (else)
S: = f (k, n-k)
In this question, we can use loops to implement recursion, or we can consider using recursive solutions. The main process is as follows:

Solution 1:
Procedure work (I, j: longint; var S: longint );
VaR T: longint;
Begin
If (I = 1) or (j = 1) Then S: = 1
Else if (I = 0) or (j = 0) Then S: = 0
Else begin
If (J MoD I = 0) and (J Div I = 1) then
Begin
Work (I-1, J, S );
T: = s;
Work (I, J-1, S );
S: = S + t + 1;
End
Else if (I> = J) then
Work (I-1, J)
Else begin
Work (I-1, J, S );
T: = s;
Work (I, J-1, S );
S: = S + T;
End;
End;

Solution 2:Procedure search (V, W, last: byte );
VaR I: byte;
Begin
If W = 0 then Inc (count)
Else
If W = 1 then
If v> = last then search (0, 0) else
Else For I: = last to V-1 do search (V-I, W-1, I );
End;

It can be seen that the program in solution 1 is lengthy and consumes a large amount of stack space. solution 2 is concise and clear, and the stack space used is also small and the efficiency is high. Therefore, recursive algorithms also have an optimization problem. The concise algorithm directly restricts the feasibility and efficiency of the program.

[Summary]
Recursion makes it easy and clear to solve some complex problems, especially when learning algorithm design and data structure. However, recursion allocates stack space for local variables and return addresses during each execution, which reduces the running efficiency and limits the depth of recursion. Therefore, when necessary, we can use only recursive ideas to solve the problem, while the program is written in non-recursive mode.

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.