Note 1: recursion; Note: Recursion

Source: Internet
Author: User

Note 1: recursion; Note: Recursion

At the beginning, I talked about the analysis of the space complexity and time complexity of the algorithm. How did the algorithm come from and how it evolved step by step? I have read the path of the algorithm before. This is a bit familiar with it, is the specific example of the time complexity analysis is not very good

Yes, you should exercise more. After all

This is a basic thing ~

Then there is recursion and sub-governance ~ We only have time to talk about recursion tonight ~

Actually, we have seen recursion ~ I used to think it was very easy when I first started to get started, and it was very difficult to get started slowly. That is, when I realized the code, I encountered a mental obstacle. If I was a sophomore, I learned recursion ~ An important foundation

In the past, I was impressed by calling myself, "shirking responsibility", mathematical formulas, recursive graphs, and the classic Fibonacci series ~

In the past, when reading or writing recursive code, we encountered a mental obstacle, which is hard to explain. However, we need to understand one of the steps and get stuck. Why ~

Through tonight's course, I think it may be because I have a wrong understanding of recursive algorithms ~ We should fully grasp its nature (the most direct contact nature to be taken out) and "not seeking for a thorough solution "~

Tonight, the teacher put the essence of recursive space into a stack. The critical ending condition is at the top, and the bottom layer is n. This is like a grenade, the top is an insurance ring. The code we want to write is insurance.


Ring (critical termination condition) and explosive (recursive relationship, reaction chain )~ This memory is very important, because it makes it clear what you want to write, there will be no mental barriers ~ It's easy to understand. What kind of recursive code should be written ~


To write recursive code, you must first introduce the critical termination condition and recursive relationship, and then implement the code !!! To analyze the time complexity of recursion, you must first write a recursive analysis !!! Recursion: focuses on "returning" and turning to "returning!

For example, the Fibonacci series f [n] = f [n-1] + f [N-2], the analysis is T (n) into two T (n) (T (n-1) and T (n-2) are seen as T (n), is T (n) = 2 T (n) + O (1 ),

Every T (n) is changed to 2 T (n), and there are n, that is, the complexity of O (2 ^ n ~ (There is also an O (n), which is ignored )~ <Note! The two words "change" here are very important ideas!>


There is also the full arrangement ~ Classic ~


Perm (n) = (changed to) rn (perm (n-1) + rn-1 (perm (n-1) ...... r0 (perm (n-1 ))

Recursive Algorithms are almost all about finding the relationship between f (n) and f (n-1) or f (n/2!


I believe these are actually very simple. I know that the direction I understand is wrong, that is, the method of opening is wrong, so I learned the algorithm so slowly !!! Come on !!!


Q: Is recursion a loop?

What is called in the method body is recursion, and there is no

Many beginners are often confused about recursion and spend a lot of time on it. In fact, the example in the teaching material is very classic, but it is a bit nagging. Beginners will see big data. Programming is to solve the problem. In reality, many problems are relatively simple and not as complicated as the tower of Hanoi. We do not need to investigate how recursion is implemented. We just need to use recursion to solve some problems for us.

First, let's look at an example:

There is a Febonacci sequence:

, 13, 21, 34 ........

The problem is: Find the nth number in the sequence.

Since its function prototype is: f (n) = f (n-1) + f (n-2)

It is easy to write code with recursion, which does not bother at all:

Int Febc (int n ){

If (n <3) return (1 );

Else

Return (Febc (n-1) + Febc (n-2 ));

}

Oh ~~~~~ Maybe you will say that recursion is too simple. It's just a mathematical model.

Actually, the procedure of recursive functions is to call itself. There are some problems that can be easily solved by recursion. You will be surprised when it is simple.

We usually start from the beginning, while recursion starts from the end. For example, the above function, when n> 3, it obviously can only turn to n-1, N-2. While (n-1)> 2, (n-2)> 2, they turn to :( N-1)-1, (n-1)-2; (n-2)-1, (n-2) -2; then, until (n-k) <3, (n-k-1) <3, the function Febc finally has the return value 1, which is then calculated from the beginning and then until n.

Through the above example, we know that recursion must have a condition for stopping. Otherwise, recursion will not be able to stop. In the preceding example, if (n <3) return (1); is the condition for stopping.

However, the cost of recursion is huge: it will consume a lot of memory !! In recursive loops, the stack is used, and the stack resources are very limited. In the above example, you can only use a small n value. If n = 20, that is, Febc (20), it will call the Febc (n) function more than 10000 times !!! In the preceding example, loops are easily written:

/* Using turboc2 */
Int febc (int );
Main ()
{
Int n;
Scanf ("% d", & n );
Febc (n );
}

Int febc (int n)
{
Int a [3], I;
A [0] = a [1] = a [2] = 1;
For (I = 3; I <= n; I ++)
A [I % 3] = a [(I + 1) % 3] + a [(I + 2) % 3];/* implement Febc (I) = Febc (I-1) + Febc (I-2 )*/
Printf ("\ n % d \ n", a [n % 3]);
}

If you are interested, enter a larger n value and compare the two function compute speeds. Of course, recursive errors may occur if n is too large. Don't yell at me if it's dead ~~~ I have already reminded you :)

Now let's take a look at the cycle from 1 to 100:

/* Turboc2 */

Main ()

{Int I, n;

For (I = 1; I <101; I ++)

N + = I ;}

This is simple and there is nothing to say. But can you write the corresponding recursive function ?... Remaining full text>

Implementation of a recursive algorithm

Shanlang, I am also doing this now. Currently, there are few people doing this, and the technology is closed.
I analyzed the direct selling software of Iris, which should be structured in a tree.
Make a remarks field in the database and store it as follows.
1
11
12
13
111
112
121
122
131
132
1311
13111
1312
13121
It is too troublesome to query the database recursively. Slow
You need to record useful information with redundant fields when entering members.
My QQ account is 30635099. If you have time, we can have in-depth discussions. I also use asp to implement it.

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.