-
Notifications
You must be signed in to change notification settings - Fork 11
/
shortestPathGraph.js
333 lines (262 loc) · 10.5 KB
/
shortestPathGraph.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
class Node
{
constructor(value, priority)
{
this.value = value;
this.priority = priority;
}
}
class PriorityQueue
{
constructor()
{
this.datas = [];
this.count = 0;
}
enqueue(value, priority) // Time Complexity O(log n)
{
this.count++;
let newNode = new Node(value, priority);
this.datas.push(newNode);
this.bubbleUp();
}
bubbleUp() // Time Complexity O(log n)
{
let index = this.datas.length - 1;
const elem = this.datas[index]; // 'data' will be stored in 'elem'
while(index > 0) // Index must be greater than 0
{
let parentIndex = Math.floor((index - 1) / 2); // Parent Index
let parent = this.datas[parentIndex]; // Parent index's data
if(elem.priority >= parent.priority) break;
// If the input 'data's priority is >= to data's priority in parent index, then we'll break out;
// Otherwise we'll swap the values
this.datas[parentIndex] = elem; // Will swap 'data' with parent index's data
this.datas[index] = parent; // Then, new 'data' will be parent value
index = parentIndex; // Update the index to parent index
}
}
dequeue() // Time Complexity O(log n)
{
this.count--;
let min = this.datas[0]; // Min data in Min ER
let endData = this.datas.pop(); // Last data in Min ER
let length = this.datas.length;
if(length > 0) // if array length is greater than 0
{
this.datas[0] = endData; // The value of Min data is now 'endData'
this.bubbleDown();
}
return min;
}
bubbleDown() // Time Complexity O(log n)
{
let index = 0;
const length = this.datas.length;
const element = this.datas[0];
while(true)
{
let leftChildIndex = 2 * index + 1;
let rightChildIndex = 2 * index + 2;
let leftChild, rightChild;
let swap = null; // It's going to keep track of the position, we'er going to swap with
if(leftChildIndex < length) // leftChildIndex is inbound, then assign the value to the left child
{
leftChild = this.datas[leftChildIndex];
if(leftChild.priority < element.priority) // If leftChild is lesser than element then set swap equal to leftChildIndex
{
swap = leftChildIndex;
}
}
if(rightChildIndex < length) // rightChildIndex is inbound, then assign the value to the right child
{
rightChild = this.datas[rightChildIndex];
if
(
(swap === null && rightChild.priority < element.priority) || // If swap is still null & rightChild is lesser than element
(swap !== null && rightChild.priority < leftChild.priority) // If swap isn't null & rightChild is lesser than leftChild
)
{
swap = rightChildIndex; // then set swap equal to rightChildIndex
}
}
if(swap === null) break;
// Otherwise we'll swap the values
this.datas[index] = this.datas[swap]; // Will swap the data in 'swap's' index with parent data's index
this.datas[swap] = element; // Then, new 'data' will be parent value
index = swap; // Update the index to parent index
}
}
}
class ShortestPathGraph
{
constructor()
{
this.adjacencyList = {};
}
addVertex(vertex) // Time Complexity O(1)
{
if(!this.adjacencyList[vertex]) this.adjacencyList[vertex] = []; // If the vertex doesn't exist, then add it.
}
removeVertex(vertex) // Time Complexity O(|VERTEX| + |EDGE|)
{
if(this.adjacencyList[vertex])
{
while(this.adjacencyList[vertex].length)
{
this.adjacencyList[vertex].pop(); // Will pop all the value
for (let key of Object.keys(this.adjacencyList)) // Will loop through all keys
{
if(this.adjacencyList[key].filter(value => value !== vertex))
{
this.removeEdge(key, vertex); // Will remove all edges
}
}
}
delete this.adjacencyList[vertex];
}
else throw new Error(`Vertex doesn't exist`);
}
addEdge(vertexOne, vertexTwo, weight) // Time Complexity O(1)
{
if(this.adjacencyList[vertexOne] && this.adjacencyList[vertexTwo]) // If both vertices exist
{
this.adjacencyList[vertexOne].push({Node: vertexTwo, Value: weight});
this.adjacencyList[vertexTwo].push({Node: vertexOne, Value: weight});
}
else throw new Error(`Vertex doesn't exist`);
}
removeEdge(vertexOne, vertexTwo) // Time Complexity O(|EDGE|)
{
if(this.adjacencyList[vertexOne] && this.adjacencyList[vertexTwo]) // If both vertices exist
{
this.adjacencyList[vertexOne] = this.adjacencyList[vertexOne].filter(vertex => vertex.Node !== vertexTwo);
this.adjacencyList[vertexTwo] = this.adjacencyList[vertexTwo].filter(vertex => vertex.Node !== vertexOne );
}
else throw new Error(`Vertex doesn't exist`);
}
printGraph()
{
console.log('Graph:\n', this.adjacencyList);
}
dijkstra(startVertex, finishVertex)
{
const nodes = new PriorityQueue()
const distances = {};
const previous = {};
let smallest;
let path = [];
// Building up initial state
for (let vertex in this.adjacencyList) // Loop through all vertices
{
if (vertex === startVertex)
{
distances[vertex] = 0;
nodes.enqueue(vertex, 0);
}
else
{
distances[vertex] = Infinity;
nodes.enqueue(vertex, Infinity);
}
previous[vertex] = null;
}
let nodesLength = nodes.datas.length;
while(nodesLength) // As long as there is something to visit
{
smallest = nodes.dequeue().value;
if(smallest === finishVertex)
{
while(previous[smallest]) // Building up the Path
{
path.push(smallest);
smallest = previous[smallest];
}
break;
}
if(smallest || distances[smallest] !== Infinity)
{
for (let neighbor in this.adjacencyList[smallest])
{
let nextNode = this.adjacencyList[smallest][neighbor]; // Finds neighboring node
// Calculate new distance to neighboring node
let newDistance = distances[smallest] + nextNode.Value; // Current smallest value + next node's Value
let nextNeighbor = nextNode.Node;
if(newDistance < distances[nextNeighbor])
{
distances[nextNeighbor] = newDistance; // Updating new smallest distance to neighbor
previous[nextNeighbor] = smallest; // Updating previous - how we got to neighbor
nodes.enqueue(nextNeighbor, newDistance); // Equeue in PQ with new priority
}
}
}
}
let result = path.concat(smallest).reverse().toString().replace(/,/g, ' -> ');
console.log(`Shortest Path: ${result}`);
return result;
}
}
// This is an WEIGHTED UNDIRECTED GRAPH
/*
Dhaka
1500 / \ 1200
/ 1300 \
Tokyo------Istanbul
| |
1400 | | 1000
| 500 |
London------Berlin
\ /
600 \ / 800
NYC
*/
let graph = new ShortestPathGraph();
graph.addVertex("Tokyo");
graph.addVertex("Dhaka");
graph.addVertex("Istanbul");
graph.addVertex("London");
graph.addVertex("Berlin");
graph.addVertex("NYC");
graph.addEdge("Dhaka", "Tokyo", 1500);
graph.addEdge("Dhaka", "Istanbul", 1200);
graph.addEdge("Tokyo","London", 1400);
graph.addEdge("Berlin", "Istanbul", 1000);
graph.addEdge("London", "Berlin", 500);
graph.addEdge("London", "NYC", 600);
graph.addEdge("NYC", "Berlin", 800);
graph.addEdge("Istanbul", "Tokyo", 1300);
graph.printGraph();
graph.dijkstra("Dhaka", "NYC");
graph.removeEdge("Dhaka", "Istanbul", 1200);
/*
Dhaka
1500 /
/ 1300
Tokyo------Istanbul
| |
1400 | | 1000
| 500 |
London------Berlin
\ /
600 \ / 800
NYC
*/
graph.printGraph();
graph.dijkstra("Berlin", "Dhaka");
graph.removeVertex("Berlin");
graph.addEdge("Istanbul", "NYC", 600);
/*
Dhaka
1500 /
/ 1300
Tokyo------Istanbul
| |
1400 | |
| |
London | 600
\ /
600 \ /
NYC
*/
graph.printGraph();
graph.dijkstra("NYC", "Dhaka");