Skip to content

Commit 1b928eb

Browse files
committed
improve dijkstra
1 parent 87a1c4d commit 1b928eb

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

algorithm/graph_search/dijkstra/shortest_path/code.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ function Dijkstra(start, end) {
33
var D = []; // D[i] indicates whether the i-th node is discovered or not
44
for (var i = 0; i < G.length; i++) D.push(false);
55
S[start] = 0; // Starting node is at distance 0 from itself
6-
tracerS._notify(start, S[start]);
6+
tracerS._notify(start, S[start])._wait()._denotify(start);
7+
tracerS._select(start);
78
var k = G.length;
89
while (k--) {
910
// Finding a node with the shortest distance from S[minIndex]
@@ -16,15 +17,17 @@ function Dijkstra(start, end) {
1617
}
1718
if (minDistance == MAX_VALUE) break; // If there is no edge from current node, jump out of loop
1819
D[minIndex] = true;
19-
tracerS._notify(minIndex);
20+
tracerS._select(minIndex);
2021
tracer._visit(minIndex)._wait();
2122
// For every unvisited neighbour of current node, we check
2223
// whether the path to it is shorter if going over the current node
2324
for (i = 0; i < G.length; i++) {
2425
if (G[minIndex][i] && S[i] > S[minIndex] + G[minIndex][i]) {
2526
S[i] = S[minIndex] + G[minIndex][i];
26-
tracerS._notify(i, S[i])._wait()._denotify(i);
27-
tracer._visit(i, minIndex, S[i])._wait()._leave(i, minIndex);
27+
tracerS._notify(i, S[i]);
28+
tracer._visit(i, minIndex, S[i])._wait();
29+
tracerS._denotify(i);
30+
tracer._leave(i, minIndex)._wait();
2831
}
2932
}
3033
tracer._leave(minIndex)._wait();

algorithm/graph_search/dijkstra/shortest_path/data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var tracer = new WeightedDirectedGraphTracer();
1+
var tracer = new WeightedUndirectedGraphTracer();
22
var tracerS = new Array1DTracer();
33
var logger = new LogTracer();
44
tracer.attach(logger);
5-
var G = WeightedDirectedGraph.random(10, .4, 1, 9);
5+
var G = WeightedUndirectedGraph.random(5, 1, 1, 9);
66
tracer._setData(G);
77
var MAX_VALUE = Infinity;
88
var S = []; // S[end] returns the distance from start node to end node

0 commit comments

Comments
 (0)