Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 881e3ae

Browse files
committed
Add more comments for Dijkstra.
1 parent 1f39355 commit 881e3ae

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

‎src/algorithms/graph/dijkstra/dijkstra.js‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
11
import PriorityQueue from '../../../data-structures/priority-queue/PriorityQueue';
22

33
/**
4-
* @param {Graph} graph
5-
* @param {GraphVertex} startVertex
4+
* @typedef {Object} ShortestPaths
5+
* @property {Object} distances - shortest distances to all vertices
6+
* @property {Object} previousVertices - shortest paths to all vertices.
7+
*/
8+
9+
/**
10+
* Implementation of Dijkstra algorithm of finding the shortest paths to graph nodes.
11+
* @param {Graph} graph - graph we're going to traverse.
12+
* @param {GraphVertex} startVertex - traversal start vertex.
13+
* @return {ShortestPaths}
614
*/
715
export default function dijkstra(graph, startVertex) {
16+
// Init helper variables that we will need for Dijkstra algorithm.
817
const distances = {};
918
const visitedVertices = {};
1019
const previousVertices = {};
1120
const queue = new PriorityQueue();
1221

1322
// Init all distances with infinity assuming that currently we can't reach
14-
// any of the vertices except start one.
23+
// any of the vertices except the start one.
1524
graph.getAllVertices().forEach((vertex) => {
1625
distances[vertex.getKey()] = Infinity;
1726
previousVertices[vertex.getKey()] = null;
1827
});
28+
29+
// We are already at the startVertex so the distance to it is zero.
1930
distances[startVertex.getKey()] = 0;
2031

2132
// Init vertices queue.
2233
queue.add(startVertex, distances[startVertex.getKey()]);
2334

35+
// Iterate over the priority queue of vertices until it is empty.
2436
while (!queue.isEmpty()) {
37+
// Fetch next closest vertex.
2538
const currentVertex = queue.poll();
2639

27-
graph.getNeighbors(currentVertex).forEach((neighbor) => {
40+
// Iterate over every unvisited neighbor of the current vertex.
41+
currentVertex.getNeighbors().forEach((neighbor) => {
2842
// Don't visit already visited vertices.
2943
if (!visitedVertices[neighbor.getKey()]) {
3044
// Update distances to every neighbor from current vertex.
@@ -33,15 +47,16 @@ export default function dijkstra(graph, startVertex) {
3347
const existingDistanceToNeighbor = distances[neighbor.getKey()];
3448
const distanceToNeighborFromCurrent = distances[currentVertex.getKey()] + edge.weight;
3549

50+
// If we've found shorter path to the neighbor - update it.
3651
if (distanceToNeighborFromCurrent < existingDistanceToNeighbor) {
3752
distances[neighbor.getKey()] = distanceToNeighborFromCurrent;
3853

39-
// Change priority.
54+
// Change priority of the neighbor in a queue since it might have became closer.
4055
if (queue.hasValue(neighbor)) {
4156
queue.changePriority(neighbor, distances[neighbor.getKey()]);
4257
}
4358

44-
// Remember previous vertex.
59+
// Remember previous closest vertex.
4560
previousVertices[neighbor.getKey()] = currentVertex;
4661
}
4762

@@ -52,10 +67,12 @@ export default function dijkstra(graph, startVertex) {
5267
}
5368
});
5469

55-
// Add current vertex to visited ones.
70+
// Add current vertex to visited ones to avoid visiting it again later.
5671
visitedVertices[currentVertex.getKey()] = currentVertex;
5772
}
5873

74+
// Return the set of shortest distances to all vertices and the set of
75+
// shortest paths to all vertices in a graph.
5976
return {
6077
distances,
6178
previousVertices,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /