-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstraTable.java
68 lines (56 loc) · 1.72 KB
/
DijkstraTable.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
public class DijkstraTable {
private final int INFINITY = Integer.MAX_VALUE;
int cost[];
int path[];
public DijkstraTable(int size){
cost = new int[size];
path = new int[size];
cost[0] = 0;
for(int i = 1; i < size; i++){
cost[i] = INFINITY;
}
for(int i = 0; i < size; i++){
path[i] = -1;
}
}
//TODO: fuck these methods. just keep two arrays in the Dijkstra class
public void updateCost(int index, int newValue){
cost[index] = newValue;
}
public void updatePath(int index, int newValue){
path[index] = newValue;
}
public void update(int index, int newPath, int newCost){
// System.out.println("Comparing newCost " + newCost + " to " + cost[index]);
if(newCost < cost[index]){
// System.err.println("Changed " + index + " from " +
// "(" + path[index] + "," + cost[index] + ") to " +
// "(" + newPath + "," + newCost + ")");
cost[index] = newCost;
path[index] = newPath;
}else{
System.out.println("Awkard...didn't do anything");
}
}
public void printDijkstraTable(){
System.out.println(" Cost\t" + (cost[1] == INFINITY ? "\t" : "") + " Path");
for(int i = 0; i < cost.length; i++){
String builder = "";
if(Integer.valueOf(i).toString().length() == 1){
builder += " ";
}
builder += i + ": " + cost[i];
if(Integer.valueOf(cost[i]).toString().length() <= 3){
// builder += (cost[i] == INFINITY ? "\t\t" : "\t");
builder += "\t";
}
System.out.println( builder + (cost[0] == 0 && cost[1] == INFINITY ? "\t" : "") + " " + path[i]);
}
}
public int getShortestPath(int vertex){
return path[vertex];
}
public int getPathCost(int vertex){
return cost[vertex];
}
}