Simple understanding of recursion and dynamic programming

1. recursive definition

Simple, recursion is itself a concept can be used to explain, for example, a dictionary, each word of explanation is to rely on other dictionary words to explain. Generally, recursive problems encountered in most of the computer is to break down a problem into smaller sub-scale problem solving, and then merge.

Recursive nature

One problem with the recursive nature, most of them have two characteristics, a first state transition equation is a recurrence equation, such as solving factorial, n-! = n * (n-1) !, will solve the factorial of n n-1 is converted to solve factorial. The second feature is the termination condition, a recursion is to solve a class of problems, there must be a result

not a recursion, there is a termination condition is simplified when the problem size is small enough when you can direct the answer. When solving factorial, when the problem had when he was a direct output 1.

    int f(n)
    {
       if(n==1)//终止条件
         {
            return 1;
         }  
          else
           {
              return n*f(n-1);//递归方程
           }  
     }

Recursive price

When recursively, a recursive function is repeatedly invoked, layer by layer to enter, after the termination conditions arrive, and then out layer by layer, in order to ensure the accuracy of the results, operation results and status of the function of each layer must be saved are stacked in the assigned system, when a large amount of data, the recursive space occupied and run time will be very scary, efficiency is very low

here and re-introduce a recursive similar but higher efficiency in the implementation of the dynamic programming algorithm .

The nature of dynamic programming

Recursive algorithm for solving the low overhead issues, and Dynamic programming algorithm to solve the problem is from a low-top, also requires the state transition equation equation and initial conditions, compared to the advantages of recursive algorithms, dynamic programming algorithm does not require repeated calls itself function, do not need to store the state of each layer function, and therefore it takes time and space have to spend much less.

Factorial dynamic programming algorithm

    int f(n)
     {
         int array[n+1],i;//array[i]表示i的阶乘
          array[1]=1;
           for(i=2;i<=n;i++)
               { 
                  array[n]=n*array[n-1];//状态转移方程
                }
            return array[n];
      }



This algorithm, regardless of time or in space better than the recursive algorithm. However, for some issues, recursive algorithm less code, better understanding, such as the Tower of Hanoi, if solved by dynamic programming algorithm, time consuming, although far less than the recursive algorithm, but also realize the difficulty is greater than the recursive algorithm.
Personal point of view, we welcome the discussion, treatise

Guess you like

Origin www.cnblogs.com/liveformyself/p/11518864.html