[C Language/C ++] Recursive Algorithm

Source: Internet
Author: User

[This article is original to the Technical blog of Silence xuanyuan Sili .]

[This article is for reprinting. for reprinting, please indicate the source in the form of a link .]

[All the articles in this blog have been carefully organized by the bloggers. Please respect the fruits of my work .]

 

 

 [C Language/C ++] Recursive Algorithm

 

Recursive Algorithms are also simple and commonly used algorithms in C Language algorithms. In this article, we will talk about recursive algorithms. First, let's take a look at what recursive algorithms are. There is only one sentence for the concept of recursive algorithms: A process (or function) calls itself directly or indirectly. Such a process (or function) is called a recursive process (or function )..

 

Let's take a look at the features of recursive algorithms:

 

(1) recursion is to call itself in a process or function.

 

(2) When using a recursive policy, there must be a clear recursive termination condition called the recursive exit.

 

(3) recursive algorithms are usually simple in solving problems, but they are less efficient in solving problems. Therefore, recursive algorithms are generally not recommended for programming.

 

(4) In the process of recursive calling, the system opens a stack for storing the return points and local volumes of each layer. Too many recursion times may cause stack overflow. Therefore, recursive algorithms are generally not recommended for programming.

 

Let's look at recursive classification:

 

Direct Recursion
In program design, a process or function calls itself directly or indirectly, which is called a recursive call. Subprograms call themselves directly, which is called direct recursion. subprograms A and B of the nested relationship, and B of the inner layer call A of the outer layer, which is indirect low conversion. subprograms A and B of the level-level relationship, where A calls B and B calls A, which is indirect recursion. However, this indirect recursion uses the rule of "Advanced reference.

 

Below, the blogger finds some questions about recursive algorithms. Let's take a look at the first question.

 

This is a classic question: factorial. I believe most people know what a factorial is, but if you do not know it, refer to Baidu Encyclopedia: factorial.

 

Well, in fact, this question is very simple. We only need a function to solve the problem.

 

1 int recursive(int i)2 {3     int sum = 0;4     if (0 == i)5         return (1);6     else7         sum = i * recursive(i-1);8     return sum;9 }

 

This is the above recursive function. I will not waste my time explaining this question. I will focus on the second example: The Tower of Hanoi.

 

 

There are three pillars in a temple, and the first has 64 dishes, which are getting bigger and bigger from top down. Ask the old monk in the temple to move all the 64 dishes to the third pillar. When moving, you can only press a small plate on a large plate. You can only move one file at a time. 1. At this time, the old monk (we call him the first monk later) felt very difficult, so he thought: if a person could move the first 63 dishes to the second pillar first, I moved the last plate directly to the third pillar, and then the person moved the first 63 plates from the second pillar to the third pillar. Then my task was completed, simple. So he finds a monk younger than him (we call him the second monk later:

① You move the first 63 dishes to the second pillar

(2) then I moved the 64th plates to the third pillar.

③ You move the first 63 dishes to the third pillar
2. The second Monk took over the task and thought it was difficult, so he thought like the first MONK: if one person could move the first 62 dishes to the Third Pillar first, I moved the last plate directly to the second pillar, and then the person moved the first 62 plates from the third pillar to the third pillar. Then my task was completed, simple. So he also found a monk younger than him (we call him the third monk later:

① You move the first 62 dishes to the third pillar

(2) Then I move the first plate to the second pillar.

③ You move the first 62 dishes to the second pillar
3. The third Monk took over the task and handed over the task of moving the first 61 dishes to the fourth monk according to the words of the gourd, until the task has been handed over to 64th monks (it is estimated that 64th monks are very depressed and have no chance to give orders to others, because there is only one plate here ).

4. Submit the task to complete the assignment. Push back:
The 64th monks moved 1st dishes and removed them. Then the 63rd monks moved the 2nd dishes They allocated to themselves.
The third monk moved 64th more dishes to the third. Here, the task of the 64th monks is completed, and the 63rd monks have completed the first step of the task assigned to them by the 62nd monks.
It can be seen from the above that only 64th monks have completed their tasks and 63rd monks have completed their tasks. Only after the tasks of 2nd monks-64th monks have been completed, the task of 1st monks can be completed. This is a typical recursive problem. Now we have three plates for analysis:

1st monk commands:

① You shall first move the first two plates of the first pillar to the second pillar. (Using the third pillar)

② I moved the last plate of the first pillar to the third pillar.

③ You move the first two plates from the second pillar to the third pillar.
Obviously, the second step is easy to implement.

In the first step, the first monk had two plates and he ordered:

① 3rd monks you move the first pillar of 1st plates to the third pillar. (Using the second pillar)

② I moved 2nd dishes on the first pillar to the second pillar by myself.

③ 3rd monks you move 1st dishes from the third pillar to the second pillar.

Similarly, the second step is easy to implement, but the first monk only needs to move one plate, so he does not have to dispatch the task. (Note: This is the condition for stopping recursion, also called the boundary value)
The third step can be divided into two dishes for the 2nd monks. The command is as follows:
① 3rd monks you move the 1st plates on the second pillar to the first pillar.

② 2nd monks I moved 2nd dishes from the second pillar to the third pillar.

③ 3rd monks move the plate on the first pillar to the third pillar.
The combination of analysis is: 1 → 3 1 → 2 3 → 2 use the third column to move to the second column | 1 → 3 | 2 → 1 → 3 → 3 use the first column to move to the third column | seven steps are required.

If there are four plates, the first monk's command contains three plates in step 1 and step 2, so each of them requires 7 steps and 14 steps in total, add one step of 1st monks, so four plates need to move 7 + 1 + 7 = 15 steps in total. Similarly, five plates need 15 + 1 + 15 = 31 steps, 31 + 1 + 31 = 64 steps for 6 dishes ...... From this we can know that moving n plates requires (2 to the Npower)-1 step.

From the overall analysis above, we can see that the n plates are moved from one (equivalent to the first pillar) to three (equivalent to the third pillar ):
(1) move one N-1) plate to two with three.
(2) Move the n plates on the first seat to the third seat.
(3) Move two (n-1) dishes on one seat to three.
The following uses hanoi (n, a, B, c) to move one n plates to three with two.
Obviously: hanoi (n-1, 2) (3) is hanoi (n-1, 3)
It is shown in C language:

 

 1 #include <stdio.h> 2 int method(int n,char a, char b) 3 { 4      printf("number..%d..form..%c..to..%c.."n",n,a,b); 5      return 0; 6 } 7 int hanoi(int n,char a,char b,char c) 8 { 9      if( n==1 ) move (1,a,c);10      else11           {12                hanoi(n-1,a,c,b);13                move(n,a,c);14                hanoi(n-1,b,a,c);15           };16      return 0;17 }18 int main()19 {20      int num;21      scanf("%d",&num);22      hanoi(num,'A','B','C');23      return 0;24 }

 

This is a recursive algorithm. In C, recursive algorithms are more practical than enumeration algorithms, but both algorithms are very simple.

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.