import java.util.*;
class Graph {
public static List<Edge> getKruskalMST(List<Edge> edges, int numNodes) {
// Create a disjoint "parentNodes" set to keep track of connected components
int[] parentNodes = new int[numNodes];
for (int i = 0; i < numNodes; i++) {
// Initialize each node is a parent of its own
parentNodes[i] = i;
}
edges.sort(Comparator.comparingInt(e -> e.weight));
// Initialize a list with edges, that create MST
List<Edge> mstEdges = new ArrayList<>();
for (Edge edge : edges) {
// Search the parent of the source and destination node of the edge
int sourceParentNode = findParentNode(edge.source, parentNodes);
int destinationParentNode = findParentNode(edge.destination, parentNodes);
if (sourceParentNode != destinationParentNode) {
// !!! Do not modify the code above !!!
// Write one line of code here
// !!! Do not modify the code below !!!
// Merge two nodes to one tree
// Set the parent of the first node to the parent of the second node
// On each step we move our tree main parent node
parentNodes[sourceParentNode] = destinationParentNode;
}
}
return mstEdges;