Backtracking method【2-5】

Suppose a salesman problem is defined by the following figure, and use the backtracking method to solve the
corresponding shortest tour path starting from node 1 (reaching each vertex exactly once). If bestL is used to represent the current optimal solution generated during the search process, the pruning function L is designed as:

L = the length of the path traveled + the shortest edge related to the current node + the sum of the shortest edges related to all unvisited nodes.

Insert image description here

Then, when walking through the node: 1->3->2, the values ​​of bestL and L are: A (if you start the deep search from node 2 first)
A. 30 and 25

B. 30 and 28

C. 27 and 25

D. 27 and 28

Insert image description here

Backtracking is a recursive algorithm for solving problems. In the backtracking method, we try to solve each part of the problem. If we find that there is a situation that cannot satisfy the problem conditions when solving the current part, we backtrack to the previous step and try other solutions until the problem is solved. When finding the shortest path, we can model the problem as a graph, where each point represents a city and each edge represents the distance between two cities. The salesman needs to start from a starting point, pass through all cities and finally return to the starting point. The path he needs to take is the shortest path we need to find.

For the pruning function L, it can help us avoid exploring unnecessary paths during the search process, thereby improving search efficiency. The calculation method of this function is: the length of the path traveled + the shortest edge related to the current node + the sum of the shortest edges related to all unvisited nodes. The length of the path that has been traveled and the shortest edge related to the current node can be calculated through dynamic programming, and the shortest edges related to all unvisited nodes can be calculated using the Dijkstra algorithm.

Backtracking is a search algorithm that finds all solutions within a problem's solution space that satisfy certain constraints. During the search process, each time a child node is expanded, the current status will be judged to see whether it meets the constraints of the problem. If the conditions are met, proceed to the next step of expansion; if not, then backtrack, return to the previous state and reselect other paths to continue searching.

In the backtracking method, the definition of the current optimal solution refers to
the solution closest to the target solution among all the solutions that have been found during the search process.
This solution is not necessarily the global optimal solution, but it is optimal for the current search state.
During the search process, we will continue to try to find better solutions. While finding better solutions, we may also change the definition of the current optimal solution.
Therefore, in the backtracking method, the current optimal solution is a dynamic concept.

Specifically, when we find a new solution during the search, we compare it with the current optimal solution. If the new solution is better, the current optimal solution is updated; otherwise, we continue to search for other possible solutions.

In some cases, we can use pruning techniques to improve the efficiency of the backtracking method. For example, during the search process, if the solution currently being searched is already worse than the current optimal solution, then we can directly skip this solution to avoid unnecessary searches.

The basic idea of ​​the backtracking algorithm is: go forward from one path, advance if you can, go back if you can't, and try another path again.

Guess you like

Origin blog.csdn.net/CSDN_YJX/article/details/130795596