-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (104 loc) · 3.4 KB
/
index.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
class PriorityQueue {
constructor(comparisonFn) {
this.heap = [];
this.comparisonFn = comparisonFn;
}
enqueue(element) {
this.heap.push(element);
this.siftUp();
}
dequeue() {
if (this.heap.length <= 1) {
return this.heap.pop();
}
const result = this.heap[0];
this.heap[0] = this.heap.pop();
this.siftDown();
return result;
}
siftUp() {
let index = this.heap.length - 1;
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.comparisonFn(this.heap[index], this.heap[parentIndex]) < 0) {
[this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];
index = parentIndex;
} else {
break;
}
}
}
siftDown() {
let index = 0;
while (index < this.heap.length) {
const leftChildIndex = 2 * index + 1;
const rightChildIndex = 2 * index + 2;
let minIndex = index;
if (leftChildIndex < this.heap.length && this.comparisonFn(this.heap[leftChildIndex], this.heap[minIndex]) < 0) {
minIndex = leftChildIndex;
}
if (rightChildIndex < this.heap.length && this.comparisonFn(this.heap[rightChildIndex], this.heap[minIndex]) < 0) {
minIndex = rightChildIndex;
}
if (minIndex !== index) {
[this.heap[index], this.heap[minIndex]] = [this.heap[minIndex], this.heap[index]];
index = minIndex;
} else {
break;
}
}
}
}
function shortestPath(start, end, graph) {
// Set up distances object, with start node having distance 0 and all other nodes having infinite distance
const distances = {};
for (const node of Object.keys(graph)) {
distances[node] = node === start ? 0 : Infinity;
}
// Set up unvisited set
const unvisited = new Set(Object.keys(graph));
// Set up previous node tracking
const previous = {};
// Set up the priority queue
const pq = new PriorityQueue((a, b) => distances[a] < distances[b]);
pq.enqueue(start);
// While there are still unvisited nodes
while (unvisited.size > 0) {
// Get the node with the smallest distance
const current = pq.dequeue();
// If we have reached the end node, return the path
if (current === end) {
return reconstructPath(previous, end);
}
// Mark the current node as visited
unvisited.delete(current);
// Update the distances of the current node's neighbors
for (const neighbor of Object.keys(graph[current])) {
const distance = distances[current] + graph[current][neighbor];
if (distance < distances[neighbor]) {
distances[neighbor] = distance;
previous[neighbor] = current;
pq.enqueue(neighbor);
}
}
}
// If we have exhausted the graph without finding the end node, there is no path
return null;
}
function reconstructPath(previous, end) {
const path = [end];
let current = end;
while (current in previous) {
path.unshift(previous[current]);
current = previous[current];
}
return path;
}
// Example usage
const graph = {
'a': { 'b': 1, 'c': 4 },
'b': { 'c': 2, 'd': 5 },
'c': { 'd': 1 },
'd': {}
};
console.log(shortestPath('a', 'd', graph)); // ['a', 'b', 'c', 'd']