Created
June 20, 2021 07:31
-
-
Save bhushan/1de61f2f526c98ffbcf99711e9697c26 to your computer and use it in GitHub Desktop.
Using Dijkstra algorithm find the shortest path between two nodes.
This file contains 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
class Graph { | |
constructor() { | |
this.vertices = []; | |
this.adjacencyList = {}; | |
this.dijkstraTable = {}; | |
} | |
addVertex(vertex) { | |
this.vertices.push(vertex); | |
this.adjacencyList[vertex] = {}; | |
} | |
addEdge(vertex1, vertex2, weight) { | |
this.adjacencyList[vertex1][vertex2] = weight; | |
} | |
changeWeight(vertex1, vertex2, weight) { | |
this.adjacencyList[vertex1][vertex2] = weight; | |
} | |
generateDijkstraLookupFrom(source) { | |
let distances = {}, | |
previousNode = {}, | |
visited = new Set(); | |
this.vertices.forEach((vertice) => { | |
if (vertice === source) { | |
distances[source] = 0; | |
} else { | |
distances[vertice] = Infinity; | |
} | |
previousNode[vertice] = null; | |
}); | |
let currVertex = this.vertexWithMinDistance(distances, visited); | |
while (currVertex !== null) { | |
let distance = distances[currVertex], | |
neighbors = this.adjacencyList[currVertex]; | |
for (let neighbor in neighbors) { | |
let newDistance = distance + neighbors[neighbor]; | |
if (distances[neighbor] > newDistance) { | |
distances[neighbor] = newDistance; | |
previousNode[neighbor] = currVertex; | |
} | |
} | |
visited.add(currVertex); | |
currVertex = this.vertexWithMinDistance(distances, visited); | |
} | |
this.dijkstraTable = { | |
previousNode, | |
distances, | |
}; | |
} | |
vertexWithMinDistance(distances, visited) { | |
let minDistance = Infinity, | |
minVertex = null; | |
for (let vertex in distances) { | |
let distance = distances[vertex]; | |
if (distance < minDistance && !visited.has(vertex)) { | |
minDistance = distance; | |
minVertex = vertex; | |
} | |
} | |
return minVertex; | |
} | |
findShortestPath(start, end) { | |
let path = "" + end; | |
let currentNode = end; | |
while (currentNode != start) { | |
path = this.dijkstraTable.previousNode[currentNode] + " -> " + path; | |
currentNode = this.dijkstraTable.previousNode[currentNode]; | |
} | |
return path; | |
} | |
} | |
let g = new Graph(); | |
g.addVertex("HOME"); | |
g.addVertex("A"); | |
g.addVertex("B"); | |
g.addVertex("C"); | |
g.addVertex("D"); | |
g.addVertex("E"); | |
g.addVertex("OFFICE"); | |
g.addEdge("HOME", "A", 5); | |
g.addEdge("OFFICE", "D", 8); | |
g.addEdge("A", "C", 3); | |
g.addEdge("A", "B", 9); | |
g.addEdge("D", "C", 4); | |
g.addEdge("D", "E", 6); | |
g.addEdge("C", "B", 5); | |
g.addEdge("C", "E", 2); | |
g.addEdge("B", "OFFICE", 7); | |
g.addEdge("E", "OFFICE", 4); | |
g.generateDijkstraLookupFrom("HOME"); | |
const pathFromHomeToOffice = g.findShortestPath("HOME", "OFFICE"); | |
const pathFromAToB = g.findShortestPath("A", "B"); | |
console.log(pathFromHomeToOffice); | |
console.log(pathFromAToB); |
Author
bhushan
commented
Jun 20, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment