|
| 1 | +// C++ program for Kruskal's algorithm to find Minimum |
| 2 | +// Spanning Tree of a given connected, undirected and |
| 3 | +// weighted graph |
| 4 | +#include<bits/stdc++.h> |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +// Creating shortcut for an integer pair |
| 8 | + |
| 9 | + |
| 10 | +// Structure to represent a graph |
| 11 | +struct Graph |
| 12 | +{ |
| 13 | + int V, E; |
| 14 | + vector< pair<int, pair<int, int>> > edges; |
| 15 | + Graph(int V, int E) |
| 16 | + { |
| 17 | + this->V = V; |
| 18 | + this->E = E; |
| 19 | + } |
| 20 | + // Utility function to add an edge |
| 21 | + void addEdge(int u, int w, int v) |
| 22 | + { |
| 23 | + edges.push_back({w, {u, v}}); |
| 24 | + } |
| 25 | + int kruskalMST(); |
| 26 | +}; |
| 27 | + |
| 28 | +// To represent Disjoint Sets |
| 29 | +struct DisjointSets |
| 30 | +{ |
| 31 | + int *parent, *rnk; |
| 32 | + int n; |
| 33 | + |
| 34 | + // Constructor. |
| 35 | + DisjointSets(int n) |
| 36 | + { |
| 37 | + // Allocate memory |
| 38 | + this->n = n; |
| 39 | + parent = new int[n+1]; |
| 40 | + rnk = new int[n+1]; |
| 41 | + for (int i = 0; i <= n; i++) |
| 42 | + { |
| 43 | + rnk[i] = 0; |
| 44 | + parent[i] = i; |
| 45 | + } |
| 46 | + } |
| 47 | + int find(int u) |
| 48 | + { |
| 49 | + if (u != parent[u]) |
| 50 | + parent[u] = find(parent[u]); |
| 51 | + return parent[u]; |
| 52 | + } |
| 53 | + |
| 54 | + // Union by rank |
| 55 | + void merge(int x, int y) |
| 56 | + { |
| 57 | + x = find(x), y = find(y); |
| 58 | + |
| 59 | + /* Make tree with smaller height |
| 60 | + a subtree of the other tree */ |
| 61 | + if (rnk[x] > rnk[y]) |
| 62 | + parent[y] = x; |
| 63 | + else // If rnk[x] <= rnk[y] |
| 64 | + parent[x] = y; |
| 65 | + |
| 66 | + if (rnk[x] == rnk[y]) |
| 67 | + rnk[y]++; |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +/* Functions returns weight of the MST*/ |
| 72 | + |
| 73 | +int Graph::kruskalMST() |
| 74 | +{ |
| 75 | + int mst_wt = 0; // Initialize result |
| 76 | + |
| 77 | + // Sort edges in increasing order on basis of cost |
| 78 | + sort(edges.begin(), edges.end()); |
| 79 | + |
| 80 | + // Create disjoint sets |
| 81 | + DisjointSets ds(V); |
| 82 | + |
| 83 | + // Iterate through all sorted edges |
| 84 | + vector< pair<int, pair<int, int>> >::iterator it; |
| 85 | + for (it=edges.begin(); it!=edges.end(); it++) |
| 86 | + { |
| 87 | + int u = it->second.first; |
| 88 | + int v = it->second.second; |
| 89 | + |
| 90 | + int set_u = ds.find(u); |
| 91 | + int set_v = ds.find(v); |
| 92 | + |
| 93 | + // Check if the selected edge is creating |
| 94 | + // a cycle or not (Cycle is created if u |
| 95 | + // and v belong to same set) |
| 96 | + if (set_u != set_v) |
| 97 | + { |
| 98 | + cout << u << " - " << v << endl; |
| 99 | + mst_wt += it->first; |
| 100 | + ds.merge(set_u, set_v); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return mst_wt; |
| 105 | +} |
| 106 | + |
| 107 | +// Driver program to test above functions |
| 108 | +int main() |
| 109 | +{ |
| 110 | + /* Let us create above shown weighted |
| 111 | + and unidrected graph */ |
| 112 | + int V = 9, E = 14; |
| 113 | + Graph g(V, E); |
| 114 | + // making above shown graph |
| 115 | + g.addEdge(0, 5, 1); |
| 116 | + g.addEdge(0, 10, 3); |
| 117 | + g.addEdge(1, 2, 2); |
| 118 | + g.addEdge(1, 10, 5); |
| 119 | + g.addEdge(1, 5, 4); |
| 120 | + g.addEdge(2, 1, 3); |
| 121 | + g.addEdge(2, 5, 4); |
| 122 | + g.addEdge(2, 3, 0); |
| 123 | + g.addEdge(4, 2, 5); |
| 124 | + |
| 125 | + cout << "Edges of MST are \n"; |
| 126 | + int mst_wt = g.kruskalMST(); |
| 127 | + |
| 128 | + cout << "\nWeight of MST is " << mst_wt; |
| 129 | + |
| 130 | + return 0; |
| 131 | +} |
0 commit comments