-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra.js
149 lines (126 loc) · 5.4 KB
/
dijkstra.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
class PriorityQueue {
constructor() {
this.heap = [];
}
enqueue(node, distance) {
const element = { node, distance };
this.heap.push(element);
this.heapifyUp();
}
dequeue() {
const root = this.heap[0];
if (this.heap.length > 1) {
this.heap[0] = this.heap.pop();
this.heapifyDown();
} else {
this.heap.pop();
}
return root;
}
isEmpty() {
return this.heap.length === 0;
}
heapifyUp() {
let index = this.heap.length - 1;
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.heap[index].distance < this.heap[parentIndex].distance) {
this.swap(index, parentIndex);
index = parentIndex;
} else {
break;
}
}
}
heapifyDown() {
let index = 0;
while (index < this.heap.length) {
const leftChildIndex = 2 * index + 1;
const rightChildIndex = 2 * index + 2;
let smallestChildIndex = index;
if (leftChildIndex < this.heap.length &&
this.heap[leftChildIndex].distance < this.heap[smallestChildIndex].distance) {
smallestChildIndex = leftChildIndex;
}
if (rightChildIndex < this.heap.length &&
this.heap[rightChildIndex].distance < this.heap[smallestChildIndex].distance) {
smallestChildIndex = rightChildIndex;
}
if (smallestChildIndex !== index) {
this.swap(index, smallestChildIndex);
index = smallestChildIndex;
} else {
break;
}
}
}
swap(index1, index2) {
const temp = this.heap[index1];
this.heap[index1] = this.heap[index2];
this.heap[index2] = temp;
}
decreaseKey(node, newDistance) {
let index = this.heap.findIndex(element => element.node === node);
if (index === -1) return;
this.heap[index].distance = newDistance;
this.heapifyUp();
}
getQueueContents() {
return this.heap.map(element => ({ node: element.node, distance: element.distance }));
}
}
function dijkstra(nodes, startNode, selectedNodeCount) {
let distances = {};
const visited = {};
const pq = new PriorityQueue();
pqStates = {};
shortestPaths = [];
shortestPathDraw = [];
for (let node in nodes) {
if (node.charCodeAt(0) - 65 < selectedNodeCount) {
distances[node] = Infinity;
visited[node] = false;
}
}
distances[startNode] = 0;
visited[startNode] = false;
pq.enqueue(startNode, 0);
while (!pq.isEmpty()) {
let { node, distance } = pq.dequeue();
for (let neighbor in nodes[node]) {
if (neighbor.charCodeAt(0) - 65 < selectedNodeCount) {
let newDistance = distance + nodes[node][neighbor];
if (!visited[neighbor] && newDistance < distances[neighbor]) {
distances[neighbor] = newDistance;
if (pq.heap.some(element => element.node === neighbor)) {
pq.decreaseKey(neighbor, newDistance);
} else {
pq.enqueue(neighbor, newDistance);
}
}
}
}
pq.heap.sort((a, b) => {
if (a.distance !== b.distance) {
return a.distance - b.distance;
}
return a.node.localeCompare(b.node);
});
visited[node] = true;
shortestPaths.push({ node, distance, pq: [...pq.heap] });
pqStates[node] = pq.getQueueContents();
path = [];
let currentNode = node;
while (currentNode !== startNode) {
path.unshift({ node: currentNode, distance: distances[currentNode] });
currentNode = Object.keys(nodes[currentNode]).find(neighbor =>
distances[currentNode] - nodes[currentNode][neighbor] === distances[neighbor]
);
if (!currentNode) {
break;
}
}
path.unshift({ node: startNode, distance: 0 });
shortestPathDraw.push(path);
}
}