Skip to content

dijkstra algo fixed ,duplicate files removed #2064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/algorithms/graph/dijkstra/README.ko-KR.md

This file was deleted.

117 changes: 0 additions & 117 deletions src/algorithms/graph/dijkstra/__test__/dijkstra.test.js

This file was deleted.

137 changes: 73 additions & 64 deletions src/algorithms/graph/dijkstra/dijkstra.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,89 @@
import PriorityQueue from '../../../data-structures/priority-queue/PriorityQueue';

/**
* @typedef {Object} ShortestPaths
* @property {Object} distances - shortest distances to all vertices
* @property {Object} previousVertices - shortest paths to all vertices.
*/

/**
* Implementation of Dijkstra algorithm of finding the shortest paths to graph nodes.
* @param {Graph} graph - graph we're going to traverse.
* @param {GraphVertex} startVertex - traversal start vertex.
* @return {ShortestPaths}
*/
export default function dijkstra(graph, startVertex) {
// Init helper variables that we will need for Dijkstra algorithm.
const distances = {};
const visitedVertices = {};
const previousVertices = {};
const queue = new PriorityQueue();
class PriorityQueue {
constructor() {
this.values = [];
}

// Init all distances with infinity assuming that currently we can't reach
// any of the vertices except the start one.
graph.getAllVertices().forEach((vertex) => {
distances[vertex.getKey()] = Infinity;
previousVertices[vertex.getKey()] = null;
});
add(value, priority) {
this.values.push({ value, priority });
this.sort();
}

// We are already at the startVertex so the distance to it is zero.
distances[startVertex.getKey()] = 0;
poll() {
return this.values.shift().value;
}

// Init vertices queue.
queue.add(startVertex, distances[startVertex.getKey()]);
isEmpty() {
return this.values.length === 0;
}

// Iterate over the priority queue of vertices until it is empty.
while (!queue.isEmpty()) {
// Fetch next closest vertex.
const currentVertex = queue.poll();
hasValue(value) {
return this.values.some((item) => item.value === value);
}

// Iterate over every unvisited neighbor of the current vertex.
currentVertex.getNeighbors().forEach((neighbor) => {
// Don't visit already visited vertices.
if (!visitedVertices[neighbor.getKey()]) {
// Update distances to every neighbor from current vertex.
const edge = graph.findEdge(currentVertex, neighbor);
changePriority(value, newPriority) {
for (let item of this.values) {
if (item.value === value) {
item.priority = newPriority;
break;
}
}
this.sort();
}

const existingDistanceToNeighbor = distances[neighbor.getKey()];
const distanceToNeighborFromCurrent = distances[currentVertex.getKey()] + edge.weight;
sort() {
this.values.sort((a, b) => a.priority - b.priority);
}
}

// If we've found shorter path to the neighbor - update it.
if (distanceToNeighborFromCurrent < existingDistanceToNeighbor) {
distances[neighbor.getKey()] = distanceToNeighborFromCurrent;
function dijkstra(graph, start) {
const distances = {};
const previous = {};
const queue = new PriorityQueue();
const visited = new Set();

// Change priority of the neighbor in a queue since it might have became closer.
if (queue.hasValue(neighbor)) {
queue.changePriority(neighbor, distances[neighbor.getKey()]);
}
// Initialize distances
for (let vertex in graph) {
if (vertex === start) {
distances[vertex] = 0;
queue.add(vertex, 0);
} else {
distances[vertex] = Infinity;
}
previous[vertex] = null;
}

// Remember previous closest vertex.
previousVertices[neighbor.getKey()] = currentVertex;
}
while (!queue.isEmpty()) {
const currentVertex = queue.poll();

// ✅ Skip already visited nodes (handles duplicates safely)
if (visited.has(currentVertex)) continue;
visited.add(currentVertex);

for (let neighbor in graph[currentVertex]) {
const distance = distances[currentVertex] + graph[currentVertex][neighbor];

if (distance < distances[neighbor]) {
distances[neighbor] = distance;
previous[neighbor] = currentVertex;

// Add neighbor to the queue for further visiting.
if (!queue.hasValue(neighbor)) {
queue.add(neighbor, distances[neighbor.getKey()]);
queue.add(neighbor, distance);
} else {
queue.changePriority(neighbor, distance);
}
}
});

// Add current vertex to visited ones to avoid visiting it again later.
visitedVertices[currentVertex.getKey()] = currentVertex;
}
}

// Return the set of shortest distances to all vertices and the set of
// shortest paths to all vertices in a graph.
return {
distances,
previousVertices,
};
return { distances, previous };
}

const graph = {
A: { B: 1, C: 4 },
B: { C: 2, D: 5 },
C: { D: 1 },
D: {}
};

const result = dijkstra(graph, "A");
console.log(result.distances); // { A: 0, B: 1, C: 3, D: 4 }