Skip to content

Commit a73af03

Browse files
committed
Dijkstra shortest path using basic queue
1 parent fb06d58 commit a73af03

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

19-Dijkstras-Shortest-Path-Algorithm/dijkstra-basic-priority-queue.js renamed to 19-Dijkstras-Shortest-Path-Algorithm/01-dijkstra-basic-priority-queue.js

File renamed without changes.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
class Node {
2+
constructor (vertex, weight) {
3+
this.vertex = vertex;
4+
this.weight = weight;
5+
}
6+
}
7+
8+
class PriorityQueue {
9+
constructor () {
10+
this.values = [];
11+
}
12+
13+
enqueue (val) {
14+
this.values.push(val);
15+
this.sort();
16+
}
17+
18+
dequeue () {
19+
return this.values.shift();
20+
}
21+
22+
sort () {
23+
this.values.sort((a, b) => a.priority - b.priority);
24+
}
25+
}
26+
27+
class WeightedGraph {
28+
constructor () {
29+
this.adjacencyList = {};
30+
}
31+
32+
addVertex (vertex) {
33+
if (!this.adjacencyList[vertex]) {
34+
this.adjacencyList[vertex] = [];
35+
}
36+
}
37+
38+
addEdge (vertex1, vertex2, weight) {
39+
this.adjacencyList[vertex1].push(new Node(vertex2, weight));
40+
this.adjacencyList[vertex2].push(new Node(vertex1, weight));
41+
}
42+
43+
Dijkstra (start, finish) {
44+
const nodes = new PriorityQueue();
45+
const distances = {};
46+
const previous = {};
47+
const path = []; // to return at end
48+
let smallest;
49+
// build up initial state
50+
for (const vertex in this.adjacencyList) {
51+
if (vertex === start) {
52+
distances[vertex] = 0;
53+
nodes.enqueue(vertex, 0);
54+
} else {
55+
distances[vertex] = Infinity;
56+
nodes.enqueue(vertex, Infinity);
57+
}
58+
previous[vertex] = null;
59+
}
60+
// as long as there is something to visit
61+
while (nodes.values.length) {
62+
smallest = nodes.dequeue().val;
63+
if (smallest === finish) {
64+
// WE ARE DONE
65+
// BUILD UP PATH TO RETURN AT END
66+
while (previous[smallest]) {
67+
path.push(smallest);
68+
smallest = previous[smallest];
69+
}
70+
break;
71+
}
72+
if (smallest || distances[smallest] !== Infinity) {
73+
for (const neighbor in this.adjacencyList[smallest]) {
74+
// find neighboring node
75+
const nextNode = this.adjacencyList[smallest][neighbor];
76+
// calculate new distance to neighboring node
77+
const candidate = distances[smallest] + nextNode.weight;
78+
const nextNeighbor = nextNode.node;
79+
if (candidate < distances[nextNeighbor]) {
80+
// updating new smallest distance to neighbor
81+
distances[nextNeighbor] = candidate;
82+
// updating previous - How we got to neighbor
83+
previous[nextNeighbor] = smallest;
84+
// enqueue in priority queue with new priority
85+
nodes.enqueue(nextNeighbor, candidate);
86+
}
87+
}
88+
}
89+
}
90+
return path.concat(smallest).reverse();
91+
}
92+
}
93+
94+
const graph = new WeightedGraph();
95+
96+
graph.addVertex('A');
97+
graph.addVertex('B');
98+
graph.addVertex('C');
99+
graph.addVertex('D');
100+
graph.addVertex('E');
101+
graph.addVertex('F');
102+
103+
graph.addEdge('A', 'B', 4);
104+
graph.addEdge('A', 'C', 2);
105+
graph.addEdge('B', 'E', 3);
106+
graph.addEdge('C', 'D', 2);
107+
graph.addEdge('C', 'F', 4);
108+
graph.addEdge('D', 'E', 3);
109+
graph.addEdge('D', 'F', 1);
110+
graph.addEdge('E', 'F', 1);
111+
112+
console.log(JSON.stringify(graph, null, 4));

0 commit comments

Comments
 (0)