Skip to content

Commit 433e4c0

Browse files
committed
chapter 9 - shortest path algorithms
1 parent f2f4556 commit 433e4c0

3 files changed

+139
-0
lines changed

chapter09/03-ShortestPath.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
function ShortestPath(graph) {
2+
3+
this.graph = graph;
4+
5+
var INF = Number.MAX_SAFE_INTEGER;
6+
7+
var minDistance = function(dist, visited){
8+
9+
var min = INF,
10+
minIndex = -1;
11+
12+
for (var v = 0; v < dist.length; v++){
13+
if (visited[v] == false && dist[v] <= min){
14+
min = dist[v];
15+
minIndex = v;
16+
}
17+
}
18+
19+
return minIndex;
20+
};
21+
22+
this.dijkstra = function(src){
23+
24+
var dist = [],
25+
visited = [],
26+
length = this.graph.length;
27+
28+
// Initialize all distances as INFINITE (JavaScript max number) and visited[] as false
29+
for (var i = 0; i < length; i++) {
30+
dist[i] = INF;
31+
visited[i] = false;
32+
}
33+
34+
// Distance of source vertex from itself is always 0
35+
dist[src] = 0;
36+
37+
// Find shortest path for all vertices
38+
for (var i = 0; i < length-1; i++){
39+
40+
// Pick the minimum distance vertex from the set of vertices
41+
// not yet processed. u is always equal to src in first
42+
// iteration.
43+
var u = minDistance(dist, visited);
44+
45+
// Mark the picked vertex as processed
46+
visited[u] = true;
47+
48+
// Update dist value of the adjacent vertices of the
49+
// picked vertex.
50+
for (var v = 0; v < length; v++){
51+
if (!visited[v] && this.graph[u][v]!=0 && dist[u] != INF && dist[u]+this.graph[u][v] < dist[v]){
52+
dist[v] = dist[u] + this.graph[u][v];
53+
}
54+
}
55+
}
56+
57+
return dist;
58+
};
59+
60+
this.floydWarshall = function(){
61+
62+
var dist = [],
63+
length = this.graph.length,
64+
i, j, k;
65+
66+
for (i = 0; i < length; i++){
67+
dist[i] = [];
68+
for (j = 0; j < length; j++){
69+
dist[i][j] = this.graph[i][j];
70+
}
71+
}
72+
73+
for (k = 0; k < length; k++){
74+
for (i = 0; i < length; i++){
75+
for (j = 0; j < length; j++){
76+
if (dist[i][k] + dist[k][j] < dist[i][j]){
77+
dist[i][j] = dist[i][k] + dist[k][j];
78+
}
79+
}
80+
}
81+
}
82+
83+
return dist;
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="03-ShortestPath.js"></script>
9+
<script src="04-UsingShortestPathAlgorithms.js"></script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//adjacent matrix
2+
var graph = [[0, 2, 4, 0, 0, 0],
3+
[0, 0, 1, 4, 2, 0],
4+
[0, 0, 0, 0, 3, 0],
5+
[0, 0, 0, 0, 0, 2],
6+
[0, 0, 0, 3, 0, 2],
7+
[0, 0, 0, 0, 0, 0]];
8+
9+
var shortestPath = new ShortestPath(graph);
10+
11+
console.log("********* Dijkstra's Algorithm - Shortest Path ***********");
12+
13+
var dist = shortestPath.dijkstra(0);
14+
15+
for (var i = 0; i < dist.length; i++){
16+
console.log(i + '\t\t' + dist[i]);
17+
}
18+
19+
console.log("********* Floyd-Warshall Algorithm - All-Pairs Shortest Path ***********");
20+
21+
var INF = Number.MAX_SAFE_INTEGER;
22+
graph = [[0, 2, 4, INF, INF, INF],
23+
[INF, 0, 1, 4, 2, INF],
24+
[INF, INF, 0, INF, 3, INF],
25+
[INF, INF, INF, 0, INF, 2],
26+
[INF, INF, INF, 3, 0, 2],
27+
[INF, INF, INF, INF, INF, 0]];
28+
29+
shortestPath = new ShortestPath(graph);
30+
31+
dist = shortestPath.floydWarshall();
32+
33+
var s = '';
34+
for (var i=0; i<dist.length; ++i) {
35+
s = '';
36+
for (var j=0; j<dist.length; ++j) {
37+
if (dist[i][j] === INF)
38+
s += "INF ";
39+
else
40+
s += dist[i][j]+" ";
41+
}
42+
console.log(s);
43+
}

0 commit comments

Comments
 (0)