Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
124 lines (105 loc) · 3.93 KB
/
Copy pathMain.java
File metadata and controls
124 lines (105 loc) · 3.93 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
public class Main {
public static void main(String[] args) {
// ==== INIT ====
//initialize matrix
int[][] matrix = new int[48][48];
for (int i = 0; i < 48; i++)
for (int j = 0; j < 48; j++)
matrix[i][j] = 9999;
for (int i = 0; i < 48; i++)
matrix[i][i] = 0;
// ==== LOAD ====
// Load edges from assignment
int[][] edges = {
{0,4,755},{0,1,129},{0,2,535},
{1,4,713},{1,5,534},{1,6,541},{1,2,663},
{2,6,441},{2,3,160},
{3,6,619},
{4,7,476},{4,5,652},
{5,8,488},{5,9,435},{5,6,338},
{6,9,727},{6,10,438},
{7,11,697},{7,12,585},{7,8,355},
{8,12,626},{8,13,536},{8,14,486},{8,9,100},
{9,14,444},{9,15,455},{9,10,675},
{10,15,742},{10,16,624},
{11,17,430},{11,18,504},{11,12,388},
{12,18,337},{12,19,416},{12,13,293},
{13,19,204},{13,14,165},
{14,19,343},{14,20,187},{14,15,392},
{15,20,490},{15,21,404},{15,16,215},
{16,21,435},
{17,22,150},{17,18,416},
{18,22,253},{18,26,344},{18,19,340},
{19,26,453},{19,27,562},{19,23,192},{19,20,255},
{20,23,291},{20,24,279},{20,21,244},
{21,24,258},
{22,25,242},{22,26,392},
{23,27,415},{23,28,190},{23,24,249},
{24,29,614},
{25,30,205},{25,31,160},{25,26,282},
{26,31,255},{26,36,530},{26,32,597},{26,27,203},
{27,32,532},{27,33,197},{27,34,186},{27,28,145},
{28,34,175},{28,29,244},
{29,34,237},
{30,31,252},
{31,35,212},{31,36,434},
{32,36,156},{32,37,129},{32,33,302},
{33,37,397},{33,38,354},{33,34,160},
{34,38,425},
{35,36,200},
{37,39,62},{37,38,103},
{38,39,124},{38,40,127},{38,41,268},
{39,40,108},
{40,41,193},
{41,43,111},{41,44,165},{41,42,153},
{42,44,236},{42,45,106},
{43,46,72},{43,44,101},
{44,46,45},{44,45,68},
{45,47,139}
};
for (int[] edge : edges) {
matrix[edge[0]][edge[1]] = edge[2];
matrix[edge[1]][edge[0]] = edge[2];
}
// create vertex array
Vertex[] vertexArray = new Vertex[48];
for (int i = 0; i < 48; i++)
vertexArray[i] = new Vertex();
// currDist(first) = 0 → source is state 0
vertexArray[0].dist = 0;
// ==== ALGORITHM ====
// Dijkstras from slides
while (true) {
// v = vertex in toBeChecked with minimal currDist(v)
int v = -1;
int minDist = 9999;
for (int i = 0; i < 48; i++) {
if (!vertexArray[i].found && vertexArray[i].dist < minDist) {
minDist = vertexArray[i].dist;
v = i;
}
}
// stop when vertex can't be reached
if (v == -1) break;
// remove vertex
vertexArray[v].found = true;
for (int u = 0; u < 48; u++) {
if (matrix[v][u] != 9999 && !vertexArray[u].found) {
// "if currDist(u) > currDist(v) + weight(edge(vu))"
int newDist = vertexArray[v].dist + matrix[v][u];
if (newDist < vertexArray[u].dist) {
// "currDist(u) = currDist(v) + weight(edge(vu))"
vertexArray[u].dist = newDist;
// "predecessor(u) = v"
vertexArray[u].prev = v;
}
}
}
}
// print
for (int i = 0; i < 48; i++) {
System.out.println("The shortest dist between state[0] and state["
+ i + "] is: " + vertexArray[i].dist);
}
}
}