Recursion, recursion, and Recursion

Source: Internet
Author: User

Recursion, recursion, and Recursion

Recursive Concept

If a function, process, concept, or data structure is directly or indirectly referenced within its definition or description, or to describe a state of the problem, the previous state must be used, and the previous state must be used to describe the previous state ...... This self-defined method is called recursive or recursive definition.
In program design, a process or function calls itself directly or indirectly, which is called a recursive call.

The recursive process is actually implemented through a recursive working stack.
First, the problem is decomposed step by step in one direction, that is, the scale of the problem is gradually reduced. In this way, the process of question moving forward is called a recursive process;
Then these problems are solved in the order that they are solved first, and finally the original problem is solved. In this way, after the sub-problems are solved one by one, we finally return to the change process of the solution of the original problem, which is called the regression process.

Elements defined by recursion

1. Recursive boundary or termination condition.
2. recursively define the rules for converting the problem to the boundary condition.

Var
N: integer;
Function fibonacci (x: integer): integer;
Begin
If (x = 0) or (x = 1) then exit (1 );
Exit (maid (x-1) + maid (X-2 ));
End;
Begin
Readln (n );
Writeln (maid (n ));
End.


Division of Sets

Set s to a set of n elements, s = {a1, a2 ,......, An}, now divides s into K sub-sets s1, s2,…, meeting the following conditions ,......, Sk, and meets the following requirements:
1. si =phi
2. si 1_sj = PHI (1 ≤ I, j ≤ k I =j)
3. s1 protocol s2 protocol s3 protocol... Sk = s
S1, s2 ,......, Sk is a division of the Set s. It is equivalent to putting n elements in the s set a1, a2 ,......, An is placed in k (0 <k ≤ n <30) unlabeled boxes, so that no box is empty. Please confirm n elements a1, a2 ,......, An is the number of Division s (n, k) placed in k unlabeled boxes ).

For example, if S = {1, 2, 3, 4}, k = 3, it is not hard to conclude that s has six different partitioning schemes, namely, number s () = 6, the specific solution is:
{1, 2} else {3} else {4}
{1, 3} else {2} else {4}
{1, 4} bytes {2} bytes {3}
{2, 3} else {1} else {4}
{2, 4} threads {1} threads {3}
{3, 4} else {1} else {2}


In general, for any element containing n a1, a2 ,......, The set s OF an is put into k unlabeled boxes, and the number of partitions is s (n, k ), it is difficult for us to calculate all the schemes for dividing numbers and enumeration Based on intuition and experience. We must summarize the nature of the problem.
The following two conditions must be considered for any element ":
1. {an} is one of k sub-sets, so we only need to put a1, a2 ,......, An-1 is divided into a subset of the K-1, it solves this problem, in this case the number of Division total s (n-1, k-1;
2. If {an} is not a k sub-set, an and other elements constitute a subset. The problem is equivalent to putting a1, a2 ,......, An-1 is divided into k subsets. In this case, the scores are total s (n-1, k), and then the element an is added to any of the k subsets, there are a total of k joining methods. In this way, each joining method of an can divide the set into k subsets. Therefore, according to the multiplication principle, the number is divided into k * s (n-1, k.


Based on the above two cases, the addition principle is applied to obtain the set of n elements {a1, a2 ,......, An} is divided into k subsets by the following recursive formula:
S (n, k) = s (n-1, k-1) + k * s (n-1, k) (n> k, k> 0 ).


Determine the boundary condition of s (n, k)

N elements cannot be put into any collection first, that is, when k is 0, s (n, k) = 0;
It is also impossible to put n elements into k sets greater than n without empty boxes, that is, when k> n, s (n, k) = 0;
In addition, put n elements into a set or put n elements into n sets, the number of solutions is obviously 1, that is, when k = 1 or k = n, s (n, k) = 1.
It can be concluded that the recursive Relationship Between the number s (n, k) is:
S (n, k) = s (n-1, k-1) + k * s (n-1, k) (n> k, k> 0)
S (n, k) = 0 (n <k) or (k = 0)
S (n, k) = 1 (k = 1) or (k = n)


Recursive and non-recursive Conversions

In the recursive call process, because the parameters and local variables used for each call need to be retained, the two major processes must be called layer by layer and returned layer by layer, the program consumes a lot of time and storage space. Therefore, when a problem requires a high degree of time and space, it is sometimes necessary to convert a recursive method into a non-recursive method. This process is often called elimination recursion. The process of eliminating recursion is essentially to simulate the change process of the complex recursive working stack. Although recursion and non-recursion can convert each other in the form, non-recursive programming is much more difficult than recursive methods.

Recurrence

In the question type, each data item is associated with several data items (or several data items) before it. This association is generally expressed by a recursive relationship. When solving the problem, we start from one or more initial data items and gradually advance through the recursive relationship to obtain the final result. This method is called the progressive method.


Var
A: array [0 .. 100] of longint;
I: integer;
Begin
Readln (n );
A [0]: = 1;
A [1]: = 1;
For I: = 2 to n do
A [I]: = a [I-1] + a [I-2];
Writeln (a [n]);
End.


Algorithm Framework

1. determine the initial conditions for solving the problem
2. Calculate the Recurrence Formula
3. Push forward or backward from the boundary
4. output results


For details, see:

Http://download.csdn.net/download/boyxiejunboy/8913331

Http://download.csdn.net/download/boyxiejunboy/8913339

Http://download.csdn.net/download/boyxiejunboy/8913343

(Download without points)

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.