classKrushkalMST{ staticclassEdge{ int source; int destination; int weight;
publicEdge(int source, int destination, int weight){ this.source = source; this.destination = destination; this.weight = weight; } }
staticclassGraph{ int vertices; ArrayList<Edge> allEdges = new ArrayList<>();
Graph(int vertices) { this.vertices = vertices; }
publicvoidaddEgde(int source, int destination, int weight){ Edge edge = new Edge(source, destination, weight); allEdges.add(edge); //add to total edges } publicvoidkruskalMST(){ // PriorityQueue<Edge> pq = new PriorityQueue<>(allEdges.size(), (o1,o2)->o1.weight-o2.weight); PriorityQueue<Edge> pq = new PriorityQueue<>(allEdges.size(), Comparator.comparingInt(o -> o.weight));
//add all the edges to priority queue, //sort the edges on weights for (int i = 0; i <allEdges.size() ; i++) { pq.add(allEdges.get(i)); }
//create a parent [] int [] parent = newint[vertices]; //makeset makeSet(parent);
ArrayList<Edge> mst = new ArrayList<>();
//process vertices - 1 edges int index = 0; while(index<vertices-1){ Edge edge = pq.remove(); //check if adding this edge creates a cycle int x_set = find(parent, edge.source); int y_set = find(parent, edge.destination);
if(x_set==y_set){ //ignore, will create cycle }else { //add it to our final result mst.add(edge); index++; union(parent,x_set,y_set); } } //print MST System.out.println("Minimum Spanning Tree: "); printGraph(mst); }
publicvoidmakeSet(int [] parent){ //Make set- creating a new element with a parent pointer to itself. for (int i = 0; i <vertices ; i++) { parent[i] = i; } }
publicintfind(int [] parent, int vertex){ //chain of parent pointers from x upwards through the tree // until an element is reached whose parent is itself if(parent[vertex]!=vertex) return find(parent, parent[vertex]); return vertex; }
publicvoidunion(int [] parent, int x, int y){ int x_set_parent = find(parent, x); int y_set_parent = find(parent, y); //make x as parent of y parent[y_set_parent] = x_set_parent; }