Advertisement
kartikkukreja

Kruskal's MST algorithm

May 19th, 2013
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. // representation of undirected edge (u, v) having weight 'weight'
  6. struct Edge {
  7.     int u, v, weight;
  8. };
  9.  
  10. // Union-find data structure
  11. class UF    {
  12.     int *id, cnt, *sz;
  13. public:
  14.     // Create an empty union find data structure with N isolated sets.
  15.     UF(int N)   {
  16.         cnt = N;
  17.     id = new int[N];
  18.     sz = new int[N];
  19.         for(int i=0; i<N; i++)  {
  20.             id[i] = i;
  21.         sz[i] = 1;
  22.     }
  23.     }
  24.     ~UF()   {
  25.     delete [] id;
  26.     delete [] sz;
  27.     }
  28.     // Return the id of component corresponding to object p.
  29.     int find(int p) {
  30.         int root = p;
  31.         while (root != id[root])
  32.             root = id[root];
  33.         while (p != root) {
  34.             int newp = id[p];
  35.             id[p] = root;
  36.             p = newp;
  37.         }
  38.         return root;
  39.     }
  40.     // Replace sets containing x and y with their union.
  41.     void merge(int x, int y)    {
  42.         int i = find(x);
  43.         int j = find(y);
  44.         if (i == j) return;
  45.        
  46.         // make smaller root point to larger one
  47.         if   (sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; }
  48.         else                 { id[j] = i; sz[i] += sz[j]; }
  49.         cnt--;
  50.     }
  51.     // Are objects x and y in the same set?
  52.     bool connected(int x, int y)    {
  53.         return find(x) == find(y);
  54.     }
  55.     // Return the number of disjoint sets.
  56.     int count() {
  57.         return cnt;
  58.     }
  59. };
  60.  
  61. // comparator function for sorting edges in ascending order by weight
  62. bool comp(Edge *a, Edge *b)   {
  63.     return a->weight < b->weight;
  64. }
  65.  
  66. int main()  {
  67.     int V, E, i, N, u, v, cost;
  68.     Edge **edges, **mst;
  69.  
  70.     // Assuming vertices are labeled 0...V-1
  71.     //freopen("input1.txt", "r", stdin);
  72.  
  73.     scanf("%d %d", &V, &E); // Enter the number of vertices and edges
  74.     edges = new Edge*[E];
  75.     for(i=0; i<E; i++)  {   // Enter E undirected edges (u, v, weight)
  76.         edges[i] = new Edge;
  77.         scanf("%d %d %d", &(edges[i]->u), &(edges[i]->v), &(edges[i]->weight));
  78.     }
  79.     sort(edges, edges + E, comp);
  80.  
  81.     UF uf(V);
  82.     mst = new Edge*[V-1];
  83.     for(N=i=cost=0; i<E && N<V-1; i++) {
  84.         u = edges[i]->u; v = edges[i]->v;
  85.         if(!uf.connected(u, v)) {
  86.             uf.merge(u, v);
  87.             mst[N++] = edges[i];
  88.             cost += edges[i]->weight;
  89.         }
  90.     }
  91.  
  92.     printf("Total cost of MST : %d\n", cost);
  93.     printf("Edges in MST :\n");
  94.     for(i=0; i<N; i++)
  95.         printf("%d %d %d\n", mst[i]->u, mst[i]->v, mst[i]->weight);
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement