Algorithm - recursive and iterative

Recursive and iterative algorithms in-depth analysis             

Original link: https: //blog.csdn.net/liujian20150808/article/details/49717427

 

 


 Recursive definition:

Program that calls itself recursively called programming skills (recursion).

Iterative definition:

Iterative feedback process is repeated activities, its purpose is usually to approach a desired result or goal. Each repetition of the process is called an "iteration", and the results will be obtained for each iteration as the initial value for the next iteration.


 Then we have a question from the introduction to compare these two algorithms:

Description Title: QAQ on a staircase, QAQ are in the 0th order, boring QAQ found that the length of his legs, each may be the first-order, second order, third order, and this step a total of order N, so QAQ We went to N-th order total number of species moves?
                                   TimeLimit: 1000ms MemoryLimit: 65536KB

A plurality of sets of test data, each data row contains a number N. (1 <= N <= 30 )
for each set of data:
output reaches the N-th order total number of species moves?


 Problem-solving ideas: You can start from a list until you find the law, the principle is the principle of addition

Algorithm Description: If the series of steps represented by n, an n-represented when someone walked steps, all the different possible moves, easily obtained:
① When n = 1, the law is clear that as long as one kind of cross, that is a =. 1. 1.
② when n is 2, a = stride can also be stepped in two stairs, and therefore, a total of two different
cross-method, i.e. 2 = 2 a.
③ when n = 3, it is possible a step across, step three can cross, can also be a first step across, the second step or two across two cross first step, the second step across the upstairs level, therefore, there are four different cross method, i.e., =. 4. 3 a.
④ when n 4, the three cases are discussed method = cross:
if the first step across a step, then the remaining three steps, there is a clear ③ a3 = 4 (species) across the process.
If the first step across two steps, then the remaining two steps, there is a clear ② a2 = 2 (kinds) cross method.
If the first step across the three steps, then the next left stairs by ① understood there a1 = 1 (species) cross method.
The addition principle, there is a 4 = a1 + a2 + a3 = 1 + 2 + 4 = 7
and so on, there is
a5 = a2 + a3 + a4 = 2 + . 4 +. 7 = 13 is
A6 = A3 + A4 + A5 =. 4 +. 7 + 13 is = 24
A7 = A4 + A5 + A6 =. 7 + 13 is + 24 = 44 is
A8 = A5 + A6 + A7 = 13 is + 24 + 44 is = 81


1. The source code of the recursive algorithm:

int tell ( int n) 
{ 
if (n == 1 ) return  1 ;
if (n == 2 ) return  2 ;
if (n == 3 ) return  4 ;
else  return say (n - 1 ) + say (n - 2 ) + say (n - 3 ); 
}

   


 

Obviously, when the value of n to 34 overtime, which is why?
Let's analyze the process of running the recursive procedure:
When n = 1,2,3, when the recursive function needs to be called only once.
When n = 4 the time, return digui (1) + digui (2) + digui (3) In this case, the recursive function is called three times.
When n = 5 time, return digui (2) + digui (3) + digui (4) In this case, the recursive function is called 1 + 1 + 3 = 5.
When n = 6, when, return digui (3) + digui (4) + digui (5) In this case, the recursive function is called 3 + 5 + 1 = 9.
So, there is
N7 = N4 + N5 + N6 + =. 5. 9. 3 = +. 17
N8 = N5 + N6 + N7 = = +17. 5. 9 + 31 is
N9 = + N6 + N7 + 31 is N8 =. 17 = 57 is 9+
......
this calculation down to n = 34, when the recursive function is called a total of 235,795,681 times already reached the million level, if it is placed on practical work, is a terrible thing, because this type of recursive calculation of the subject, calculation is repeated many times, such as when the time n = 34, n = 1 when the value calculated was repeated 53798079 53798080-1 = (the number of calls can be calculated by the recursive ), double-counting more than 50 million, imagine how inefficient algorithm is, so we can put it another thought, to minimize the number of double-counting algorithm to improve efficiency, the introduction of an iterative algorithm.


2. iterative algorithm source code:

a1 = 1 ; 
a2 = 2 ; 
a3 = 4 ;
for (i = 4 ; i <= N; i ++ ) 
{ 
sum = a1 + a2 + a3; 
a1 = a2; 
a2 = a3; 
a3 = sum; 
}

 

Iteration that time to n = 34, time-consuming or negligible
following analysis of iterative algorithm:
when n = 1,2,3, when the for loop is skipped, and the sum of the output value itself.
When n = 4 when the number of cycles for case 1, calculated sum = 1 + 1 + 1 = 3;
when n = 5, when the number of cycles at this time for two times, the first sum calculated = 3, then assign the value of a2 a1, a2 assigned values of a3, assigned values of a3 sum, new case a1 = 1, a2 = 1, a3 = 3, then the program proceeds to the second adjustment portion cycles, the calculated sum = 5, and then out of for loop output value of 5. the sum
so when n = 6, n = 7 ...... process similar to the process described above.
The main idea of iteration is constantly replacing the old value new value, thus effectively reducing the number of double-counting.
Like the above source code iterative algorithm, when n = 34, double counting n = 1, n = 2 ...... times are 0, the iterative procedure is calculated up to n = 4 n = 34 to process, has been replaced by the new value with the old value. The only time-consuming procedures for cycle time-consuming. For the size N, a linear O (n) order, and is the coefficient of linear 1-order, number of times for loop is n-4.
In summary, when the processing of small circulation problems or other problems factorial recursive algorithm is a very inefficient algorithm, in the case of find a better algorithm, it is best not to use a recursive algorithm.

Guess you like

Origin www.cnblogs.com/wangqiwen-jer/p/11872338.html