| 3 | import java.util.*; |
| 4 | |
| 5 | class Graph { |
| 6 | class Edge implements Comparable<Edge> { |
| 7 | int src, dest, weight; |
| 8 | |
| 9 | public int compareTo(Edge compareEdge) { |
| 10 | return this.weight - compareEdge.weight; |
| 11 | } |
| 12 | }; |
| 13 | |
| 14 | // Union |
| 15 | class subset { |
| 16 | int parent, rank; |
| 17 | }; |
| 18 | |
| 19 | int vertices, edges; |
| 20 | Edge edge[]; |
| 21 | |
| 22 | // Graph creation |
| 23 | Graph(int v, int e) { |
| 24 | vertices = v; |
| 25 | edges = e; |
| 26 | edge = new Edge[edges]; |
| 27 | for (int i = 0; i < e; ++i) |
| 28 | edge[i] = new Edge(); |
| 29 | } |
| 30 | |
| 31 | int find(subset subsets[], int i) { |
| 32 | if (subsets[i].parent != i) |
| 33 | subsets[i].parent = find(subsets, subsets[i].parent); |
| 34 | return subsets[i].parent; |
| 35 | } |
| 36 | |
| 37 | void Union(subset subsets[], int x, int y) { |
| 38 | int xroot = find(subsets, x); |
| 39 | int yroot = find(subsets, y); |
| 40 | |
| 41 | if (subsets[xroot].rank < subsets[yroot].rank) |
| 42 | subsets[xroot].parent = yroot; |
| 43 | else if (subsets[xroot].rank > subsets[yroot].rank) |
| 44 | subsets[yroot].parent = xroot; |
| 45 | else { |
| 46 | subsets[yroot].parent = xroot; |
| 47 | subsets[xroot].rank++; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Applying Krushkal Algorithm |
| 52 | void KruskalAlgo() { |
| 53 | Edge result[] = new Edge[vertices]; |
| 54 | int e = 0; |
| 55 | int i = 0; |
| 56 | for (i = 0; i < vertices; ++i) |
| 57 | result[i] = new Edge(); |
| 58 | |
| 59 | // Sorting the edges |
| 60 | Arrays.sort(edge); |
| 61 | subset subsets[] = new subset[vertices]; |
| 62 | for (i = 0; i < vertices; ++i) |
nothing calls this directly
no outgoing calls
no test coverage detected