Facebook
From Gentle Hedgehog, 4 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 216
  1. // Java program for Kruskal's algorithm to find Minimum
  2. // Spanning Tree of a given connected, undirected and
  3. // weighted graph
  4. import java.util.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. class Graph
  9. {
  10.         // A class to represent a graph edge
  11.         class Edge implements Comparable<Edge>
  12.         {
  13.                 int src, dest, weight;
  14.  
  15.                 // Comparator function used for sorting edges
  16.                 // based on their weight
  17.                 public int compareTo(Edge compareEdge)
  18.                 {
  19.                         return this.weight-compareEdge.weight;
  20.                 }
  21.         };
  22.  
  23.         // A class to represent a subset for union-find
  24.         class subset
  25.         {
  26.                 int parent, rank;
  27.         };
  28.  
  29.         int V, E; // V-> no. of vertices & E->no.of edges
  30.         Edge edge[]; // collection of all edges
  31.  
  32.         // Creates a graph with V vertices and E edges
  33.         Graph(int v, int e)
  34.         {
  35.                 V = v;
  36.                 E = e;
  37.                 edge = new Edge[E];
  38.                 for (int i=0; i<e; ++i)
  39.                         edge[i] = new Edge();
  40.         }
  41.  
  42.         // A utility function to find set of an element i
  43.         // (uses path compression technique)
  44.         int find(subset subsets[], int i)
  45.         {
  46.                 // find root and make root as parent of i (path compression)
  47.                 if (subsets[i].parent != i)
  48.                         subsets[i].parent = find(subsets, subsets[i].parent);
  49.  
  50.                 return subsets[i].parent;
  51.         }
  52.  
  53.         // A function that does union of two sets of x and y
  54.         // (uses union by rank)
  55.         void Union(subset subsets[], int x, int y)
  56.         {
  57.                 int xroot = find(subsets, x);
  58.                 int yroot = find(subsets, y);
  59.  
  60.                 // Attach smaller rank tree under root of high rank tree
  61.                 // (Union by Rank)
  62.                 if (subsets[xroot].rank < subsets[yroot].rank)
  63.                         subsets[xroot].parent = yroot;
  64.                 else if (subsets[xroot].rank > subsets[yroot].rank)
  65.                         subsets[yroot].parent = xroot;
  66.  
  67.                 // If ranks are same, then make one as root and increment
  68.                 // its rank by one
  69.                 else
  70.                 {
  71.                         subsets[yroot].parent = xroot;
  72.                         subsets[xroot].rank++;
  73.                 }
  74.         }
  75.  
  76.         // The main function to construct MST using Kruskal's algorithm
  77.         void KruskalMST()
  78.         {
  79.                 Edge result[] = new Edge[V]; // Tnis will store the resultant MST
  80.                 int e = 0; // An index variable, used for result[]
  81.                 int i = 0; // An index variable, used for sorted edges
  82.                 for (i=0; i<V; ++i)
  83.                         result[i] = new Edge();
  84.  
  85.                 // Step 1: Sort all the edges in non-decreasing order of their
  86.                 // weight. If we are not allowed to change the given graph, we
  87.                 // can create a copy of array of edges
  88.                 Arrays.sort(edge);
  89.  
  90.                 // Allocate memory for creating V ssubsets
  91.                 subset subsets[] = new subset[V];
  92.                 for(i=0; i<V; ++i)
  93.                         subsets[i]=new subset();
  94.  
  95.                 // Create V subsets with single elements
  96.                 for (int v = 0; v < V; ++v)
  97.                 {
  98.                         subsets[v].parent = v;
  99.                         subsets[v].rank = 0;
  100.                 }
  101.  
  102.                 i = 0; // Index used to pick next edge
  103.  
  104.                 // Number of edges to be taken is equal to V-1
  105.                 while (e < V - 1)
  106.                 {
  107.                         // Step 2: Pick the smallest edge. And increment
  108.                         // the index for next iteration
  109.                         Edge next_edge = new Edge();
  110.                         next_edge = edge[i++];
  111.  
  112.                         int x = find(subsets, next_edge.src);
  113.                         int y = find(subsets, next_edge.dest);
  114.  
  115.                         // If including this edge does't cause cycle,
  116.                         // include it in result and increment the index
  117.                         // of result for next edge
  118.                         if (x != y)
  119.                         {
  120.                                 result[e++] = next_edge;
  121.                                 Union(subsets, x, y);
  122.                         }
  123.                         // Else discard the next_edge
  124.                 }
  125.  
  126.                 // print the contents of result[] to display
  127.                 // the built MST
  128.                 System.out.println("Following are the edges in " +
  129.                                                                         "the constructed MST");
  130.                 for (i = 0; i < e; ++i)
  131.                         System.out.println(result[i].src+" -- " +
  132.                                 result[i].dest+" == " + result[i].weight);
  133.         }
  134.  
  135.         // Driver Program
  136.         public static void main (String[] args)
  137.         {
  138.  
  139.                 /* Let us create following weighted graph
  140.                                 10
  141.                         0--------1
  142.                         | \      |
  143.                 6| 5\ |15
  144.                         |        \ |
  145.                         2--------3
  146.                                 4        */
  147.                 int V = 4; // Number of vertices in graph
  148.                 int E = 5; // Number of edges in graph
  149.                 Graph graph = new Graph(V, E);
  150.  
  151.                 // add edge 0-1
  152.                 graph.edge[0].src = 0;
  153.                 graph.edge[0].dest = 1;
  154.                 graph.edge[0].weight = 10;
  155.  
  156.                 // add edge 0-2
  157.                 graph.edge[1].src = 0;
  158.                 graph.edge[1].dest = 2;
  159.                 graph.edge[1].weight = 6;
  160.  
  161.                 // add edge 0-3
  162.                 graph.edge[2].src = 0;
  163.                 graph.edge[2].dest = 3;
  164.                 graph.edge[2].weight = 5;
  165.  
  166.                 // add edge 1-3
  167.                 graph.edge[3].src = 1;
  168.                 graph.edge[3].dest = 3;
  169.                 graph.edge[3].weight = 15;
  170.  
  171.                 // add edge 2-3
  172.                 graph.edge[4].src = 2;
  173.                 graph.edge[4].dest = 3;
  174.                 graph.edge[4].weight = 4;
  175.  
  176.                 graph.KruskalMST();
  177.         }
  178. }
  179. //This code is contributed by Aakash Hasija
  180.