-
Notifications
You must be signed in to change notification settings - Fork 0
/
BranchBound.java
59 lines (54 loc) · 1.59 KB
/
BranchBound.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
public class BranchBound {
private Graph graph;
private Integer nVertex;
private Solution sol = new Solution(Integer.MAX_VALUE);
public BranchBound(Graph graph) {
super();
this.graph = graph;
nVertex = graph.getnV();
}
public BranchBound(int nVertex) {
super();
graph = new Graph(nVertex);
this.nVertex = nVertex;
}
public Solution branchAndBound() {
Solution tempSol = new Solution();
dfs(0, 0, tempSol);
return sol;
}
private Solution dfs(Integer vertex, Integer parentVertex, Solution tempSol) {
graph.setVertexVisited(vertex, true);
tempSol.pathAddVertex(vertex);
if (vertex != parentVertex)
tempSol.setCost(tempSol.getCost() + graph.getEdgeLength(parentVertex, vertex));
if(tempSol.pathSize() == nVertex) {
if (graph.getEdgeLength(0, vertex) < 0) {
//tempSol.pathRemoveVertex();
graph.setVertexVisited(vertex, false);
return tempSol;
}
if ((tempSol.getCost() + graph.getEdgeLength(0, vertex)) <= sol.getCost()) {
sol = new Solution(tempSol);
sol.setCost(tempSol.getCost() + graph.getEdgeLength(0, vertex));
graph.setVertexVisited(vertex, false);
return tempSol;
}
}
if(tempSol.getCost() > sol.getCost())
return tempSol;
for (int i = 0; i < nVertex; i++) {
if (!graph.isVertexVisited(i)) {
if (graph.getEdgeLength(vertex, i) < 0)
continue;
tempSol = dfs(i, vertex, tempSol);
tempSol.pathRemoveVertex();
tempSol.setCost(tempSol.getCost() - graph.getEdgeLength(i, vertex));
graph.setVertexVisited(i, false);
}
}
graph.setVertexVisited(vertex, false);
return tempSol;
}
}