algorithm
-
Backtracking
- Depth-first algorithm divide-and-conquer-and-bound method (breadth-first method)
- n queens problem
- Given an N*N chessboard, place N queens on the board, and satisfy that any two queens in the N queens are not in the same row, column, and slash (forward slash, backslash)
- Because they are all arranged in rows, it is good to express the column number at the end
- Judge the same slash:|Row Qi-Row Qj|=|Column Qi=Column Qj|
- Judge the same column: Qi column = Qj column
- Non-recursive code method and recursive code method
-
divide and conquer
- The design idea is to decompose the problem that is difficult to solve directly into some smaller-scale identical problems, so as to break through each one and divide and conquer. Recursion is the twin brother
- There are two basic elements of recursion: the boundary condition is when the recursion terminates, also known as the recursive exit; the recursive mode, which is how the big problem is decomposed into small problems, is also called the recursive body
- The divide-and-conquer algorithm has three steps on each level of recursion (merge sort, quick sort)
- Decomposition, decomposing the original problem into a series of sub-problems
- Solve to solve each subproblem recursively. If the subproblem is small enough, it is solved directly
- Combine the solutions of the subproblems into the solution of the original problem
- Maximum Fields and Questions
-
Dynamic programming method√
-
Similar to the divide and conquer method, the difference is that the sub-problems of the dynamic programming method are often not independent; it is used to solve the global optimal solution of problems with certain optimal properties
-
It is a very effective algorithm design structure, which has the following two properties, and uses the dynamic programming method
- Optimal substructure, if the optimal solution of a problem contains the optimal solution of its subproblems, it means that the problem has optimal substructure
- Overlapping sub-problems, the recursive algorithm to solve the original problem can repeatedly solve the same sub-problem, the time constant of the table lookup
-
step
- Find out the properties of the optimal solution and characterize its structure
- recursively define the value of the optimal solution
- Calculate the optimal value in a descendant way
- According to the information obtained when calculating the optimal value, an optimal solution is constructed
-
0-1 knapsack problem
- Both time complexity and space complexity are O(N W) item quantity knapsack weight
-
matrix multiplication
- dynamic programming
- Time complexity O(n to the 3rd power)
- Space complexity O(n²)
- Calculation method: M1(20 5) M2(5 35)=20 5* 35 The best is generally to eliminate the largest number first
-
Longest common sequence O(n 2 to the nth power)
-
shortest route problem
-
-
greedy method
- Like the dynamic programming method, it solves the optimization problem. The difference is that the greedy method only makes choices based on the only current information. Once a choice is made, it will not change in the future. It is not considered from the overall optimal, but a local optimal in a certain sense, an approximate optimal solution. ps: Find change according to the largest amount to find 15, there are 1 5 11 greedy 11 four 1 optimal 555
- With the following properties, the optimal solution can be obtained by greedy method
- optimal substructure
- greedy choice property
- Partial knapsack problem O(nlog2n)
- Items can be partially loaded into the backpack
- Put it in first according to the maximum unit weight value (v/w)
- Put the highest value first
- Put the minimum weight in first
- Items can be partially loaded into the backpack
- activity selection