First, the problem description
A salesperson is going to a number of cities to sell goods and to know the distance (or travel) between cities. He wants to select a route from the station, passing each city once, and finally returning to the station, so that the total distance (or total travel) is minimized.
such as: 1,2,3,4 four cities and their route cost map, any two cities are not necessarily a road to reach.
Ii. understanding of the problem
1. The branch-and-bound method uses the breadth-first search and the optimal value strategy.
2. Using a two-dimensional array to save the information of the diagram A[max_size][max_size]
Where the value of A[i][j] represents the route cost between City I and City J
Once a city does not have a road to another city, it is impossible to have a circuit, no longer looking for it.
Here is a book on a method, there are some errors in the book, after modification can draw the correct answer, in particular through code analysis:
Packageessay;ImportJava.util.PriorityQueue; Public classBBTSP { Public Static float[][]a; Private Static classMinheapextendsPriorityqueue{ Publicminheap () {Super(); } Public voidput (Heapnode node) {Add (node); } PublicHeapnode removemin () {Heapnode poll=poll (); returnpoll; } } Private Static classHeapnodeImplementscomparable{floatLcost,//sub-tree cost lower boundCc//Current costsRcost;//x[s:n-1] Fixed-point minimum out-of-border costs and ints; //corresponds to the counter, expressed in the first layer of the current solution tree; int[] x; PublicHeapnode (floatLcfloatCccfloatRcintSsint[] xx) {lcost=LC; CC=CCC; Rcost=RC; S=SS; X=xx; } @Override Public intCompareTo (Object x) {floatXLC =((heapnode) x). Lcost; if(Lcost < XLC)return-1; if(Lcost = = XLC)return0; return1; } } Public Static floatBBTSP (intv[]) { intn = v.length-1; Minheap Heap=Newminheap (); float[] Minout =New float[n + 1]; floatMinsum = 0; for(inti = 1; I <= N; i++){ floatMin =Float.max_value; for(intj = 1; J <=n; J + +){ if(A[i][j] <min) {min=A[i][j]; } } if(min = =float.max_value)returnFloat.max_value; Minout[i]=min; Minsum+=min; } int[] x =New int[n]; int[] Minx =New int[n]; for(inti = 0; I < n; i++) X[i] = i+1; Heapnode Enode=NewHeapnode (0, 0, minsum, 0, x); floatBESTC =Float.max_value; while(Enode! =NULL&& ENODE.S < n-1) {x=enode.x; if(ENODE.S = = N-2){ if(A[x[n-2]][x[n-1]) <Float.max_value&& A[x[n-1]][1] <Float.max_value&& enode.cc + a[x[n-2]][x[n-1]] + a[x[n-1]][1] <BESTC) {BESTC= enode.cc + a[x[n-2]][x[n-1]] + a[x[n-1]][1]; enode.cc=BESTC; Enode.lcost=BESTC; //System.out.println (x[0]+ "," +x[1]+ "," +x[2]+ "," +x[3] ");enode.s++; Minx=x; Heap.put (Enode); } } Else{ for(inti = enode.s + 1; I < n; i++){ if(A[x[enode.s]][x[i]) <float.max_value) { floatCC = enode.cc +A[x[enode.s]][x[i]]; floatRcost = Enode.rcost-MINOUT[X[ENODE.S]]; floatb = cc +Rcost; //update current costs, minimum out-of-bounds costs, and sub-tree cost lower bound;//sub-tree cost lower bound, that is, the current smallest possible solution if(b <BESTC) { int[] xx =New int[n]; for(intj = 0; J < N; J + +) Xx[j] =X[j]; XX[ENODE.S+ 1] =X[i]; Xx[i]= X[ENODE.S + 1]; //access to the I node, the I node and the ENODE.S+1 node Exchange location copy to XX;Heapnode node =NewHeapnode (b, CC, Rcost, enode.s+1, XX); Heap.put (node); } }}} Enode=(Heapnode) heap.removemin (); } for(inti = 0; I < n; i++) {V[i+ 1] =Minx[i]; } returnBESTC; }}
First introduce this BBTSP class:
Composition of the BBTSP:
1. Class Minheap inherits Priority_queue, which is the priority queue we want to put in;
2. Class Heapnode This is our custom data structure, placed in the priority queue;
3.BBTSP is the solution class for this problem
4.A is the adjacency matrix, as we want to solve the problem of the diagram;
The idea of problem solving:
The minimum heap is used to represent the Slipknot point priority queue, the smallest element in the minimum heap is Heapnode, the Lcost in the sub-tree is the lower bound of the subtree, CC is the current cost, Rcost is the minimum out-of-line cost of the remaining points, and S records the first layer of the solution tree, the X array stores the optimal solution; Sub-tree cost lower bounds as the minimum heap arrangement basis, each fetch to Enode (the current optimal solution) for the son node, to obtain the best feasible subtree son solution, the node inserted in the smallest heap; when traversing to the last node, the best solution to the minimum 1 cities is obtained. Note that the optimal solution should be stored separately in the Minx array, with some questions written on the textbook;
Test code:
Packageessay;Importorg.junit.*; Public classMyTest {@Test Public voidTest () {bbtsp bbtsp=Newbbtsp (); Bbtsp.a=New float[5] [5]; bbtsp.a[1][1] = 0; bbtsp.a[[30]; bbtsp.a[1][3] = 8; bbtsp.a[1][4] = 7; bbtsp.a[2][1] = 30; bbtsp.a[2][2] = 0; bbtsp.a[2][3] = 4; bbtsp.a[2][4] = 5; bbtsp.a[3][1] = 8; bbtsp.a[3][2] = 4; bbtsp.a[3][3] = 0; bbtsp.a[3][4] = 10; bbtsp.a[4][1] = 7; bbtsp.a[4][2] = 5; bbtsp.a[4][3] = 10; bbtsp.a[4][4] = 0; int[] v =New int[5]; System.out.println ("Minimum fee is:" +bbtsp.bbtsp (v)); System.out.println ("Path is:"); for(inti = 1; I <= 4; i++) {System.out.printf ("%d-->", V[i]); } System.out.println (1); }}
Source: Http://www.cnblogs.com/handsomecui
Welcome visit: Handsomecui.top
Travel salesman question