Skip to content

Commit 626374c

Browse files
committed
Fix Dijkstra
1 parent 4240992 commit 626374c

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

src/graphs/shortest-path/dijkstra.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,19 @@ var dijstra = function () {
9393
var tempDistance = 0;
9494
init(src, graph);
9595
while (current.node != dest && current.distance != Infinity) {
96-
var changed = false;
9796
for (var i = 0; i < graph.length; i += 1) {
9897
if (current.node !== i && //if it's not the current node
9998
!visited[i] && //and if we haven't visited this node
10099
Number.isFinite(graph[i][current.node])) { //and this node is sibling of the current...
101100

102-
tempDistance = current.distance + graph[i][current.node];
101+
tempDistance = current.distance + graph[i][current.node];
103102
if (tempDistance < distance[i].distance) {
104-
changed = true;
105103
distance[i].distance = tempDistance;
104+
current.distance = tempDistance;
105+
unvisited.update(current);
106106
}
107107
}
108108
}
109-
if (changed) {
110-
// TODO the heap should update the order of the elements!
111-
}
112109
visited[current.node] = true;
113110
current = unvisited.extract();
114111
}

0 commit comments

Comments
 (0)