Reading Notes: Chapter 3 Recursion of "Algorithm Diagram"

definition:

In mathematics and computer science, a method of using the function itself in the definition of a function. The term recursion is also more commonly used to describe the process of repeating things in a self-similar way. For example, when two mirrors are approximately parallel to each other, the images nested in the mirrors appear as infinite recursion. It can also be understood as the process of self-replication.

example:

  • Once upon a time, there was a mountain, and there was a temple in the mountain. There was an old monk in the temple who was telling stories to the young monks! What is the story? "Once upon a time there was a mountain, there was a temple in the mountain, and there was an old monk in the temple, who was telling a story to the young monk! What is the story?" Tell a story to the little monk! What is the story?...'"
  • A dog came to the kitchen and stole a small piece of bread. The cook raised his ladle and beat the dog to death. So all the dogs came running, dug a grave for the dog, and carved an epitaph on the tombstone for future dogs to see: "A dog came to the kitchen and stole a small piece of bread. The cook raised the ladle and beat the dog to death. So all the dogs came and dug a grave for the dog, and carved an epitaph on the tombstone so that future dogs could see: 'One A dog came to the kitchen and stole a small piece of bread. The cook raised his ladle and beat the dog to death. So all the dogs came and dug a grave for the dog and carved it on the tombstone Epitaph, so that future dogs can see...'"

    Recursion in programming languages:

    The programming technique in which a program calls itself is called recursion. Recursion as an algorithm is widely used in programming languages. A procedure or function in its definition or description has a method of directly or indirectly calling itself, which usually transforms a large and complex problem into a smaller problem similar to the original problem to solve. The recursive strategy only A small number of programs can be used to describe the repeated calculations required for the problem-solving process, which greatly reduces the code amount of the program. The power of recursion lies in defining infinite sets of objects with finite statements.

Recursive condition:

In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. When the boundary conditions are not satisfied, the recursion proceeds; when the boundary conditions are satisfied, the recursion returns.

  • Base case  functions no longer call themselves
  • recursive conditional  function calls itself

Conditions required to form recursion:

  1. The subproblem must be the same thing as the original problem and be simpler;
  2. It cannot call itself indefinitely, there must be an exit, which is simplified to non-recursive state processing.

Disadvantages of recursion:

The recursive algorithm for solving problems is relatively inefficient, such as ordinary loops, etc. Therefore, recursion should be avoided as much as possible, unless there is no better algorithm or there is a specific situation where recursion is more suitable. In the process of recursive calling, the system opens up a stack to store the return point, local quantity, etc. of each layer. Excessive recursion can easily lead to stack overflow and so on.
stack:

Example: Find the factorial

def factorial(n):
    if n == 1:
        return n
    else:
        return n*factorial(n-1)

In the above example, n==1 is the base case, and n $\neq$ 1 is the recursive case.

A stack
, also known as a stack, is a linear list with limited operations. The limitation is that insert and delete operations are only allowed on one side of the table. This end is called the top of the stack, and the other end is called the bottom of the stack. Inserting a new element into a stack is also called pushing, pushing or pushing. It is to put a new element on top of the top element of the stack to make it a new top element of the stack; deleting an element from a stack is also called making a stack or a stack. To pop off the stack, it deletes the top element of the stack, making its adjacent elements the new top element of the stack.

A stack is a linear list restricted to insert and delete operations only at the head.

Stacked data structures use two basic operations: push and pop:

  • Push: Put the data into the top of the stack (array form or serial form), and add one to the top index of the top of the stack.
  • Popup: output (return) the top data, and subtract one from the top data of the stack.

The basic characteristics of stack:

  • First-in, first-out, last-in, first-out.
  • Except for the head and tail nodes, each element has a predecessor and a successor.

Original address:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326343520&siteId=291194637