The branch restriction method is similar to the Backtracking Method, and is also an algorithm used to search for the problem solution on the spatial tree T of the problem. However, in general, the branch restriction method and the Backtracking Method have different targets. The solution objective of the backtracking method is to find all the solutions that meet the constraints in T, while the solution objective of the branch restriction method is to find a solution that meets the constraints, or, in the solution that satisfies the constraints, find a solution that makes a certain target function value extremely large or extremely small, that is, the optimal solution in a certain sense.
Because of the different solution objectives, the branch restriction method and backtracing method have different search methods on the solution space tree T. The Backtracking Method searches for the solution tree T in depth-first mode, while the branch restriction rule searches for the solution tree T in width-first or minimum-consumption-first mode. The search policy of the branch restriction method is: in the extension node, the Mr becomes all of its son nodes (branches), and then selects the next extension point from the current dynamic node table. In order to effectively select the next extended node to accelerate the search process, a function value (Restriction) is calculated at each dynamic node, and based on the calculated function values, select the most favorable node from the current active node table as the expansion node, so that the search moves toward the branch where the solution space tree has the optimal solution, so as to find an optimal solution as soon as possible.
The branch restriction method usually searches for the spatial tree of a problem in a breadth-first or least-consumed (maximum-benefit)-First manner. The space tree for solving the problem is an ordered tree that represents the space for resolving the problem. It is common to have a subset tree and an arrangement tree. When you search for the spatial tree of a problem, the branch restriction method and the Backtracking Method are different for the current extension node. In the branch restriction method, each active node has only one chance to become an extension node. Once a dynamic node becomes an extended node, all its son nodes are generated at one time. Among these son nodes, son nodes that cause infeasible or non-optimal solutions are discarded, and the other son node quilts are added to the dynamic knots table. After that, remove a node from the active node table to become the current expansion node, and repeat the above node expansion process. This process continues until the desired solution or dynamic knots table is null.
There are some problems that can be well solved by both the Backtracking Method and the branch restriction method, but others do not. Maybe we need some specific analysis-When should we use the branch restriction and when should we use backtracing? The following table lists the differences between the Backtracking Method and the branch restriction method:
Method |
How to search for a spatial tree |
Common Data Structures of storage nodes |
Node storage features |
Common applications |
Backtracking |
Deep Priority Search |
Stack |
All feasible child nodes of the dynamic knots are traversed before being popped up from the stack. |
Find all solutions that meet the constraints |
Branch Limit Method |
Search by breadth first or minimum consumption first |
Queue and priority queue |
Each node has only one chance to become a dynamic node. |
Find a solution that meets the constraints or the optimal solution in a specific sense |
A. a back-to-N Problem
Place n entries on the N * n chess board so that none of the backend servers can attack each other and find out all the matching conditions.
The space tree for solving the problem after N is an arrangement tree. Once a combination is a solution, there is no difference between the solution and the solution. A group of solutions cannot be determined until the leaf node is searched. At this time, we can use the Backtracking Method to systematically search for all solutions to the problem. Because the spatial tree is a feature of the arrangement tree, code writing is very easy. In the worst case, the stack depth cannot exceed n. If we adopt the branch restriction method, N knots will be generated at the first layer of the spatial tree. If we do not consider pruning, N * (n-1) will be generated at the second layer) nodes, so the requirements for queue space are too high. After N, the root cause of the problem is that it needs to find a combination of local solutions rather than a certain optimal solution (in fact there is no optimal solution ). In terms of image, If we manually solve the post-N problem on our own, our idea should be to put a post, to see whether there is conflict or not. If there is a conflict, change the position, in this way, we can use only one chessboard and regularly find all the conditions that meet the conditions. This is the simulation process of the Backtracking Method. However, the Branch restriction rule can be expressed in this way. Take a chessboard and place the next one. Then, take a chessboard and place another one. After each chessboard has one, each Board is broken down into more disks ..., as a result, there are more and more boards, but there is a lack of causal and restrictive connections between solutions and solutions (including local solutions. It is a waste of resources because it cannot be obtained based on the information of the other party.
B. A problem that can be solved using either the backtracking method or the branch restriction method-0-1 backpack Problem
Specify the weight and value of several items and the maximum capacity of a backpack. Find a solution that provides the highest value for storing items in a backpack. In addition to the Backtracking Method and the branch restriction method, this problem can also be solved using dynamic planning. However, here we mainly discuss the comparison between the first two methods.
The spatial tree for solving the 0-1 knapsack problem is a subset tree. The required solution is of the optimal nature.
If we use the backtracking method to solve this problem, we adopt the following search strategy: as long as the left son node of a node is a feasible node, we can search for its left subtree. For the right subtree, we need to use the greedy algorithm to construct an upper bound function [[this function indicates the maximum capacity possible for the subtree of this node (because it is only possible to change the 0-1 knapsack problem to a backpack problem. greedy Algorithm, therefore, this upper-bound function is not an upper-bound function in most cases)], and searches only when the value of this upper-bound function exceeds the current optimal solution. As the search process advances, the optimal solution is constantly enhanced, and the search restrictions become increasingly strict.
If we use the branch limit method to solve this problem, we also need to use the greedy algorithm to construct the upper bound function. What's different is that, this upper-bound function does not determine whether to enter the subtree of a node to continue searching, because we cannot know what the optimal solution has been obtained before it reaches the leaf node. Here, we use a maximum heap to implement a priority queue for the active nodes. The value of the upper bound function will be used as the priority. Once a leaf node becomes an extended node, it indicates that the optimal solution has been found.
We can see that it is feasible to use two methods to solve the problem of 0-1 backpacks. In contrast, the Backtracking Method is easier to understand, but this is a problem of finding the optimal solution, because priority queue processing is adopted, different nodes no longer have no mutual restraint and connection as the post-N problem, and the processing effect is as good as that with the branch limit method.
C. A problem that is suitable to be solved using the branch restriction method-Wiring Problem
The printed circuit board divides the cabling area into N * m square arrays. Accurate Circuit Wiring problems require determining the shortest Wiring Scheme connecting the midpoint of Square A to the midpoint of square B. During cabling, the circuit can only be routed along a straight line or right angle. To avoid line intersection, the square of the line is blocked, and other lines are not allowed to pass through the blocked square.
The solution space of the wiring problem is a diagram, which is suitable for the queue-type branch limit method. Start from position a and use it as the first extension node. The squares adjacent to the node and reachable are added to the active node queue and marked as 1, indicating that the distance from them to a is 1. Then, the worker node is taken from the active node queue as the next expansion node, and the square adjacent to the current expansion node and unmarked is marked as 2, and stored in the active node queue. This process continues until the algorithm searches for the target square B or the active node queue is empty (indicating no path ).
Now let's look at the above two figures. The left figure shows the process of branch restriction. In the beginning, the active nodes in the queue are marked with a grid marked with 1. After a round, the active nodes are changed to the grid marked with 2, and so on. Once square B becomes a dynamic node, the optimal solution is found. Why must this path be the shortest? This is determined by the characteristics of this search process. If there is a shorter path from A to B, Node B will be added to the live node queue earlier and processed.
The right figure shows the shortest path between A and B. It is worth mentioning that although we can know the path length from the starting point in the search process, we cannot directly obtain the specific path description. In order to construct a specific path, we need to trace back from the target square to the starting square, and gradually construct the optimal solution, that is, moving the distance of each tag to an adjacent square that is 1 less than the distance of the current square, until the start square is reached.
Now let's take a look at why it is quite inefficient to use backtracking. The backtracking method is based on the depth priority principle. If we set a fixed priority for search in the top, bottom, and left directions, the search will continue along a path until it switches to another sub-path, but we cannot determine the correct path direction at first, which leads to blind and waste of searching. Even if we find a path from A to B, we cannot guarantee that it is the shortest path in all paths, this requires that all the paths in the entire region be searched one by one to obtain the optimal solution. Because of this, the wiring problem cannot be solved using the Backtracking Method.
There are also many problems similar to wiring that can provide a good display space for the branch restriction method. In the just-concluded ACM competition in the Beijing division at the end of October this year, there was a very similar problem with the wiring problem, which could be achieved through the branch restriction method.
Let's briefly describe this problem. For example, in the left figure, B1, B2, B3, and B4 represent a snake's body. B1 indicates the snake's head (the snake's body size, starting position, and obstacle position are obtained by the input ), search for the shortest path between two fixed point snake headers and point () in the same range. The requirements of the question are simplified compared with those of the wiring problem. For example, the end point is fixed and only the length of the shortest path is required. No specific path is required, that is to say, it can save the start point of backtracking, but it also has a promotion that requires some processing to solve, that is, the direction of the snake to be moved, except not the obstacle, it cannot be a part of its own body. For example, if a snake moves from the position on the left, it cannot move up or to the right, but it can only move to the position on B1 below to form the right.
How can we solve this problem? A framework can be used to solve the wiring problem, but the obstacles on each node need to change. It would be easy to think of if we only mark the snake as the same mark as the obstacle at each node, however, this solution is not conducive to the promotion of the obstacle situation from a grid to the obstacle situation of adjacent grids. A more effective way is to describe the snake body as a fixed-length queue. The first queue is the snake tail, and the last queue is the Snake Head (if it is for the sake of Train of Thought consistency, it can also be processed in turn, but this should use a two-way Queue), so that when we promote from a square to an adjacent square, we only need to process this queue step by step to get a new description of the snake queue. Then, the queue and the square itself can be used to form a structure or class. In the process of solving the problem, the queue-type branch limit method is still used, the element in the queue is the structure or Class Object we define. Using STL and other generic technologies, this process is easy to implement.
Struct position // Describe the structure of a location { Int posx, Posy; }; Struct snkenode // Describe the structure of a node in the branch limit queue { Position Square; // The Position of the square. List <position> snake body (snkelength ); // Describe the queue of the snake body. Each snake in the grid still uses position to describe }; |
In this way, we use queue <snkenode> nodelist (0) to create this queue, and the subsequent process is basically consistent with the wiring problem.