Algorithm-Branch and Bound

The backtracking method is the solution space tree of the depth-first strategy traversal problem. The branch and bound method traverses the solution space tree of the problem according to the breadth-first strategy . During the traversal process, the possible value of the objective function is estimated according to the connection function for each node that has been processed, and the objective function is selected to obtain the extreme value (maximum or minimum) ) Nodes first perform a breadth-first search, so as to continuously adjust the search direction and find the solution of the problem as soon as possible.

Overview

Dynamic search of solution space tree

The branch and bound method first determines a reasonable bound function, and determines the bounds of the objective function according to the bound function [down, up], and then traverses the solution space tree of the problem according to the breadth-first strategy, and searches all the nodes of the node at one time on the branch node Child nodes, respectively estimate the possible values ​​of the objective function of these child nodes. If the possible value of the objective function of a child node exceeds the bounds of the objective function, it will be discarded, because the solution generated from a node will not be better than the current value. The solution that has been obtained is better; otherwise, it is added to the table of nodes to be processed (referred to as PT for short). The node that makes the value of the objective function the extreme value is selected from the table PT in turn becomes the current expansion node, and the above process is repeated until the optimal solution is found.

Use branch and bound method to solve 0/1 problem

  1. Sort items in descending order of unit weight value
  2. Objective function: ub=v + (W-w)*(vi+1/wi+1), which represents the upper limit of value
  3. Starting from item 1, keep selecting items to put into the backpack, and you need to check whether the space is suitable. If the space is suitable, calculate the objective function and put the node into the PT together. Then select the node with the largest objective function from the PT, and continue to select items until the result is calculated.

Design Ideas of Branch and Bound Method

Assuming that the problem of maximization is solved, the solution vector of the problem is X=(x1,x2,……,xn), where the value range of xi is a finite set Si, |Si|=ri(1<=i< =n). When using the branch and bound method to search for the solution space tree of the problem, first estimate the bounds of the objective function [down, up] according to the bound function, and then start from the root node, expand the r1 child nodes of the root node, thereby forming the r1 species of component x1 Possible values. Estimate the possible objective function value bound(x1) of the r1 children respectively, which means that the objective function value that the subtree lock rooted at the child node may obtain is not greater than bound(x1), which is a partial solution Should meet:

bound(x1)>=bound(x1,x2)>=……>=bound(x1,x2,……,xn)

If the value of the objective function of a child node exceeds the bounds of the objective function, the child node is discarded; otherwise, the child node is stored in the to-be-processed node table PT. Select the node that maximizes the objective function from the table PT as the root node of the next expansion, repeat the above process, when a leaf node is reached, a feasible solution X=(x1,x2,……,xn ) And its objective function value bound(x1,x2,……,xn). If bound(x1,x2,……,xn) is the node with the largest objective function value in the table PT, then bound(x1,x2,……,xn) is the maximum value of the problem, (x1,x2,…… ,xn) is the optimal solution of the problem; if bound(x1,x2,……,xn) is not the node with the largest objective function value in the table PT, it means that there is a node corresponding to a certain partial solution, and its upper bound is greater than bound( x1,x2,……,xn). Therefore, use bound(x1,x2,……,xn) to adjust the lower bound of the objective function, that is, set down=bound(x1,x2,……,xn), and delete the nodes in the table PT that exceed the lower bound of the objective function down, and then The node with the maximum value of the objective function value is selected as the root node of the next expansion, and the search is continued until the objective function value of a certain leaf node is the largest in the table PT.

This design idea describes the nature of the branch and bound method, and everyone must fully understand it.

The key issues in applying the branch and bound method are:

  1. How to determine the appropriate bounding function

    The branch and bound method estimates the possible value of the objective function of a node according to the bound function in the traversal process, and a good bound function must be designed.

  2. How to organize the list of pending nodes

    The table PT can be in the form of a heap or a priority queue

  3. How to determine the components in the optimal solution

    • Save the path from the root node to the node for each expansion node
    • In the search process, the searched tree structure is constructed. When the optimal solution is obtained, the leaf node is continuously backtracked to the root node to determine the components of the optimal solution.

The branch and bound method in graph problem

TSP problem

The TSP problem refers to a traveler who wants to travel n cities, requires each city to experience and only experience once, and then returns to the departure city, and requires the shortest distance.

In general, for a path that is being generated, suppose the set of vertices U=(r1,r2,……,rk) has been determined, that is, k vertices have been determined on the path. At this time, the objective function value of the partial solution The calculation method is as follows:

So the lower limit of lb is ((1+3)+(3+6)+(1+2)+(3+4)+(2+3))/2=14, the upper limit uses the greedy method, and the calculated value is 16, so the bound of the bounding function is [14,16].

Why the bounding function is like this is because

  1. Easy to calculate, because each point has an out edge and an in edge. When calculating, add twice and then divide. It is easier to calculate
  2. The lower limit can be calculated. If there is an edge (1,3), then the distance from 1 to 3 is determined, but 1 needs to have an out side and a back side. Therefore, the lower limit is the smallest in addition to the distance to 3 On the other side.

The problem-solving process and pseudo-code are as follows:

  1. 107. The binary tree hierarchy traversal II - a simple code for this is the easiest breadth-first traversal, we did not use bounding function

The shortest path problem of multi-segment graphs

Let the graph G=(V,E) be a weighted directed connected graph, if the vertex set V is divided into k disjoint subsets Vi(2<=k<=n,1<=i<=k ), so that for any edge (u, v) in E, u must belong to Vi, and v belong to Vi+m (1<=i<k, 1<i+m<=k), then the graph G is called multi-segment In the figure, it is said that s belongs to V1 as the source point, and t belongs to Vk as the end point.

  1. Determine the upper and lower bounds, the lower bound calculates the minimum value of each segment, 2+4+5+3=14, the upper bound uses the greedy method 0->2->5->8->9, the path cost is 18, so the objective function bounds For [14,18]

  2. Suppose that segment i has been determined, and its path is (r1, r2,..., ri, ri+1). At this time, the limit function:

    the meaning of this formula is to add the determined road segments together, and then find the shortest connection of the next road segment , And then add the shortest side of each group. For example, if the walking side (0, 1) has been determined, the objective function is 4 (the determined side) + 8 (the shortest side from 1 to 4, 5) + 5 (the smallest side of the third group) + 3 (the first Four sets of smallest edges)

  3. Each time the edge is selected, if lb exceeds 18, it is discarded. If it is within the range, it is placed in the smallest heap structure at the position of lb, and the smallest node of lb is selected each time, and the breadth-first traversal is performed until the final result is obtained.

example:

  1. 815. Bus routes - difficulty Codes This question is also not used to bounding function, and both can also be used with a depth-first priority to achieve breadth

The branch and bound method in combinatorial problems

Task assignment problem

The task allocation problem requires that n tasks be allocated to n individuals, and each person has a different cost to complete each task, and requires the allocation of the optimal allocation division with the smallest total cost.

  1. Determine the upper and lower bounds, the lower bound is the minimum value of each line 2+3+1+4=10, and the upper bound uses the greedy method to calculate 2+3+5+4=14, so the range is [10,14]

  2. Bound function: Assuming that the task has been assigned to personnel 1~i and the cost v is obtained, the bound function can be defined as,

  3. Use breadth-first traversal to calculate lb. If lb exceeds the upper bound, discard it. If it is within the range, put it into the smallest heap, and select the smallest lb from it for breadth-first traversal until all values ​​are calculated.


example

  1. 690. The importance of staff - a simple code for this question did not use the bounding function, did not do with the sort heap, less examples

Batch job scheduling problem

Given a set of n jobs J={J1,J2,……,Jn}, each job has 3 tasks to be completed on 3 machines, and job Ji requires the processing time of machine j to be tij(1<= i<=n,1<=j<=3), each job must be processed by machine 1 first, then by machine 2, and finally by machine 3. The batch job scheduling problem requires determining the optimal processing order of these n jobs, so that the time from the first job being processed on machine 1 to the end of the optimal job being processed on machine 3 is the least.


For an example of implementation, execute in the order of (J2, J3, J1, J4), and the total completion time is 54


In addition, let’s think about a question. If we start processing with Ji, we estimate that the shortest time for processing is:


This formula is actually very easy to understand. Machine 1 starts to work, and it takes ti1 time, and then machine 2 can process J1. At this time, machine 2 is not idle and the time is the shortest. When all tasks are completed on machine 2, the last completed task is on the machine. 3 on execution. So the shortest time is composed of these three segments.

If you understand this, then the objective function value will be easy to understand.

For a set of scheduled jobs M is a subset of {1,2,...,n}, |M|=k, that is, k jobs have been scheduled, let sum1[k] mean that machine 1 completes k jobs Processing time, sum2[k] represents the processing time for machine 2 to complete k jobs. Now job k+1 is to be processed. At this time, the lower bound of the objective function value of this partial solution is calculated as follows:


The search process of the branch and bound method is:

  1. 207. curriculum - Medium Code

to sum up

The branch and bound method also has routines. First confirm the upper and lower bounds, then find a reasonable bounding function, and finally use the breadth first traversal, sort according to the value calculated by the bound function, and select the minimum/maximum each time for the next breadth first Traverse.

There are few questions in LOCK that require the use of bounding functions, but some questions require more skills. You can contact the questions in this chapter, and the medium-difficulty questions are not too big a problem.

At last

If you like my article, you can follow my official account (Programmer Mala Tang)

My personal blog is: https://shidawuhen.github.io/

Review of previous articles:

algorithm

  1. Algorithm learning plan
  2. Brute force
  3. Divide and conquer
  4. Reduction method
  5. Dynamic programming
  6. Greedy Method
  7. Backtracking
  8. Branch and bound
  9. Algorithm summary

technology

  1. Service framework and registry of microservices
  2. Beego framework usage
  3. Talking about microservices
  4. TCP performance optimization
  5. Current limit realization 1
  6. Redis implements distributed locks
  7. Golang source code bug tracking
  8. The realization principle of transaction atomicity, consistency and durability
  9. Detailed CDN request process
  10. The history of blog service being crushed
  11. Common caching techniques
  12. How to efficiently connect with third-party payment
  13. Gin framework concise version
  14. A brief analysis of InnoDB locks and transactions

study notes

  1. Agile revolution
  2. How to exercise your memory
  3. Simple logic-after reading
  4. Hot air-after reading
  5. The Analects-Thoughts after Reading
  6. Sun Tzu's Art of War-Thoughts after reading

Thinking

  1. Some views on project management
  2. Some thoughts on product managers
  3. Thoughts on the career development of programmers
  4. Thinking about code review
  5. Markdown editor recommendation-typora

Guess you like

Origin blog.csdn.net/shida219/article/details/109260748