forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-UsingShortestPathAlgorithms.js
45 lines (35 loc) · 1.09 KB
/
04-UsingShortestPathAlgorithms.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
//adjacent matrix
var i;
var graph = [[0, 2, 4, 0, 0, 0],
[0, 0, 2, 4, 2, 0],
[0, 0, 0, 0, 3, 0],
[0, 0, 0, 0, 0, 2],
[0, 0, 0, 3, 0, 2],
[0, 0, 0, 0, 0, 0]];
var shortestPath = new ShortestPath(graph);
console.log("********* Dijkstra's Algorithm - Shortest Path ***********");
var dist = shortestPath.dijkstra(0);
for (i = 0; i < dist.length; i++){
console.log(i + '\t\t' + dist[i]);
}
console.log("********* Floyd-Warshall Algorithm - All-Pairs Shortest Path ***********");
var INF = Number.MAX_SAFE_INTEGER;
graph = [[0, 2, 4, INF, INF, INF],
[INF, 0, 2, 4, 2, INF],
[INF, INF, 0, INF, 3, INF],
[INF, INF, INF, 0, INF, 2],
[INF, INF, INF, 3, 0, 2],
[INF, INF, INF, INF, INF, 0]];
shortestPath = new ShortestPath(graph);
dist = shortestPath.floydWarshall();
var s = '';
for (i=0; i<dist.length; ++i) {
s = '';
for (var j=0; j<dist.length; ++j) {
if (dist[i][j] === INF)
s += "INF ";
else
s += dist[i][j]+" ";
}
console.log(s);
}