[soft test] data structure and algorithm basis - common algorithm design ideas (retrospective analysis)

1. What is backtracking?

Backtracking analysis is an algorithm design and analysis technique
Backtracking helps us find all possible solutions to a problem.
Backtracking is an algorithm design idea that finds all solutions by exploring all possible candidate solutions.
Backtracking is a general method for solving problems with a depth-first search strategy.
Backtracking analysis is a very useful algorithm design and analysis technique.

2. The core idea of ​​backtracking method

(1) Core idea 1
starts from the initial state of the problem, and tries all possible candidate solutions.
When it is found that the candidate solution does not meet the constraints or the objective function of the problem,
it "backtracks" to the previous step, changes the state of the candidate solution, and then Go ahead and try.
This process continues until a candidate solution that meets the requirements is found, or all possible candidate solutions are searched.

(2) Core idea 2
In backtracking analysis, we decompose the problem into some sub-problems and try all possible solutions.
If one solution doesn't work, we go back to the previous subproblem and try other solutions.
By constantly backtracking and trying, we can eventually find all possible solutions.

3. Application scenarios of backtracking method

Backtracking is often used to solve combinatorial optimization problems.
Backtracking analysis can help us find all possible solutions to a problem.
Backtracking analysis is often combined with recursive algorithms to solve complex problems.
Backtracking is often used to solve the following problems:

  • Permutation and combination problem: Given a full permutation or combination of n elements.
  • Subset Problem: Given all subsets of n elements.
  • Path Problem: Given a directed acyclic graph, find all the paths from the start point to the end point.
  • Interval Problem: Given an array, find all consecutive subarrays that satisfy a given condition.
  • Bin packing problem: Given some small balls of different sizes and a large box, it is required to pack these small balls into the large box so that the number of small balls loaded is the largest.

Fourth, the advantages and disadvantages of the backtracking method

It should be noted that the time complexity of the backtracking method is usually high, so it needs to be chosen carefully in practical applications.

5. Common applications of retrospective analysis

5.1 Solving the traveling salesperson problem

Question: A travel salesperson needs to visit some cities, and each city can only be visited once to minimize the total distance traveled.

The solution for backtracking analysis is:
first, we choose a city as a starting point and mark it as visited.
Then we try to choose the next city and calculate the distance from the origin to this city.
If this distance is smaller than the previous distance, we update the distance and mark the city as visited.
Next, we proceed to select the next city and repeat the above process.
If we have visited all cities, we have found an efficient solution.
If no effective solution is found, backtrack to the previous city and try another city as the next city.

5.2 Solving the Eight Queens Problem

Question: On an 8x8 board, place 8 queens so that the queens will not attack each other.

The solution for backtracking analysis is:
first, select a position to place the queen, and mark this position as already occupied.
Then, try to pick the next position and check if it can place the queen.
If this position can place a queen, we continue to choose the next position and repeat the above process.
If all queens have been placed, an efficient solution has been found.
If no valid solution is found, backtrack to the previous position and try another position as the next position.

Guess you like

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