forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FloydWarshall.js
47 lines (42 loc) · 1.3 KB
/
FloydWarshall.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
/*
Source:
https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm
Complexity:
O(|V|^3) where V is the set of vertices
*/
const FloydWarshall = (dist) => {
// Input:- dist: 2D Array where dist[i][j] = edge weight b/w i and j
// Output:- dist: 2D Array where dist[i][j] = shortest dist b/w i and j
const n = dist.length
for (let k = 0; k < n; k++) {
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
// dist from i to j via k is lesser than the current distance
dist[i][j] = dist[i][k] + dist[k][j]
}
}
}
}
return dist
}
export { FloydWarshall }
// For the following graph (edge weights are shown in brackets)
// 4 1 dist[1][2] = dist[2][1] = 1
// \ (2)/ \ dist[1][3] = dist[3][1] = 2
// \ / \(1) dist[1][4] = dist[4][1] = Infinity
// (1)\ / \ dist[3][4] = dist[4][3] = 1
// 3 2 dist[2][4] = dist[4][2] = Infinity
// dist[2][3] = dist[3][2] = Infinity
// Output should be:
// [ [0, 1, 2, 3],
// [1, 0, 3, 4],
// [2, 3, 0, 1],
// [3, 4, 1, 0] ]
// FloydWarshall(
// [[0, 1, 2, Infinity],
// [1, 0, Infinity, Infinity],
// [2, Infinity, 0, 1],
// [Infinity, Infinity, 1, 0]
// ]
// )