The branch-and-bound method is similar to the backtracking algorithm, and is a solution strategy based on solving space search problem.
The general steps for solving problems are:
1. By width first strategy traversal solution space tree;
2. In the traversal process, to the processing of each node VI, according to the boundary function, estimated to search down the node may reach the full solution of the possible target function range-boundary bound (vi) =[downi, UPI];
3. From the selection of the objective function to take the extreme value (maximum, minimum) node priority for width first search, so as to continuously adjust the search direction, to find the problem solution as soon as possible.
The boundary function of each node bound (vi) =[downi, UPI] is the key to solve the problem, usually depends on the specific problem. The two common branch-and-bound methods are the queue-type branch-bound method and the precedence queue-type branch-bound method, which select the next node as the extended node according to the first-order FIFO principle and Priority queue priority. The difference between the branch-bound method and the backtracking method is the difference between the target and the search method. The goal of backtracking is to find all the solutions that satisfy the constraint conditions in the solution space tree. The target of the branch-bound method is to find a solution that satisfies the constraint condition, or to find the optimal solution in a certain sense in the solution satisfying the constraint condition, and the backtracking method searches the space tree in depth-first way, The branch-and-bound rules search for solution space trees in breadth first or in the least expensive way. We take TSP as an example to solve the problem by branch-bound method. The TSP problem (traveling salesman Problem) is one of the famous problems in the field of mathematics. If there is a traveling businessman to visit N cities, he must choose the path to go, the limit of the path is each city can only visit once, and finally to go back to the original city, the sum of the minimum path required. Consider the situation shown in the following figure and its cost matrix, assuming the starting city is City No. 1th.
Note the characteristics of the cost matrix, each of which satisfies the requirements of the loop in each row of the cost matrix and has only 1 elements corresponding to each column. Therefore, we can compute the upper bound of the problem with the greedy algorithm. To start the city as the starting city, every time from the current departure of the city issued a number of edges, select the shortest side of the link is not traversed the city, as the next step to reach the city. In this problem, from the city 1, through 1→3→5→4→2→1, path length 1+2+3+7+3=16 as the upper bound, that is, the shortest path length <=16. For the lower bound, a simple method is to add the smallest elements of each row of the matrix directly, in this problem, the path length 1+3+1+3+2=10 as the lower bound, that is, the shortest path length >=10. The better way to do this is to divide the smallest 2 elements in the matrix by 2 and then up the entire line. Because on a path, each city has 2 contiguous edges: Enter the city and leave the city. For each step through the city J, from the nearest last city I came, and then to the next nearest city K go, that is, i→j→k. In this problem, the path length {(1+3) + (3+6) + (1+2) + (3+4) + (2+3)}/2 up rounding equals 4 as the lower bound, that is, the shortest path length >=14. Therefore, the boundary of Dist is [14,16] for the shortest path length dist as the objective function of TSP problem. In the process of solving the problem, if the target function of the 1 partial solution exceeds this limit, the dist decomposition corresponds to the knot point and can be pruned. For 1 path/partial solutions that are being generated, set the vertices that have already been identified (the cities that have been/traversed) as u= (R1, R2, ..., RK), the lower bound of the target function of the division is (twice times the total length of the path that has been traversed + from the beginning to the nearest not traversing the city + The distance from the end point to the most recent not traversing the city + the minimum path cost of entering/leaving the city without traversing the city is divided by 2 and rounded up. Assuming that the path/part being generated is decomposed to 1→4,u={1,4}, the city ={2,3,5} is not traversed, the lower bound is {2*5+1+3+ (3+6) + (1+2) + (2+3)}/2 rounding up equals 16.
The following code enters the number of cities N and the x-coordinate and coordinates of each city, calculating the optimal solution of the TSP problem starting from the first city.
#include <queue> #include <cmath> #include <cstdio> #include <iostream> #include <algorithm
> Using namespace std;
const int inf=10000000;
int low,up,n,used[20],graph[20][20];
struct Node {bool vis[20]; int st;//path starting point int ed;//path end int k;//traversed points int sumv;//path distance int lb;//value of target function bool operator< (cons
T node &p) const {return lb>p.lb;
}
};
Priority_queue<node> Q;
int get_up_helper (int v,int j,int len) {if (j==n) return len+graph[v][1];
int minlen=inf,pos;
for (int i=1;i<=n;i++) {///Use the greedy algorithm to take the smallest edge if (Used[i]==0&&minlen>graph[v][i]) {
Minlen=graph[v][i];
Pos=i;
}} used[pos]=1;
Return Get_up_helper (Pos,j+1,len+minlen);
///Compute upper bound void get_up () {used[1]=1;
Up=get_up_helper (1,1,0);
The lower bound int get_lb (node p) {int ret=p.sumv*2) of the target function of the partial solution;
int min1=inf,min2=inf; Distance from the beginning to the nearest not traversing the city for (int i=1;i<=n;i++) {if (P.vis[i]==0&&min1>graph[p.st][i]) {min1=graph[p.st][
I];
}} ret+=min1;
Distance from the end point to the most recently not traversed city for (int i=1;i<=n;i++) {if (P.vis[i]==0&&min2>graph[p.ed][i]) {
Min2=graph[p.ed][i];
}} ret+=min2;
Enter and leave the minimum cost for each not traversing the city for (int i=1;i<=n;i++) {if (p.vis[i]==0) {min1=min2=inf;
for (int j=1;j<=n;j++) {if (Min1>graph[i][j]) min1=graph[i][j]; for (int j=1;j<=n;j++) {if (min2>graph[j][i]) mi
N2=graph[j][i];
} ret+=min1+min2; }///Up return ret%2==0?
(RET/2):(ret/2+1);
///compute the lower bound void Get_low () {low=0;
for (int i=1;i<=n;i++) {int temp[20]; for (int j=1;j<=n;j++) {temp[j]=grAPH[I][J];
Sort (temp+1,temp+1+n);
LOW=LOW+TEMP[1]+TEMP[2];
} LOW=LOW/2;
int solve () {get_up ();
Get_low ();
node start;
Start.st=1;
start.ed=1;
start.k=1;
for (int i=1;i<=n;i++) start.vis[i]=0;
Start.vis[1]=1;
start.sumv=0;
Start.lb=low;
int ret=inf;
Q.push (start);
Node next,temp;
while (!q.empty ()) {temp=q.top ();
Q.pop ();
if (temp.k==n-1)//If only the last point is left {int pos=0;
for (int i=1;i<=n;i++) {if (temp.vis[i]==0) {pos=i;
Break
} if (pos==0) break;
int Ans=temp.sumv+graph[pos][temp.st]+graph[temp.ed][pos];
Node Judge=q.top ();
If the current path and the value of all the target functions are smaller then jump out and directly output the optimal solution if (ans<=judge.lb) {ret=min (Ans,ret);
Break //Otherwise continue to ask for other possible paths and update the upper bound else {up=min (Up,ans);
Ret=min (Ret,ans);
Continue } for (int i=1;i<=n;i++) {if (temp.vis[i]==0) {Next
. St=temp.st;
Next.sumv=temp.sumv+graph[temp.ed][i];
Next.ed=i;
next.k=temp.k+1;
for (int j=1;j<=n;j++) NEXT.VIS[J]=TEMP.VIS[J];
Next.vis[i]=1;
NEXT.LB=GET_LB (next);
if (next.lb>=up) continue;
Q.push (next);
}} return ret;
int main () {Ios::sync_with_stdio (false);
cin>>n;
int x[20],y[20];
for (int i=1;i<=n;i++) cin>>x[i]>>y[i];
for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {if (i==j) Graph[i][j]=inf; else Graph[i][j]=ceil (sqrt (x[i]-x[j)) * (X[i]-x[j]) + (Y[i]-y[j]) * (Y[i]-y[j]));
} cout<<solve () <<endl; }