mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-13 08:21:36 +08:00

* ref: KeyPriorityQueue in separate file #1298 * feat: add tests for KeyPriorityQueue #1298 * fix: _shiftDown refactored and corrected #1298 * fix: use KeyPriorityQueue in PrimMST #1298 * feat: add test for PrimMST #1298 * fix: format files #1298 * fix: minor coding style changes * fix: use map for keys and priorities #1298
57 lines
2.0 KiB
JavaScript
57 lines
2.0 KiB
JavaScript
import { KeyPriorityQueue } from '../Data-Structures/Heap/KeyPriorityQueue'
|
|
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 KeyPriorityQueue()
|
|
// 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 }
|