|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +public class KruskalsMinimumSpanningTree { |
| 5 | + int V; |
| 6 | + int E; //Number of vertices and edges in the graph |
| 7 | + Edge[] edge; |
| 8 | + Edge[] mst; //Array of Edge holds the entire graph and mst array holds the Edges that are in the mst |
| 9 | + int[] parent; //Disjoint-set |
| 10 | + int[] size; //Size array for size of set |
| 11 | + |
| 12 | + public static class Edge implements Comparable<Edge> { |
| 13 | + //beginning vertex, ending vertex, weight of edge |
| 14 | + int bv; |
| 15 | + int ev; |
| 16 | + int cost; |
| 17 | + |
| 18 | + //Empty constructor |
| 19 | + public Edge() { |
| 20 | + //to initialize arrays later |
| 21 | + } |
| 22 | + |
| 23 | + //Full constructor |
| 24 | + public Edge(int bv, int ev, int cost) { |
| 25 | + this.bv = bv; |
| 26 | + this.ev = ev; |
| 27 | + this.cost = cost; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public int compareTo(Edge other) { |
| 32 | + return this.cost - other.cost; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public KruskalsMinimumSpanningTree(int v, int e) { |
| 37 | + V = v; |
| 38 | + E = e; |
| 39 | + |
| 40 | + edge = new Edge[e]; |
| 41 | + for (int i = 0; i < e; i++) { |
| 42 | + edge[i] = new Edge(); |
| 43 | + } |
| 44 | + |
| 45 | + mst = new Edge[v - 1]; |
| 46 | + for (int i = 0; i < v - 1; i++) { |
| 47 | + mst[i] = new Edge(); |
| 48 | + } |
| 49 | + |
| 50 | + parent = new int[v]; |
| 51 | + for (int i = 0; i < v; i++) { |
| 52 | + parent[i] = -1; |
| 53 | + } |
| 54 | + |
| 55 | + size = new int[v]; |
| 56 | + for (int i = 0; i < v; i++) { |
| 57 | + size[i] = 1; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public int find(int v) { |
| 62 | + if (parent[v] == -1) { // if -1 it is the root/parent |
| 63 | + return v; //the vertex is already the parent |
| 64 | + } else { |
| 65 | + return find(parent[v]); //if it's not the parent, keep using find to find the parent |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public void union(int bv, int ev) { |
| 70 | + int pb = find(bv); //parent of beginning vertex |
| 71 | + int pe = find(ev); //parent of beginning vertex |
| 72 | + |
| 73 | + if (size[pb] < size[pe]) { //if the size of one set is greater than the other |
| 74 | + /* |
| 75 | + * set the parent of the smaller set to the parent of the larger set, |
| 76 | + * we're attaching the ENTIRE smaller set from its parent vertex to |
| 77 | + * the parent vertex of the larger set. |
| 78 | + */ |
| 79 | + parent[pb] = pe; |
| 80 | + /* |
| 81 | + * add the size of the smaller set to the size of the |
| 82 | + * larger set (since they're 1 set now) |
| 83 | + */ |
| 84 | + size[pe] += size[pb]; |
| 85 | + } else { |
| 86 | + //same procedure as above |
| 87 | + parent[pe] = pb; |
| 88 | + size[pb] += size[pe]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public static void main(String[] args) { |
| 93 | + //Create a Scanner so we can input the information about edges |
| 94 | + Scanner sc = new Scanner(System.in); |
| 95 | + |
| 96 | + //Let's input how many vertices and edges we're given |
| 97 | + int v = sc.nextInt(); |
| 98 | + int e = sc.nextInt(); |
| 99 | + |
| 100 | + //Create a new object of your Main class, let's call it "graph" |
| 101 | + //and pass in the parameters v (vertices) and e (edges) |
| 102 | + //The constructor of the Main class will initialize all our |
| 103 | + //arrays for us. |
| 104 | + KruskalsMinimumSpanningTree graph = new KruskalsMinimumSpanningTree(v, e); |
| 105 | + |
| 106 | + //Using a for-loop, input the information about each edge |
| 107 | + for (int i = 0; i < e; i++) { |
| 108 | + int bv = sc.nextInt(); //beginning vertex |
| 109 | + int ev = sc.nextInt(); //ending vertex |
| 110 | + int cost = sc.nextInt(); |
| 111 | + //Now let's use the 2nd constructor of the Edge class |
| 112 | + //and put the above information into our Edge array |
| 113 | + graph.edge[i] = new Edge(bv, ev, cost); |
| 114 | + } |
| 115 | + |
| 116 | + //Using Arrays.sort(), we make use of the Comparable interface |
| 117 | + //we implemented in the Edge class |
| 118 | + Arrays.sort(graph.edge); |
| 119 | + |
| 120 | + //Create a count variable to keep track of the edges we've added |
| 121 | + int count = 0; |
| 122 | + //Create a for-loop to loop through all the given edges |
| 123 | + //we sorted earlier |
| 124 | + for (int i = 0; i < e; i++) { |
| 125 | + //Grab the details of the ith edge |
| 126 | + //it should be the edge with the least cost |
| 127 | + int bv = graph.edge[i].bv; |
| 128 | + int ev = graph.edge[i].ev; |
| 129 | + int cost = graph.edge[i].cost; |
| 130 | + |
| 131 | + //Using the find function we created earlier |
| 132 | + //for our disjoint-set, use it to find bv's root/parent |
| 133 | + //and ev's root/parent. Store in respective variables |
| 134 | + int pb = graph.find(bv); //parent of beginning vertex |
| 135 | + int pe = graph.find(ev); //parent of ending vertex |
| 136 | + |
| 137 | + //If the parent of bv and ev are not the same, |
| 138 | + //then the edge won't form a cycle |
| 139 | + if (pb != pe) { |
| 140 | + //Using the union function |
| 141 | + graph.union(bv, ev); |
| 142 | + |
| 143 | + //Add the edge to the MST array |
| 144 | + //Using count because not every given edge (i) |
| 145 | + //can be added to the MST |
| 146 | + graph.mst[count].bv = bv; |
| 147 | + graph.mst[count].ev = ev; |
| 148 | + graph.mst[count].cost = cost; |
| 149 | + |
| 150 | + //If the MST has V - 1 edges in it |
| 151 | + //then we have found the MST of the graph |
| 152 | + //WE'VE COMPLETED THE ALGORITHM! |
| 153 | + if (count == v - 1) { |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + for (int i = 0; i < v - 1; i++) { |
| 159 | + System.out.print(graph.mst[i].bv + " "); |
| 160 | + System.out.print(graph.mst[i].ev + " "); |
| 161 | + System.out.println(graph.mst[i].cost); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments