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