forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimMST.js
208 lines (188 loc) · 6.51 KB
/
PrimMST.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
// Priority Queue Helper functions
function getParentPosition (position) {
// Get the parent node of the current node
return Math.floor((position - 1) / 2)
}
function getChildrenPosition (position) {
// Get the children nodes of the current node
return [2 * position + 1, 2 * position + 2]
}
class PriorityQueue {
// Priority Queue class using Minimum Binary Heap
constructor () {
this._heap = []
this.keys = {}
}
isEmpty () {
// Checking if the heap is empty
return this._heap.length === 0
}
push (key, priority) {
// Adding element to the queue (equivalent to add)
this._heap.push([key, priority])
this.keys[key] = this._heap.length - 1
this._shiftUp(this.keys[key])
}
pop () {
// Removing the element with least priority (equivalent to extractMin)
this._swap(0, this._heap.length - 1)
const [key] = this._heap.pop()
delete this.keys[key]
this._shiftDown(0)
return key
}
contains (key) {
// Check if a given key is present in the queue
return (key in this.keys)
}
update (key, priority) {
// Update the priority of the given element (equivalent to decreaseKey)
const currPos = this.keys[key]
this._heap[currPos][1] = priority
const parentPos = getParentPosition(currPos)
const currPriority = this._heap[currPos][1]
let parentPriority = Infinity
if (parentPos >= 0) {
parentPriority = this._heap[parentPos][1]
}
const [child1Pos, child2Pos] = getChildrenPosition(currPos)
let [child1Priority, child2Priority] = [Infinity, Infinity]
if (child1Pos < this._heap.length) {
child1Priority = this._heap[child1Pos][1]
}
if (child2Pos < this._heap.length) {
child2Priority = this._heap[child2Pos][1]
}
if (parentPos >= 0 && parentPriority > currPriority) {
this._shiftUp(currPos)
} else if (child2Pos < this._heap.length &&
(child1Priority < currPriority || child2Priority < currPriority)) {
this._shiftDown(currPos)
}
}
_shiftUp (position) {
// Helper function to shift up a node to proper position (equivalent to bubbleUp)
let currPos = position
let parentPos = getParentPosition(currPos)
let currPriority = this._heap[currPos][1]
let parentPriority = Infinity
if (parentPos >= 0) {
parentPriority = this._heap[parentPos][1]
}
while (parentPos >= 0 && parentPriority > currPriority) {
this._swap(currPos, parentPos)
currPos = parentPos
parentPos = getParentPosition(currPos)
currPriority = this._heap[currPos][1]
try {
parentPriority = this._heap[parentPos][1]
} catch (error) {
parentPriority = Infinity
}
}
this.keys[this._heap[currPos][0]] = currPos
}
_shiftDown (position) {
// Helper function to shift down a node to proper position (equivalent to bubbleDown)
let currPos = position
let [child1Pos, child2Pos] = getChildrenPosition(currPos)
let [child1Priority, child2Priority] = [Infinity, Infinity]
if (child1Pos < this._heap.length) {
child1Priority = this._heap[child1Pos][1]
}
if (child2Pos < this._heap.length) {
child2Priority = this._heap[child2Pos][1]
}
let currPriority
try {
currPriority = this._heap[currPos][1]
} catch {
return
}
while (child2Pos < this._heap.length &&
(child1Priority < currPriority || child2Priority < currPriority)) {
if (child1Priority < currPriority && child1Priority < child2Priority) {
this._swap(child1Pos, currPos)
currPos = child1Pos
} else {
this._swap(child2Pos, currPos)
currPos = child2Pos
}
[child1Pos, child2Pos] = getChildrenPosition(currPos)
try {
[child1Priority, child2Priority] = [this._heap[child1Pos][1], this._heap[child2Pos][1]]
} catch (error) {
[child1Priority, child2Priority] = [Infinity, Infinity]
}
currPriority = this._heap[currPos][1]
}
this.keys[this._heap[currPos][0]] = currPos
if (child1Pos < this._heap.length && child1Priority < currPriority) {
this._swap(child1Pos, currPos)
this.keys[this._heap[child1Pos][0]] = child1Pos
}
}
_swap (position1, position2) {
// Helper function to swap 2 nodes
[this._heap[position1], this._heap[position2]] = [this._heap[position2], this._heap[position1]]
this.keys[this._heap[position1][0]] = position1
this.keys[this._heap[position2][0]] = position2
}
}
class GraphWeightedUndirectedAdjacencyList {
// Weighted Undirected Graph class
constructor () {
this.connections = {}
}
addNode (node) {
// Function to add a node to the graph (connection represented by set)
this.connections[node] = {}
}
addEdge (node1, node2, weight) {
// Function to add an edge (adds the node too if they are not present in the graph)
if (!(node1 in this.connections)) { this.addNode(node1) }
if (!(node2 in this.connections)) { this.addNode(node2) }
this.connections[node1][node2] = weight
this.connections[node2][node1] = weight
}
PrimMST (start) {
// Prim's Algorithm to generate a Minimum Spanning Tree (MST) of a graph
// Details: https://en.wikipedia.org/wiki/Prim%27s_algorithm
const distance = {}
const parent = {}
const priorityQueue = new PriorityQueue()
// Initialization
for (const node in this.connections) {
distance[node] = (node === start.toString() ? 0 : Infinity)
parent[node] = null
priorityQueue.push(node, distance[node])
}
// Updating 'distance' object
while (!priorityQueue.isEmpty()) {
const node = priorityQueue.pop()
Object.keys(this.connections[node]).forEach(neighbour => {
if (priorityQueue.contains(neighbour) && distance[node] + this.connections[node][neighbour] < distance[neighbour]) {
distance[neighbour] = distance[node] + this.connections[node][neighbour]
parent[neighbour] = node
priorityQueue.update(neighbour, distance[neighbour])
}
})
}
// MST Generation from the 'parent' object
const graph = new GraphWeightedUndirectedAdjacencyList()
Object.keys(parent).forEach(node => {
if (node && parent[node]) {
graph.addEdge(node, parent[node], this.connections[node][parent[node]])
}
})
return graph
}
}
export { GraphWeightedUndirectedAdjacencyList }
// const graph = new GraphWeightedUndirectedAdjacencyList()
// graph.addEdge(1, 2, 1)
// graph.addEdge(2, 3, 2)
// graph.addEdge(3, 4, 1)
// graph.addEdge(3, 5, 100) // Removed in MST
// graph.addEdge(4, 5, 5)
// graph.PrimMST(1)