-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Dijkstra’s Algorithm #1761
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
Closed
RohitVishwakarma8840
wants to merge
1
commit into
TheAlgorithms:master
from
RohitVishwakarma8840:patch-1
Closed
Dijkstra’s Algorithm #1761
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
Graphs/Dijkstra’s Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
class Graph { | ||
constructor() { | ||
this.adjacencyList = new Map(); | ||
} | ||
|
||
addVertex(vertex) { | ||
if (!this.adjacencyList.has(vertex)) { | ||
this.adjacencyList.set(vertex, []); | ||
} | ||
} | ||
|
||
addEdge(source, destination, weight) { | ||
if (!this.adjacencyList.has(source)) { | ||
this.addVertex(source); | ||
} | ||
if (!this.adjacencyList.has(destination)) { | ||
this.addVertex(destination); | ||
} | ||
this.adjacencyList.get(source).push({ node: destination, weight }); | ||
} | ||
|
||
dijkstra(start, end) { | ||
const distances = {}; | ||
const visited = new Set(); | ||
const priorityQueue = new Map(); | ||
const previous = {}; | ||
|
||
// Initialize distances and previous map | ||
for (let vertex of this.adjacencyList.keys()) { | ||
distances[vertex] = Infinity; | ||
previous[vertex] = null; | ||
} | ||
distances[start] = 0; | ||
priorityQueue.set(start, 0); | ||
|
||
while (priorityQueue.size > 0) { | ||
// Get the node with the smallest distance | ||
let [currentNode, currentDistance] = [...priorityQueue.entries()].reduce((a, b) => (a[1] < b[1] ? a : b)); | ||
priorityQueue.delete(currentNode); | ||
|
||
// If we reached the destination node, build and return the path | ||
if (currentNode === end) { | ||
const path = []; | ||
while (previous[currentNode]) { | ||
path.push(currentNode); | ||
currentNode = previous[currentNode]; | ||
} | ||
return { path: path.reverse(), distance: distances[end] }; | ||
} | ||
|
||
visited.add(currentNode); | ||
|
||
// Update distances and queue with neighbors | ||
for (let neighbor of this.adjacencyList.get(currentNode)) { | ||
if (!visited.has(neighbor.node)) { | ||
const newDist = currentDistance + neighbor.weight; | ||
if (newDist < distances[neighbor.node]) { | ||
distances[neighbor.node] = newDist; | ||
previous[neighbor.node] = currentNode; | ||
priorityQueue.set(neighbor.node, newDist); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return { path: [], distance: Infinity }; // Path not found | ||
} | ||
} | ||
|
||
// Example Usage: | ||
const graph = new Graph(); | ||
graph.addVertex("A"); | ||
graph.addVertex("B"); | ||
graph.addVertex("C"); | ||
graph.addVertex("D"); | ||
|
||
graph.addEdge("A", "B", 5); | ||
graph.addEdge("A", "C", 2); | ||
graph.addEdge("B", "D", 1); | ||
graph.addEdge("C", "B", 8); | ||
graph.addEdge("C", "D", 7); | ||
|
||
const result = graph.dijkstra("A", "D"); | ||
console.log("Shortest path:", result.path); | ||
console.log("Shortest distance:", result.distance); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.