-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBellmanFord.java
More file actions
103 lines (88 loc) · 4.11 KB
/
Copy pathBellmanFord.java
File metadata and controls
103 lines (88 loc) · 4.11 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* 음수 양수 Edge 에 대해서도 가능하다.
* Worst case: O(|V| |E|)
*
* @author Justin Wetherell <phishman3579@gmail.com>
*/
public class BellmanFord {
private static Map<Graph.Vertex<Integer>, Graph.CostVertexPair<Integer>> costs = null;
private static Map<Graph.Vertex<Integer>, Set<Graph.Edge<Integer>>> paths = null;
private static boolean containsNegativeWeightCycle = false;
private BellmanFord() { }
public static Map<Graph.Vertex<Integer>, Graph.CostPathPair<Integer>> getShortestPaths(Graph<Integer> g, Graph.Vertex<Integer> start) {
getShortestPath(g, start, null);
Map<Graph.Vertex<Integer>, Graph.CostPathPair<Integer>> map = new HashMap<Graph.Vertex<Integer>, Graph.CostPathPair<Integer>>();
for (Graph.CostVertexPair<Integer> pair : costs.values()) {
int cost = pair.getCost();
Graph.Vertex<Integer> vertex = pair.getVertex();
Set<Graph.Edge<Integer>> path = paths.get(vertex);
map.put(vertex, new Graph.CostPathPair<Integer>(cost, path));
}
return map;
}
public static Graph.CostPathPair<Integer> getShortestPath(Graph<Integer> g, Graph.Vertex<Integer> start, Graph.Vertex<Integer> end) {
if (g == null)
throw (new NullPointerException("Graph must be non-NULL."));
// Reset variables
costs = null;
paths = null;
containsNegativeWeightCycle = false;
paths = new TreeMap<Graph.Vertex<Integer>, Set<Graph.Edge<Integer>>>();
for (Graph.Vertex<Integer> v : g.getVerticies()) {
paths.put(v, new LinkedHashSet<Graph.Edge<Integer>>());
}
costs = new TreeMap<Graph.Vertex<Integer>, Graph.CostVertexPair<Integer>>();
for (Graph.Vertex<Integer> v : g.getVerticies()) {
if (v.equals(start))
costs.put(v, new Graph.CostVertexPair<Integer>(0, v));
else
costs.put(v, new Graph.CostVertexPair<Integer>(Integer.MAX_VALUE, v));
}
boolean negativeCycleCheck = false;
for (int i = 0; i < (g.getVerticies().size()); i++) {
// If it's the last vertices perform a negative weight cycle check.
// The graph should be
// finished by the size()-1 time through this loop.
if (i == (g.getVerticies().size() - 1))
negativeCycleCheck = true;
// Compute costs to all vertices
for (Graph.Edge<Integer> e : g.getEdges()) {
Graph.CostVertexPair<Integer> pair = costs.get(e.getToVertex());
Graph.CostVertexPair<Integer> lowestCostToThisVertex = costs.get(e.getFromVertex());
// If the cost of the from vertex is MAX_VALUE then treat as
// INIFINITY.
if (lowestCostToThisVertex.getCost() == Integer.MAX_VALUE)
continue;
int cost = lowestCostToThisVertex.getCost() + e.getCost();
if (cost < pair.getCost()) {
if (negativeCycleCheck) {
// Uhh ohh... negative weight cycle
System.out.println("Graph contains a negative weight cycle.");
containsNegativeWeightCycle = true;
return null;
}
// Found a shorter path to a reachable vertex
pair.setCost(cost);
Set<Graph.Edge<Integer>> set = paths.get(e.getToVertex());
set.clear();
set.addAll(paths.get(e.getFromVertex()));
set.add(e);
}
}
}
if (end != null) {
Graph.CostVertexPair<Integer> pair = costs.get(end);
Set<Graph.Edge<Integer>> set = paths.get(end);
return (new Graph.CostPathPair<Integer>(pair.getCost(), set));
}
return null;
}
public static boolean containsNegativeWeightCycle() {
return containsNegativeWeightCycle;
}
}