Recursive algorithm converts non-recursive algorithm

Transfer: https://blog.csdn.net/fbz123456/article/details/50959412  
 

Recursive algorithm is actually a divide-and-conquer approach, which the complex problems into simpler problems to solve. For certain complex problems (e.g. problems hanio column), recursive algorithm is a natural and logical manner desirable to solve the problem, but the efficiency is usually poor recursive algorithm. Therefore, in solving certain problems, often using a recursive algorithm to analyze problems, non-recursive algorithm to solve the problem; In addition, some programming languages do not support recursion, which requires the recursive algorithm into a non-recursive algorithm.
    The recursive algorithm into non-recursive algorithm, there are two methods, one is directly evaluated (iteration / cycle), no backtracking; the other is not directly evaluated, required backtracking. The former intermediate results using some variables, called direct conversion method; stack which uses intermediate results, known as the indirect conversion method, two methods are discussed below.

1. The direct conversion method
directly converts tail recursion method is usually used to remove and unidirectional recursive, recursive structure with the loop structure instead.
Recursive way: simply refers to the recursive process is always in one direction, if the function calls the function 2 1, 2 and function in turn calls the function 1, the case does not belong to the one-way recursion. Fibonacci number of recursive solution can be transferred realized by an iterative method.

Fibonacci number of recursive solution:

int Fib(int n) {
if(n <= 1) return n;
else return Fib(n - 1) + Fib(n - 2);
}

Into an iterative solution:

int Fib(int n) {
if(n <= 1) return n;
int twoBack = 0;
int oneBack = 1;
int cur;
for(int i = 2;i < = n; i++) {
   cur = twoBack + oneBack;
twoBack = oneBack;
   oneBack = cur;
}
return cur;
}

Tail-recursive function is a recursive function calls ending in a recursive way is a special case. It is only a statement of recursive calls, and is placed at the end of the process. When the recursive invocation returns, the return to the next statement in statement one recursive call, and this position is just the end of the program, so the recursive stack may not work saved return address; except return value and reference value, other parameters, and local variables are no longer needed, so the stack can not directly write cycle using non-recursive process.

Factorial function is not a tail recursive. Because after it received the results of the recursive call, you must do it again multiplication before returning calls. However factorial function may be converted into a tail recursive function, for example:

Recursive solution factorial:

int factorial(int n)

{
if(n == 0) return 1;
else

     {
int val = factorial(n - 1);
return n * val;
}
}

Converted to tail recursive solution:

int factorial(int acc, int x)

 {// acc transmission value of 1.
IF (X <=. 1) return ACC;
the else

     return factorial(x * acc, x - 1);
}

The importance is that tail-recursive when the tail call, the caller's return position need not be present in the call stack. When the recursive call returns, which branch directly to a previously saved return address. Therefore, in support tail recursion optimization compiler, tail recursion in time and space are more cost-effective. Iterative algorithm requires a temporary variable, which undoubtedly led to lower spending readability, iterated function program not to be considered as a function recursive function calls, but also for a thread stack space is usually available for less than the available heap space much more, and a recursive algorithm is relatively iterative algorithm requires more stack space!

2. Indirect conversion method
which uses a stack to save intermediate results, obtained according to the recursive function is generally required during execution stack changes. Its general process is as follows:

The initial state s0 push
while (the stack is not empty)
{
de-stacked, the top element is assigned to s;
IF (s result is looking for) is returned;
the else {
find relevant state s1 to s;
the push s1
}
}

Indirect conversion method has many instances in the data structure, such as a non-binary tree traversal algorithm recursive depth-first traversal algorithm of FIG non-recursive and the like, refer the reader to the main teaching relevant content.
 

Guess you like

Origin blog.csdn.net/qq_38446366/article/details/89499257