Skip to content

Commit c5c5c93

Browse files
committed
Merge pull request algorithm-visualizer#11 from TornjV/feature/dijkstra
Dijkstra's algorithm
2 parents f745921 + d841e0c commit c5c5c93

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

algorithm/category.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"name": "Graph Search",
44
"list": {
55
"dfs": "DFS",
6-
"bfs": "BFS"
6+
"bfs": "BFS",
7+
"dijkstra": "Dijkstra"
78
}
89
},
910
"search": {
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Dijkstra": "Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.",
3+
"Applications": [
4+
"Ar."
5+
],
6+
"Complexity": {
7+
"time": "worst O(|V|<sup>2</sup>)",
8+
"space": "worst O(|V|)"
9+
},
10+
"References": [
11+
"<a href='https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm'>Wikipedia</a>"
12+
],
13+
"files": {
14+
"shortest_path": "Finding the shortest path between two nodes"
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function Dijkstra(start, end){
2+
tracer._sleep(333)
3+
var minIndex, minDistance;
4+
var S = []; // S[i] returns the distance from node v to node start
5+
var D = []; // D[i] indicates whether the i-th node is discovered or not
6+
for (var i = 0; i < G.length; i++){
7+
D.push(false);
8+
S[i] = 19990719 // Some big distance (infinity)
9+
}
10+
S[start] = 0; // Starting node is at distance 0 from itself
11+
for(var k = 0; k < G.length; k++){
12+
// Finding a node with the shortest distance from S[minIndex]
13+
minDistance = 19990719 // Some big distance (infinity)
14+
for(i = 0; i < G.length; i++){
15+
if(S[i] < minDistance && !D[i]){
16+
minDistance = S[i];
17+
minIndex = i;
18+
}
19+
}
20+
tracer._visit(minIndex,undefined);
21+
tracer._sleep(500);
22+
D[minIndex] = true;
23+
if(minDistance == 19990719){ // If the distance is big (infinity), there is no more paths
24+
tracer._print('there is no path from ' + s + ' to ' + e);
25+
return false;
26+
}
27+
// For every unvisited neighbour of current node, we check
28+
// whether the path to it is shorter if going over the current node
29+
for(i = 0; i < G.length; i++){
30+
if (G[minIndex][i] && !D[i] && ( S[i] > S[minIndex] + G[minIndex][i])){
31+
S[i] = S[minIndex] + G[minIndex][i];
32+
tracer._visit(i,minIndex,S[i]);
33+
tracer._sleep(500);
34+
tracer._leave(i,minIndex,S[i]);
35+
}
36+
}
37+
tracer._leave(minIndex,undefined);
38+
}
39+
tracer._print('the shortest path from ' + s + ' to ' + e + ' is ' + S[e]);
40+
}
41+
42+
var s = Math.random() * G.length | 0; // s = start node
43+
var e; // e = end node
44+
do {
45+
e = Math.random() * G.length | 0;
46+
} while (s == e);
47+
tracer._pace(100);
48+
tracer._print('finding the shortest path from ' + s + ' to ' + e);
49+
Dijkstra(s, e);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var tracer = new WeightedDirectedGraphTracer();
2+
/*var G = [ // G[i][j] indicates the weight of the path from the i-th node to the j-th node
3+
[0, 3, 0, 1, 0],
4+
[5, 0, 1, 2, 4],
5+
[1, 0, 0, 2, 0],
6+
[0, 2, 0, 0, 1],
7+
[0, 1, 3, 0, 0]
8+
];*/
9+
var G = WeightedDirectedGraph.random(10, .4, 1, 9);
10+
tracer._setData(G);

0 commit comments

Comments
 (0)