[soft test] data structure and algorithm basis - common algorithm design ideas (recursion and violent recursion)

1. What is recursion?

Recursion is an algorithm design idea

2. The core idea of ​​recursive algorithm

Recursion solves a problem by breaking it down into smaller sub-problems, and then solves these sub-problems to arrive at a solution to the original problem.

Recursion refers to converting a problem into a sub-problem of the same kind of problem with a reduced scale. There is a clear condition (base case) that does not need to continue recursion. When the result of the sub-problem is obtained, the decision-making process does not record each sub-problem. solution. When repeated calculation occurs in recursion, the calculated value needs to be saved in the cache table for convenience.

3. What is violent recursion?

Violent recursion is a special kind of recursion.
Violent recursion is mostly reflected in the way of brute force enumeration

4. The core idea of ​​violent recursion

Violent recursion continuously enumerates various possible solutions and recursively verifies whether these solutions meet the requirements of the problem.

Violent recursion transforms the problem into a sub-problem of the same kind of problem with a reduced scale, and there is a clear condition (base case) that does not need to continue the recursion. When the result of the sub-problem is obtained, the decision-making process does not record the result of each sub-problem. untie.

5. Precautions for using recursion and violent recursion

A recursive algorithm is inefficient because it repeatedly computes many subproblems. In practical applications, more efficient algorithms are usually used to solve similar problems, such as dynamic programming.

Violent recursion is usually suitable for situations where the solution space of the problem is limited, the cost of enumeration is not high, and the accuracy of the solution is not high. However, brute-force recursion is often less efficient, especially when the solution space of the problem is large. Therefore, in practical applications, it is necessary to analyze and design in combination with specific problems, and select appropriate algorithm strategies and optimization methods to improve the efficiency and performance of the algorithm.

6. Common applications of recursion

6.1 Solve the Fibonacci sequence.

Here is an example of Java code using recursion:

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

This function uses recursion to calculate the nth term of the Fibonacci sequence.
If n is less than or equal to 1, the function returns n directly.
Otherwise, the function calls itself to calculate the sum of the first two Fibonacci sequence entries, and returns this value.

6.2 Towers of Hanoi problem

6.3 Eight Queens Problem

6.4 Cattle grazing problem

6.5 Poker problem

6.6 Full row of strings

Guess you like

Origin blog.csdn.net/wstever/article/details/132133427