DSA_07: recursive algorithm

Front to explain the complexity of the analysis, arrays, linked lists, stacks, queues, recursive algorithm to explain today.

 

Recursive, it can be said with wide general front of these underlying data structure uses, in many algorithm, which is very basic, commonly used.

A few simple examples:

  1. Before Binary Tree, in order, a subsequent traversal recursion are available.

  2. recursive depth-first algorithms, such as maze pathfinding, maze generation.

  3. fast row, recursive merge sort is available.

  4. recursive binary search is available.

  5. recursion available to solve practical problems.

Similarly, the use of recursion also numerous. Want to correct, to flexible application of the algorithm, need to have some of its characteristics.

 

In this paper the algorithm through an example of a learning-based techniques and gives various properties of the algorithm.

 

Example: There are N steps of the stair, you can cross a 1 1st, 2nd or 3rd order, then you have completed this step N-order how many different moves it?

If you have encountered this problem, you will find it very simple. But if you do not recursion, you will find it very difficult, almost impossible to think clearly human brain, behind the explanation is given.

 

Direct methods are given here:

  This N-order finish how many moves? Then you can span a 1-3 order to make this method finish of order N with a F (N).

  Whether the order of the path is equal to go N: N-1 order went methods come + N-2 + go order process order Method N-3?

  Of course, there may need to take time to savor it.

  You can understand: N-order can come up from the N-1 order, also went up from N-2 order, it can also go up from N-3 order, to say whether or not understand it?

 

Therefore F (n) = F (n-1) + F (n-2) + F (n-3).

Then the termination condition: F (1) = 1, F (2) = 2, F (3) = 4. I believe this can think clearly.

Therefore the following codes:

int Fn(int n)
{
    if (n == 1) return 1;
    if (n == 2) return 2;
    if (n == 3) return 4;
    if (n <= 0) return -1;
    return Fn(n - 1) + Fn(n - 2) + Fn(n - 3);
}

Now, let's look at an exploded view of the algorithm:

    

Do you find it a problem?

    F (4) is repeatedly calculated! ! !

As N increases, more and more will be counted twice, the performance overhead too! ! !

Looking back, why the human brain can not handle over it? As you can see, its complexity is O (3 ^ n), second only to the factorial complexity, your brain can withstand such an explosion index it?

 

Recursion consume stack memory, the cost of space is very large, carefully blast stack.

How recursive algorithm to optimize it? On the one hand by the recursive iterative method writable, and can be used tail recursion optimization, not elaborate here.

There is a time limit, it will give a large amount of test data buckle brush title in force and other places, what can we make very little time to run it?

For the purposes of this question, if I tell you, steps up to 30 bands, see the code:

class Solution
{
public:
    Solution() { gen(); }
    int Fn(int n)
    {
        if (n <= 0 || n > 30) return -1;
        return val[n - 1];
    }

private:
    int val[30];
    void gen()
    {
        val[0] = 1;
        val[1] = 2;
        val[2] = 4;
        for (int i = 3; i < 30; ++i)
            val[i] = val[i - 1] + val[i - 2] + val[i - 3];
    }
};

As a result, the original problem can be solved recursively, directly into the look-up table, and all of a sudden have the time and space complexity becomes O (1).

How many moves do when we look at 30 steps of the stair?

  Fn(30) = 53798080

More than 50 million, which further validate the person is unable to complete such a large calculation.

 

Stairs: stay button 70th title.

With a lookup table, let's look at the record:

    

 

To conclude recursive algorithm and a number of caveats:

  1. recursive three conditions need to be met:

    a. solution of a solution of the problem can be decomposed into several sub-problems.

    b. The problem with sub-problems after decomposition, in addition to the different size of the data, solving thinking exactly the same.

    C. to recursive termination condition.

  2. How to write recursive Code:

    a. Find out how to break down big problems into small problems of law.

    b. Based on this write recursive formula.

    c. further scrutiny termination condition.

    D. recursion formulas and finally translated into a code termination conditions.

  3.  recursive code to be vigilant Stack Overflow

  4.  how to rewrite the code for non-recursive or recursive coding how optimal recursive codes

  5. Recursive pros and cons:

    Lee is very expressive recursive code, write up is very simple.

    Disadvantages is the space complexity is high , there is the risk of stack overflow , there is double counting , excessive function calls will be time-consuming issues.

 

About this being the end of the recursive algorithm, optimized with regard to its non-recursive and recursive transformation article does not explain in depth, which in the future will be to explain many times.

 

Guess you like

Origin www.cnblogs.com/teternity/p/DSA_07.html