|
| 1 | +class Solution |
| 2 | +{ |
| 3 | + public: |
| 4 | + // Function to initialize the parent and rank arrays for Union-Find (Disjoint Set Union) |
| 5 | + void makeSet(vector<int>& rank, vector<int>& parent, int n){ |
| 6 | + for(int i = 0; i < n; i++){ |
| 7 | + parent[i] = i; // Each node is initially its own parent |
| 8 | + rank[i] = 0; // Rank of each node is initialized to 0 |
| 9 | + } |
| 10 | + } |
| 11 | + |
| 12 | + // Function to find the representative (root) of a set using path compression |
| 13 | + int findParent(vector<int>& parent, int node){ |
| 14 | + if(parent[node] == node) |
| 15 | + return node; // If the node is its own parent, it is the root |
| 16 | + // Recursively find the root and apply path compression |
| 17 | + return parent[node] = findParent(parent, parent[node]); |
| 18 | + } |
| 19 | + |
| 20 | + // Function to unify two sets using union by rank |
| 21 | + void unionSet(int source, int destination, vector<int>& parent, vector<int>& rank){ |
| 22 | + // Find the roots of the sets for both nodes |
| 23 | + source = findParent(parent, source); |
| 24 | + destination = findParent(parent, destination); |
| 25 | + |
| 26 | + // Attach the smaller ranked tree under the larger ranked tree |
| 27 | + if(rank[source] < rank[destination]) |
| 28 | + parent[source] = destination; // Attach source under destination |
| 29 | + else if(rank[destination] < rank[source]) |
| 30 | + parent[destination] = source; // Attach destination under source |
| 31 | + else{ |
| 32 | + // If ranks are equal, choose one root arbitrarily and increase its rank |
| 33 | + parent[destination] = source; |
| 34 | + rank[source]++; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Function to calculate the weight of the Minimum Spanning Tree (MST) |
| 39 | + int spanningTree(int V, vector<vector<int>> adj[]) |
| 40 | + { |
| 41 | + vector<vector<int>> edges; // Vector to store all edges in the graph |
| 42 | + |
| 43 | + // Convert adjacency list to an edge list |
| 44 | + for(int source = 0; source < V; source++){ |
| 45 | + for(auto &neighbour : adj[source]){ |
| 46 | + int destination = neighbour[0]; // Neighboring node |
| 47 | + int weight = neighbour[1]; // Weight of the edge |
| 48 | + |
| 49 | + // To avoid duplicate edges, only consider source < destination |
| 50 | + if(source < destination) |
| 51 | + edges.push_back({weight, source, destination}); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Arrays to store the rank and parent of each node |
| 56 | + vector<int> rank(V); |
| 57 | + vector<int> parent(V); |
| 58 | + makeSet(rank, parent, V); // Initialize the rank and parent arrays |
| 59 | + |
| 60 | + // Sort the edges by weight (Kruskal's algorithm requires this step) |
| 61 | + sort(edges.begin(), edges.end(), [](vector<int>& a, vector<int>& b) { |
| 62 | + return a[0] < b[0]; // Compare weights of the edges |
| 63 | + }); |
| 64 | + |
| 65 | + int minWeight = 0; // Variable to store the total weight of the MST |
| 66 | + |
| 67 | + // Iterate through the sorted edges |
| 68 | + for(int i = 0; i < edges.size(); i++){ |
| 69 | + int source = findParent(parent, edges[i][1]); // Find root of source |
| 70 | + int destination = findParent(parent, edges[i][2]); // Find root of destination |
| 71 | + int weight = edges[i][0]; // Weight of the current edge |
| 72 | + |
| 73 | + // If the nodes belong to different sets, include this edge in the MST |
| 74 | + if(source != destination) { |
| 75 | + minWeight += weight; // Add the weight of the edge to the MST |
| 76 | + unionSet(source, destination, parent, rank); // Union the sets |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return minWeight; // Return the total weight of the MST |
| 81 | + } |
| 82 | +}; |
0 commit comments