Data Structures and Algorithms (XI) recursively

Recursion


What is recursion?

Program that calls itself recursively called programming skills (recursion) . As a recursive algorithm widely used programming languages. A procedure or function calls itself directly or indirectly in a way, it is usually a large complex layers converted to a problem with the original problem similar to the smaller problem to solve in its definition or description, Recursion only requiring less program can describe the problem-solving process is repeated calculation needed, greatly reducing the code size. In that the radio capability recursive finite set of statements to define the object.

Generally, recursive need to have the boundary conditions, recursive and recursive return of the advance period. When the boundary condition is not satisfied, recursive forward; when the boundary conditions are met, the recursive return.

Each of the advance is actually a converted partial recursive thought, to a large complex layers converted to a problem with the original problem similar to the smaller problem to solve, the same thought and thought this divide and conquer algorithms, divide and conquer algorithm is different from bubble sort, selection sort algorithms, it does not have specific operational procedures, it is an idea to solve the problem.

Each of the advance and divide and conquer algorithm

When we solve certain problems, since these data are quite a few problems to deal with, or the solution process is quite complex, making direct methods in quite a long time, or simply can not be obtained directly. For such problems, we tend to put it down into several sub-problems, find solution obtained after several sub-problems, and then find the right way to put them into seeking the solution of the whole problem. If these sub-problems also large, difficult to solve, then they can be divided into several smaller sub-problems, and so on, until a solution can be obtained directly so far. This is the basic idea of ​​divide and conquer algorithm of divide and rule.

Before learning the array of array elements to find a way binary search example belongs to divide and conquer algorithm.

Backtracking algorithm and return segments

Backtracking is actually a similar attempt to enumerate the search process, the main problem is to find a solution in search attempt process, when found to have been solved condition is not satisfied, on the "back" to return to try a different path. Backtracking is a search method selected from the preferred, optimal selection by the forward search conditions to achieve the goal. But when a step to explore, discover original choice is not superior or to meet its target, it is a step back to re-select, this dead end would roll back some state walk technique called backtracking, backtracking and meet conditions the point is called "backtracking" point. Many complex, large-scale problems can use backtracking, laudatory "universal problem-solving approach".

Recursive loop

In simple terms, the cycle is never to return, and recursion is there to go back there (because of the termination condition).

For example, you use your hands the key to open a door, there is a result to find the front door, followed by you and unlocked the door, and then you see a door ... but when you open the when a door, found in front of a wall is no way out, you choose to backtrack - this is recursive

But if you open a door, the same one found in front of them there are, and you immediately open the next door ... until you open the last door to go out, or has not been met at the end of (infinite loop) - This is the cycle .

Finally generalize recursion from the following aspects:

  • Recursive performance: calls the function yourself

    Recursive use: the large and complex problem solving to resolve a number of small problems

    Recursive benefits: less code

    Recursive drawbacks: the space, the function is run based on the stack memory

    Recursive elements: forward segment, boundary conditions, return segment

Published 70 original articles · won praise 56 · views 1961

Guess you like

Origin blog.csdn.net/qq_43624033/article/details/104098786