-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBellmanFord.java
152 lines (129 loc) · 4.29 KB
/
BellmanFord.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.*;
public class BellmanFord{
/**
* Utility class. Don't use.
*/
public class BellmanFordException extends Exception{
private static final long serialVersionUID = -4302041380938489291L;
public BellmanFordException() {super();}
public BellmanFordException(String message) {
super(message);
}
}
/**
* Custom exception class for BellmanFord algorithm
*
* Use this to specify a negative cycle has been found
*/
public class NegativeWeightException extends BellmanFordException{
private static final long serialVersionUID = -7144618211100573822L;
public NegativeWeightException() {super();}
public NegativeWeightException(String message) {
super(message);
}
}
/**
* Custom exception class for BellmanFord algorithm
*
* Use this to specify that a path does not exist
*/
public class PathDoesNotExistException extends BellmanFordException{
private static final long serialVersionUID = 547323414762935276L;
public PathDoesNotExistException() { super();}
public PathDoesNotExistException(String message) {
super(message);
}
}
private int[] distances = null;
private int[] predecessors = null;
private int source;
BellmanFord(WGraph g, int source) throws BellmanFordException{
/* Constructor, input a graph and a source
* Computes the Bellman Ford algorithm to populate the
* attributes
* distances - at position "n" the distance of node "n" to the source is kept
* predecessors - at position "n" the predecessor of node "n" on the path
* to the source is kept
* source - the source node
*
* If the node is not reachable from the source, the
* distance value must be Integer.MAX_VALUE
*
* When throwing an exception, choose an appropriate one from the ones given above
*/
this.distances = new int[g.getNbNodes()];
for (int i=0; i<distances.length; i++) {
distances[i] = Integer.MAX_VALUE;
}
distances[source] = 0;
this.predecessors = new int[g.getNbNodes()];
for (int i=1; i<g.getNbNodes()-1; i++) {
for (Edge e: g.getEdges()) {
int u = e.nodes[0];
int v = e.nodes[1];
if (distances[v] > (distances[u] + e.weight)) {
distances[v] = distances[u] + e.weight;
predecessors[v] = u;
}
}
for (Edge e: g.getEdges()) {
int u = e.nodes[0];
int v = e.nodes[1];
if (distances[v] > distances[u] + e.weight) {
throw new NegativeWeightException();
}
}
}
}
public int[] shortestPath(int destination) throws BellmanFordException{
ArrayList<Integer> pathList = new ArrayList<Integer>();
int current = destination;
pathList.add(current);
while(current != source) {
if(predecessors[current] == -1) {
throw new PathDoesNotExistException();
}
else {
pathList.add(predecessors[current]);
current = predecessors[current];
}
}
int[] path = new int[pathList.size()];
for(int i = 0; i<pathList.size(); i++) {
path[i] = pathList.get(pathList.size()-i-1);
}
return path;
}
public void printPath(int destination){
/*Print the path in the format s->n1->n2->destination
*if the path exists, else catch the Error and
*prints it
*/
try {
int[] path = this.shortestPath(destination);
for (int i = 0; i < path.length; i++){
int next = path[i];
if (next == destination){
System.out.println(destination);
}
else {
System.out.print(next + "-->");
}
}
}
catch (BellmanFordException e){
System.out.println(e);
}
}
public static void main(String[] args){
String file = args[0];
WGraph g = new WGraph(file);
try{
BellmanFord bf = new BellmanFord(g, g.getSource());
bf.printPath(g.getDestination());
}
catch (BellmanFordException e){
System.out.println(e);
}
}
}