|
1 | 1 | function Dijkstra(start, end) { |
2 | | - var MAX_VALUE = Infinity; |
3 | 2 | var minIndex, minDistance; |
4 | | - var S = []; // S[i] returns the distance from node v to node start |
5 | 3 | 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] = MAX_VALUE; |
9 | | - } |
| 4 | + for (var i = 0; i < G.length; i++) D.push(false); |
10 | 5 | S[start] = 0; // Starting node is at distance 0 from itself |
11 | 6 | var k = G.length; |
12 | 7 | while (k--) { |
13 | 8 | // Finding a node with the shortest distance from S[minIndex] |
14 | 9 | minDistance = MAX_VALUE; |
15 | 10 | for (i = 0; i < G.length; i++) { |
| 11 | + tracerS._select(i)._wait(); |
16 | 12 | if (S[i] < minDistance && !D[i]) { |
17 | 13 | minDistance = S[i]; |
18 | 14 | minIndex = i; |
19 | 15 | } |
| 16 | + tracerS._deselect(i); |
20 | 17 | } |
21 | 18 | if (minDistance == MAX_VALUE) break; // If there is no edge from current node, jump out of loop |
22 | 19 | D[minIndex] = true; |
| 20 | + tracerS._notify(minIndex, S[minIndex])._denotify(minIndex); |
23 | 21 | tracer._visit(minIndex)._wait(); |
24 | 22 | // For every unvisited neighbour of current node, we check |
25 | 23 | // whether the path to it is shorter if going over the current node |
26 | 24 | for (i = 0; i < G.length; i++) { |
27 | 25 | if (G[minIndex][i] && S[i] > S[minIndex] + G[minIndex][i]) { |
28 | 26 | S[i] = S[minIndex] + G[minIndex][i]; |
29 | | - tracer._visit(i, minIndex, S[i])._wait(); |
30 | | - tracer._leave(i, minIndex, S[i])._wait(); |
| 27 | + tracerS._notify(i, S[i])._wait()._denotify(i); |
| 28 | + tracer._visit(i, minIndex, S[i])._wait()._leave(i, minIndex); |
31 | 29 | } |
32 | 30 | } |
33 | 31 | tracer._leave(minIndex)._wait(); |
|
0 commit comments