Introduction to Recursive Algorithms

Source: Internet
Author: User

Recursive Algorithms

The Programming Technique of program calling itself is called recursion ).
A process or function directly or indirectly calls itself in its definition or description, it usually converts a large and complex problem into a small problem similar to the original question to solve it, recursive strategies require only a small number of programs to describe the repeated computation required in the problem-solving process, greatly reducing the amount of code in the program.
Note:
(1) recursion is to call itself in a process or function;
(2) When using an incremental strategy, there must be a clear recursive termination condition called the recursive exit.

A classic description of the story is the story of the old monk. He said that there was a mountain, a temple on the mountain, and an old monk in the temple telling a story. He said that there was a mountain and a temple on the mountain, there was an old monk in the temple telling a story. He said there was a mountain in the past ,....... In this way, the story will be repeatedly told until the old monk stops tired.

Repeated stories can be seen as repeated calls, but it makes no sense if you cannot stop, so you can finally stop. The key to recursion is to find the recursive equation and the conditions for Recursive termination. That is to say, the old monk repeats the story and the recursive equation needs to exist. At last, the old monk is tired of stopping and the recursive termination condition also needs to exist.

Factorial algorithms can be defined as functions.

When N> 0, F (n-1) is used to define F (N), F (n-1-1) is used to define F (n-1 )......, This is a description of recursive forms.

When n = 0, F (n) = 1, which is the condition for Recursive termination.

In fact, all recursive problems can be viewed as class problems.

The entire problem to be solved (the entire recursive function) is regarded as F (N). In this recursive function, we need to do the following:

* 1. Write the recursive exit
* 2. solve the current problem ----- equivalent to (n) of the class problem)
* 3. recursion (calling itself) solves another problem of the same but smaller scale-equivalent to F (n-1)

If the function implements these points, the recursive algorithm is basically successful.

But there are some problems. His f (n-1) may need to be called several times (the number of workers may be different each time), because he needs to call f (n-1) before implementing (n) prerequisites,

There are many such examples. For example, this is the case with the Quanta problem.

All in all, you need to understand how to migrate complex recursive problems to simple class recursive problems. At this time, the problems are better understood and solved than the hidden ones.

The following are examples of recursive solutions to this problem:

1. Use recursive algorithms to store N numbers in an array in an inverted order.

The above method is used to analyze the program as follows:

Package SF. digui;

Public class recursion {
Private int B [] = NULL;
Private int Len = 0;
Public recursion (int B []) {
This. B = B;
This. Len = B. length;
}

Public void resevert (int I, Int J ){
If (I> = J) return;
// ==================================
B [I] = B [I] + B [J];
B [J] = B [I]-B [J]; // Note: The value exchange between two variables is not implemented through a third party (with another memory enabled ).
B [I] = B [I]-B [J];
// ================================

Resevert (I + 1, J-1 );
}

Public void printthis (){

For (INT I = 0; I <Len; I ++ ){
System. Out. Print (B [I] + "");

}
System. Out. println ();
}


Public static void main (string [] ARGs ){
Int B [] = {1, 2, 4, 5, 6, 7, 8, 9 };
Int Len = B. length;
Recursion rec = new recursion (B );
System. Out. println ("the Starting number of arrays is :");
Rec. printthis ();
Rec. resevert (0, len-1 );
System. Out. println ("Number of inverted Arrays :");
Rec. printthis ();
}
}

II .. using recursive algorithms: there are 52 cards, so that all of them face up. The first round starts from 2nd cards, and the cards at the multiples of 2 are turned to face down; in the second round, starting from 3rd cards, all cards at the multiples of 3 are turned from the front to the front and from the front to the front; starting from 4th cards in the third round, the cards at the multiples of 4 follow the same rules above, and so on until the first card is more than 52. Count the numbers of cards facing up and their positions.

After the above method analysis, the program is obtained as follows:

Package SF. digui;

Public class digui {
Private int N;
// Private int;
Private int P [] = NULL; // stores the front and back information of all cards
Public digui (int n ){
This. n = N;
// A = N;
P = new int [N];
For (INT I = 0; I <n; I ++ ){
P [I] = 0; // here 0 indicates positive, and 1 indicates negative.
}
}


Public void process (int A) {// ====== equivalent to F (N)
Int B =;
If (A = 1) return; // recursive exit
// The following part is to solve the current problem (you can start from the last step or the first step) --- equivalent to (n)
// ================================================ ==================
While (B <= N ){//
P [b-1] = (P [b-1] = 0 )? 1:0 ;//
B = 2 * B ;//
}
// ================================================ ====================
Process (A-1); // calls itself to solve the same but smaller problem-equivalent to F (n-1)


}

Public void printthis (){
Process (N );
For (INT I = 0; I <n; I ++ ){
System. Out. println ("no." + (I + 1) + "the positive and negative numbers of the sheets are:" + P [I]);
}
}


Public static void main (string [] ARGs ){
Digui = new digui (52 );
Digui. printthis ();
}
}
 
 
/* Description:
* I think all recursion is the same as that of the imaging class-equivalent to F (n) = N + f (n-1 ).
* Solve the following problems:
* 1. Write the recursive exit
* 2. solve the current problem ----- equivalent to (n) of the class problem)
* 3. recursion (calling itself) solves another problem of the same but smaller scale-equivalent to F (n-1)
*
*
* You must learn to migrate complex recursive problems to a simple recursive problem just like the imaging class.

**/

 

The above are some of my thoughts on recursion. I hope many others will reply and discuss, communicate, and make progress together.

Introduction to Recursive 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.