-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDijkstra's Shortest Path Algorithm.js
146 lines (123 loc) · 3.52 KB
/
Dijkstra's Shortest Path Algorithm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class PriorityQueue {
constructor() {
this.values = [];
}
enqueue(value, priority) {
this.values.push({ value, priority });
this.sort();
}
dequeue() {
return this.values.shift();
}
sort() {
this.values.sort((a, b) => a.priority - b.priority);
}
}
function Graph() {
this.adjacencyList = {};
}
Graph.prototype.numEdges = function () {
let total = 0;
Object.values(this.adjacencyList).forEach(list => {
total += list.length;
});
// note that we've double-counted up til now since we've looked at
// the adjacencyList for every node.
return total / 2;
};
Graph.prototype.addVertex = function (vertex) {
this.adjacencyList[vertex] = [];
};
Graph.prototype.addEdge = function (vertex1, vertex2) {
this.adjacencyList[vertex1].push(vertex2);
this.adjacencyList[vertex2].push(vertex1);
};
Graph.prototype.removeVertex = function (vertex) {
while (this.adjacencyList[vertex].length) {
const adjacentVertex = this.adjacencyList[vertex].pop();
this.removeEdge(adjacentVertex, vertex);
}
delete this.adjacencyList[vertex];
};
Graph.prototype.removeEdge = function (vertex1, vertex2) {
this.adjacencyList[vertex1] = this.adjacencyList[vertex1].filter(v => v !== vertex2);
this.adjacencyList[vertex2] = this.adjacencyList[vertex2].filter(v => v !== vertex1);
};
function WeightedGraph() {
Graph.call(this);
}
WeightedGraph.prototype = Object.create(Graph.prototype);
WeightedGraph.prototype.constructor = WeightedGraph;
WeightedGraph.prototype.addEdge = function (vertex1, vertex2, weight) {
this.adjacencyList[vertex1].push({ weight: weight, node: vertex2 });
this.adjacencyList[vertex2].push({ weight: weight, node: vertex1 });
};
WeightedGraph.prototype.Dijkstra = function (source, dest) {
const distances = {};
const previous = {};
const pq = new PriorityQueue();
const visited = new Set();
for (let v in this.adjacencyList) {
previous[v] = null;
if (v === source) distances[v] = 0;
else distances[v] = Infinity;
pq.enqueue(v, distances[v]);
}
while (pq.values.length) {
const current = pq.dequeue();
visited.add(current.value);
for (let neighbour of this.adjacencyList[current.value]) {
if (visited.has(neighbour.node)) continue;
let tempDist = (current.value === source ? 0 : current.priority) + neighbour.weight;
if (distances[neighbour.node] > tempDist) {
distances[neighbour.node] = tempDist;
pq.enqueue(neighbour.node, distances[neighbour.node]);
previous[neighbour.node] = current.value;
}
}
}
// Track back from destination to source
let result = [];
let current = dest;
while (current) {
result.unshift(current);
current = previous[current];
}
console.log(result);
};
const wg = new WeightedGraph();
wg.addVertex("A");
wg.addVertex("B");
wg.addVertex("C");
wg.addVertex("D");
wg.addVertex("E");
wg.addEdge("A", "B", 7);
wg.addEdge("A", "C", 3);
wg.addEdge("B", "C", 1);
wg.addEdge("B", "D", 2);
wg.addEdge("B", "E", 6);
wg.addEdge("C", "D", 2);
wg.addEdge("D", "E", 4);
wg.Dijkstra("A", "E"); // ["A", "C", "D", "E"]
var g = new WeightedGraph();
g.addVertex("A");
g.addVertex("Z");
g.addVertex("C");
g.addVertex("D");
g.addVertex("E");
g.addVertex("H");
g.addVertex("Q");
g.addVertex("G");
g.addEdge("A", "Z", 7);
g.addEdge("A", "C", 8);
g.addEdge("Z", "Q", 2);
g.addEdge("C", "G", 4);
g.addEdge("D", "Q", 8);
g.addEdge("E", "H", 1);
g.addEdge("H", "Q", 3);
g.addEdge("Q", "C", 6);
g.addEdge("G", "Q", 9);
g.Dijkstra("A", "E"); // ["A", "Z", "Q", "H", "E"]
g.Dijkstra("A", "Q"); // ["A", "Z", "Q"]
g.Dijkstra("A", "G"); // ["A", "C", "G"]
g.Dijkstra("A", "D"); // ["A", "Z", "Q", "D"]