(graph, startVertex)
| 13 | * @return {ShortestPaths} |
| 14 | */ |
| 15 | export default function dijkstra(graph, startVertex) { |
| 16 | // Init helper variables that we will need for Dijkstra algorithm. |
| 17 | const distances = {}; |
| 18 | const visitedVertices = {}; |
| 19 | const previousVertices = {}; |
| 20 | const queue = new PriorityQueue(); |
| 21 | |
| 22 | // Init all distances with infinity assuming that currently we can't reach |
| 23 | // any of the vertices except the start one. |
| 24 | graph.getAllVertices().forEach((vertex) => { |
| 25 | distances[vertex.getKey()] = Infinity; |
| 26 | previousVertices[vertex.getKey()] = null; |
| 27 | }); |
| 28 | |
| 29 | // We are already at the startVertex so the distance to it is zero. |
| 30 | distances[startVertex.getKey()] = 0; |
| 31 | |
| 32 | // Init vertices queue. |
| 33 | queue.add(startVertex, distances[startVertex.getKey()]); |
| 34 | |
| 35 | // Iterate over the priority queue of vertices until it is empty. |
| 36 | while (!queue.isEmpty()) { |
| 37 | // Fetch next closest vertex. |
| 38 | const currentVertex = queue.poll(); |
| 39 | |
| 40 | // Iterate over every unvisited neighbor of the current vertex. |
| 41 | currentVertex.getNeighbors().forEach((neighbor) => { |
| 42 | // Don't visit already visited vertices. |
| 43 | if (!visitedVertices[neighbor.getKey()]) { |
| 44 | // Update distances to every neighbor from current vertex. |
| 45 | const edge = graph.findEdge(currentVertex, neighbor); |
| 46 | |
| 47 | const existingDistanceToNeighbor = distances[neighbor.getKey()]; |
| 48 | const distanceToNeighborFromCurrent = distances[currentVertex.getKey()] + edge.weight; |
| 49 | |
| 50 | // If we've found shorter path to the neighbor - update it. |
| 51 | if (distanceToNeighborFromCurrent < existingDistanceToNeighbor) { |
| 52 | distances[neighbor.getKey()] = distanceToNeighborFromCurrent; |
| 53 | |
| 54 | // Change priority of the neighbor in a queue since it might have became closer. |
| 55 | if (queue.hasValue(neighbor)) { |
| 56 | queue.changePriority(neighbor, distances[neighbor.getKey()]); |
| 57 | } |
| 58 | |
| 59 | // Remember previous closest vertex. |
| 60 | previousVertices[neighbor.getKey()] = currentVertex; |
| 61 | } |
| 62 | |
| 63 | // Add neighbor to the queue for further visiting. |
| 64 | if (!queue.hasValue(neighbor)) { |
| 65 | queue.add(neighbor, distances[neighbor.getKey()]); |
| 66 | } |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | // Add current vertex to visited ones to avoid visiting it again later. |
| 71 | visitedVertices[currentVertex.getKey()] = currentVertex; |
| 72 | } |
no test coverage detected