|
| 1 | +// Find MST of given graph |
| 2 | +import java.util.*; |
| 3 | +import java.lang.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Graph |
| 7 | +{ |
| 8 | + class Edge implements Comparable<Edge> |
| 9 | + { |
| 10 | + int src, dest, weight; |
| 11 | + public int compareTo(Edge compareEdge) |
| 12 | + { |
| 13 | + return this.weight-compareEdge.weight; |
| 14 | + } |
| 15 | + }; |
| 16 | + class subset |
| 17 | + { |
| 18 | + int parent, rank; |
| 19 | + }; |
| 20 | + |
| 21 | + int V, E; |
| 22 | + Edge edge[]; |
| 23 | + Graph(int v, int e) |
| 24 | + { |
| 25 | + V = v; |
| 26 | + E = e; |
| 27 | + edge = new Edge[E]; |
| 28 | + for (int i=0; i<e; ++i) |
| 29 | + edge[i] = new Edge(); |
| 30 | + } |
| 31 | + int find(subset subsets[], int i) |
| 32 | + { |
| 33 | + if (subsets[i].parent != i) |
| 34 | + subsets[i].parent = find(subsets, subsets[i].parent); |
| 35 | + |
| 36 | + return subsets[i].parent; |
| 37 | + } |
| 38 | + void Union(subset subsets[], int x, int y) |
| 39 | + { |
| 40 | + int xroot = find(subsets, x); |
| 41 | + int yroot = find(subsets, y); |
| 42 | + if (subsets[xroot].rank < subsets[yroot].rank) |
| 43 | + subsets[xroot].parent = yroot; |
| 44 | + else if (subsets[xroot].rank > subsets[yroot].rank) |
| 45 | + subsets[yroot].parent = xroot; |
| 46 | + |
| 47 | + else |
| 48 | + { |
| 49 | + subsets[yroot].parent = xroot; |
| 50 | + subsets[xroot].rank++; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + void KruskalMST() |
| 55 | + { |
| 56 | + Edge result[] = new Edge[V]; |
| 57 | + int e = 0; |
| 58 | + int i = 0; |
| 59 | + for (i=0; i<V; ++i) |
| 60 | + result[i] = new Edge(); |
| 61 | + Arrays.sort(edge); |
| 62 | + subset subsets[] = new subset[V]; |
| 63 | + for(i=0; i<V; ++i) |
| 64 | + subsets[i]=new subset(); |
| 65 | + for (int v = 0; v < V; ++v) |
| 66 | + { |
| 67 | + subsets[v].parent = v; |
| 68 | + subsets[v].rank = 0; |
| 69 | + } |
| 70 | + |
| 71 | + i = 0; |
| 72 | + while (e < V - 1) |
| 73 | + { |
| 74 | + Edge next_edge = new Edge(); |
| 75 | + next_edge = edge[i++]; |
| 76 | + |
| 77 | + int x = find(subsets, next_edge.src); |
| 78 | + int y = find(subsets, next_edge.dest); |
| 79 | + if (x != y) |
| 80 | + { |
| 81 | + result[e++] = next_edge; |
| 82 | + Union(subsets, x, y); |
| 83 | + } |
| 84 | + } |
| 85 | + for (i = 0; i < e; ++i) |
| 86 | + System.out.println(result[i].src+" " + |
| 87 | + result[i].dest+" == " + result[i].weight); |
| 88 | + } |
| 89 | + public static void main (String[] args) |
| 90 | + { |
| 91 | + int V = 4; |
| 92 | + int E = 5; |
| 93 | + Graph graph = new Graph(V, E); |
| 94 | + graph.edge[0].src = 0; |
| 95 | + graph.edge[0].dest = 1; |
| 96 | + graph.edge[0].weight = 10; |
| 97 | + |
| 98 | + graph.edge[1].src = 0; |
| 99 | + graph.edge[1].dest = 2; |
| 100 | + graph.edge[1].weight = 6; |
| 101 | + |
| 102 | + graph.edge[2].src = 0; |
| 103 | + graph.edge[2].dest = 3; |
| 104 | + graph.edge[2].weight = 5; |
| 105 | + |
| 106 | + graph.edge[3].src = 1; |
| 107 | + graph.edge[3].dest = 3; |
| 108 | + graph.edge[3].weight = 15; |
| 109 | + |
| 110 | + graph.KruskalMST(); |
| 111 | + } |
| 112 | +} |
0 commit comments