-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathFloyd-Warshall-Algorithm.js
58 lines (53 loc) · 1.19 KB
/
Floyd-Warshall-Algorithm.js
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
var INF = 99999;
class AllPairShortestPath {
constructor() {
this.V = 4;
}
floydWarshall(graph) {
var dist = Array.from(Array(this.V), () => new Array(this.V).fill(0));
var i, j, k;
for (i = 0; i < this.V; i++) {
for (j = 0; j < this.V; j++) {
dist[i][j] = graph[i][j];
}
}
for (k = 0; k < this.V; k++) {
// Pick all vertices as source
// one by one
for (i = 0; i < this.V; i++) {
// Pick all vertices as destination
// for the above picked source
for (j = 0; j < this.V; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
this.printSolution(dist);
}
printSolution(dist) {
document.write(
"Following matrix shows the shortest " +
"distances between every pair of vertices<br>"
);
for (var i = 0; i < this.V; ++i) {
for (var j = 0; j < this.V; ++j) {
if (dist[i][j] == INF) {
document.write(" INF ");
} else {
document.write(" " + dist[i][j] + " ");
}
}
document.write("<br>");
}
}
}
var graph = [
[0, 5, INF, 10],
[INF, 0, 3, INF],
[INF, INF, 0, 1],
[INF, INF, INF, 0],
];
var a = new AllPairShortestPath();
a.floydWarshall(graph);