|
| 1 | +/** |
| 2 | + * Graphs: Implement Kruskal's Algorithm in JavaScript |
| 3 | + * |
| 4 | + * This code implements Kruskal's Algorithm to find the Minimum Spanning Tree (MST) |
| 5 | + * in a graph. The graph is represented using an adjacency list. The algorithm works |
| 6 | + * by repeatedly adding the minimum weight edges that do not form a cycle in the MST. |
| 7 | + * |
| 8 | + * Time Complexity: O(E log V), where E is the number of edges and V is the number of vertices. |
| 9 | + * Space Complexity: O(V), where V is the number of vertices. |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Class representing a Disjoint Set data structure. |
| 14 | + * Used to track disjoint sets and perform union-find operations. |
| 15 | + */ |
| 16 | +class DisjointSet { |
| 17 | + constructor(n) { |
| 18 | + this.parent = new Array(n).fill(-1); // Array to store parent of each node |
| 19 | + this.rank = new Array(n).fill(0); // Array to store the rank of each node |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Find the parent of a node in the disjoint set. |
| 24 | + * Implements path compression to optimize future finds. |
| 25 | + * @param {number} x - The node to find the parent for. |
| 26 | + * @returns {number} The parent of the given node. |
| 27 | + */ |
| 28 | + find(x) { |
| 29 | + if (this.parent[x] === -1) { |
| 30 | + return x; |
| 31 | + } |
| 32 | + this.parent[x] = this.find(this.parent[x]); // Path compression |
| 33 | + return this.parent[x]; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Union two disjoint sets by rank. |
| 38 | + * Uses union by rank to optimize the merge operation. |
| 39 | + * @param {number} x - The first node to union. |
| 40 | + * @param {number} y - The second node to union. |
| 41 | + */ |
| 42 | + union(x, y) { |
| 43 | + let xRoot = this.find(x); |
| 44 | + let yRoot = this.find(y); |
| 45 | + |
| 46 | + if (this.rank[xRoot] < this.rank[yRoot]) { |
| 47 | + this.parent[xRoot] = yRoot; |
| 48 | + } else if (this.rank[xRoot] > this.rank[yRoot]) { |
| 49 | + this.parent[yRoot] = xRoot; |
| 50 | + } else { |
| 51 | + this.parent[yRoot] = xRoot; |
| 52 | + this.rank[xRoot]++; |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Function to implement Kruskal's Algorithm for finding the Minimum Spanning Tree (MST). |
| 59 | + * @param {number} n - The number of vertices in the graph. |
| 60 | + * @param {Array} edges - The edges of the graph represented as an adjacency list. |
| 61 | + * @returns {Array} The edges of the Minimum Spanning Tree. |
| 62 | + */ |
| 63 | +function kruskalsAlgorithm(n, edges) { |
| 64 | + // Sort edges in non-decreasing order by weight |
| 65 | + edges.sort((a, b) => a[2] - b[2]); |
| 66 | + |
| 67 | + const mst = []; // Minimum Spanning Tree |
| 68 | + const disjointSet = new DisjointSet(n); // Create a disjoint set for tracking sets |
| 69 | + |
| 70 | + for (let [src, dest, weight] of edges) { |
| 71 | + let srcRoot = disjointSet.find(src); |
| 72 | + let destRoot = disjointSet.find(dest); |
| 73 | + |
| 74 | + // If adding the edge does not create a cycle, add it to the MST |
| 75 | + if (srcRoot !== destRoot) { |
| 76 | + mst.push([src, dest, weight]); |
| 77 | + disjointSet.union(srcRoot, destRoot); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return mst; |
| 82 | +} |
| 83 | + |
| 84 | +// Example usage |
| 85 | +const numVertices = 5; |
| 86 | +const graphEdges = [ |
| 87 | + [0, 1, 2], |
| 88 | + [1, 2, 3], |
| 89 | + [0, 3, 6], |
| 90 | + [1, 3, 8], |
| 91 | + [1, 4, 5], |
| 92 | + [2, 4, 7], |
| 93 | + [3, 4, 9], |
| 94 | +]; |
| 95 | + |
| 96 | +const minimumSpanningTree = kruskalsAlgorithm(numVertices, graphEdges); |
| 97 | +console.log("Minimum Spanning Tree:", minimumSpanningTree); |
0 commit comments