Program algorithm Art and Practice: Strategies recursive recursive, and iterative loop

As we all know, it is achieved by calling the recursive function itself, when the function call, each call to be done to save the address, parameter passing, etc. It is achieved through a stack of recursive work, affect efficiency. Recursion is the use of local variables among the stack to save the system function to solve the problem, and that is recursion stack processing pile on the stack pointer to an object in memory, these objects have not be released until after recursively to the last, only free up space.

Recursive cycle efficiency and efficiency

Recursion and circulation are two different typical problem-solving ideas. Of course, not to say that the cycle efficiency will certainly be higher than the recursive, recursion and cycling are two different things, with recursion stack operations, circulation is not necessarily, the two concepts is not a level, try different scenarios do different.

Recursion is usually very straightforward and describes a solution process, and therefore the most likely to be thought of algorithm and implementation. In fact, a recursive loop and have the same characteristics (ie: do repetitive tasks), but sometimes, using loops algorithm does not solve the problem so clearly described steps. Just from algorithm design point of view, a recursive loop and do no better. However, in the actual development, because the overhead of function calls, recursion is often a performance problem, especially in solving the uncertain size of the case. The cycle because there is no function call overhead, so the efficiency will be higher than the recursive. Apart from a few programming language optimized for recursion, the majority language in the realization of recursive algorithm is still very clumsy, which bring about how to convert a recursive algorithm for the round-robin algorithm problem. Conversion algorithm should be based on a thorough understanding of the solution process, and sometimes need a different approach.

  • General recursive algorithm can handle calls, but also to address the need for additional processing inefficiencies through the cycle.
  • Now the compiler optimized for handling multiple calls to the function will have a very good efficiency optimization, efficiency is not necessarily lower than the cycle.
  • Both recursive loop and completely interchangeable. If you use the recursive loop where it is easy to use replacement without affecting the reader, then replace recursion is often good. (Example: factorial recursive implementation and realization cycle.)

To be converted into non-recursive, two steps: first, you can build yourself a stack to save these local variables, replace the system stack; the second step of the recursive call into a loop handle on it. Recursion stack layout used. First, look at the use of the system stack and user stack. System stack (also called core stack, kernel stack) is an area belonging to the operating system's memory space, and its main purpose is: to save the context for nested interrupts, interrupt program site information is in turn pushed the system stack, interrupt return when the pop-up reverse; storage operating system and a parameter between each subroutine call, return values, and return point subroutine (function) of the local variables. User stack is an area of ​​a user process space for the user to save the parameter inter-process call another subroutine, return values, and return point subroutine (function) of the local variables. We write a recursive program is part of a user program, using the user stack.

And iterative loop

<< >> Essay from whom these few concepts for some to understand:. "Loop, iterate, traversal and recursion" The words are a few words of computer technology books often occur, it is known that the words were translated as: loop, iterative, and recursive traversal. At first glance, this seems to have the words and repeat (repeat) related, but some also do not seem to completely duplicate meaning. So that the words in the end what is the meaning of each, what is the difference and the link? which is:

  • Loop (Loop), refers to the condition is met, the same code is repeatedly performed. For example, while the statement.
  • Iteration (iterate), it refers to each one by one in a certain order to access the list. For example, for the statement.
  • Traversing (Traversal), refers to the access tree structure according to certain rules in each node, and each node is visited only once.
  • Recursion (recursion), refers to a function call to continue their behavior. For example, the output programmatically famous Fibonacci sequence.

The difference between these concepts actually are more clear. As for the links between them, strictly speaking, they seem to belong to the scope of the algorithm. In other words, they are simply different means and ways to solve the problem, but in essence it is a way to reach a specific target computer programming.

Iterative algorithm is a fundamental solution to the problem with the computer. It uses the computing speed, suitable for repetitive operation characteristics, so that a set of instructions for the computer (or a certain step) is repeatedly executed, each time the set of instructions (or these steps) performed, all the variables from the original value it launched a new value. Using an iterative algorithm to solve the problem, three aspects need to first determine the iteration variable. The problem can be solved by an iterative algorithm, there is at least a constant value by the old hands to launch a new variable value, directly or indirectly, this variable is the iteration variable; the second iteration of the establishment of relations. The so-called iterative relationship, refers to how the introduction of a formula under which a value (or relationship) from the previous value of the variable. The establishment of an iterative relationship is the key to solving the problem of iteration, you can usually use recursion or push down to complete. The third iteration process control. In the end what the iterative process? This is the question to write iterative programs must be considered. Can not let the iterative process endlessly repeat the down. Iterative process can generally be divided into two cases: one is the number of iterations required is determined value, can be calculated; the other is the number of iterations required could not be determined. In the former case, it is possible to build a fixed number of cycles to achieve control of the iterative process; the latter case, the need for further analysis of the condition of the end of the iterative process. Classical iterative algorithm problems such as rabbits birth problems and issues and the walk up the stairs.

And iterative cycle, starting literally: Iteration: "Diego": turns, turns, replace, alternately, be replaced. "Generation": instead. So iteration means: change of cycle, turns this change is in place, turn instead. The cycle: constant repetition. Or an iterative loop, the loop body code is divided into fixed cycle loop body, and change.

In a constant cycle, such as:

 

for(int  i=0; i < 8; i++){
	echo 'Welcome to AlgrithemMagic';
}

Achieve iteration: 

 

int sum = 0;
for(int i = 1; i <= 1000; i++ ){ sum += i; } 

 

The above is a common iteration of incremental iterations. There are similar decreasing iteration, iterative delivery multiply. Benefits iterations: Iteration reduce redundant code, improve the utilization and dynamic code.

Loop, iteration and recursion

Design ideas and the difference between a recursive algorithm iterative algorithm is: function or whether they have the convergence of the algorithm, if and only if there is an expected effect of a convergence algorithm, a recursive algorithm is feasible, otherwise, you can not use a recursive algorithm. Of course, in theory, all recursive functions can be converted to iterative function, and vice versa, but the cost is usually relatively high. However, the structure of the algorithm, the recursive structure declaration does not always converted into an iterative structure, because the structure of the extended concept itself is recursive, iterative method can not be realized at the design stage, it's like moving things polymorphism the same can not always be achieved by the method of static polymorphism. This is why the structural design, usually not the cause recursive manner using the iterative manner, a very typical example is similar to the list, using a recursive definition and simple, but the memory is defined (array mode) and its definition calls handling instructions becomes very obscure, especially in the face of chain, graphs, grids and other issues, from the description of an iterative manner to achieve have become unrealistic.

Recursion is actually difficult for programmers to facilitate the machine. As long as it can get mathematical formulas easily write programs. Advantages are easy to understand, easy to program. But recursion stack mechanism is implemented, goes one level deeper, we must take up a stack data region, some deep nesting algorithm, recursive run out of steam, the memory space will collapse in the end, but also with recursion to a large number of function calls, it also has a lot of extra time overhead. So in great depth, it would look very bad time and space. The disadvantage is that the cycle is not easy to understand, difficult when writing complex problems. The advantage is high efficiency. Running time increase because the number of cycles increases, no additional overhead. There is no increase in space.

Local variables occupy a one-time memory, that is O (1) complexity of the space, and for recursion, each function call must push, then the space complexity is O (n), and a linear relationship between the number of recursions .

Recursive loop, then switch to program implementation, usually to maintain a stack of his own, back to the state. If a recursive loop when the program switch simply does not need to maintain the stack, that is actually just wrote this recursive procedure evident in some sense, not necessarily written in recursive form. However, many recursive procedure is to use the function itself auto variables on the recording state of the system stack, for backtracking. In principle, all recursion can all be eliminated, the price is likely they have to maintain a stack. And I personally think that, in many cases using recursion is still necessary, it is often able to break down complex problems into simpler steps, and it reflects the nature of the problem.

Recursion is actually using the system stack, to achieve the function itself calls or procedure call each other. In the process leading to the border, the address will be saved to step down, and so know the boundary, and then last-out operation is performed, which, as we loaded barrels, every East and West can only once at the top, acquired when first put aggressive but finally removed. Recursive data transmission is similar. But infinite recursion can not go on, you must stop calling itself under certain conditions, so its value should be a clear boundary. We loaded it into barrels, we can not always unlimited installed inside, things must be taken out at some point. The process is relatively simple recursive factorial function, you can take a look. But recursive calculation method, often determines its efficiency is very low, because the data have to constantly push the stack. But recursive algorithm as a basis for comparison, its role can not be ignored.

 

https://blog.csdn.net/songzitea/article/details/48498119

Guess you like

Origin www.cnblogs.com/feng9exe/p/11908975.html