Complete the code to select the next edge with the smallest weight in Kruskal's algorithm.
edges.sort(key=lambda edge: edge.[1])In Kruskal's algorithm, edges are sorted by their weight to pick the smallest edge first.
Complete the code to check if adding an edge creates a cycle using the union-find data structure.
if find(parent, u) != [1](parent, v):
To check if two vertices belong to different sets, we use the find function on both.
Fix the error in the union operation to correctly merge two sets in Kruskal's algorithm.
def union(parent, rank, u, v): root_u = find(parent, u) root_v = find(parent, v) if root_u != root_v: if rank[root_u] < rank[root_v]: parent[root_u] = [1] else: parent[root_v] = root_u if rank[root_u] == rank[root_v]: rank[root_u] += 1
When the rank of root_u is less, we attach root_u's parent to root_v to keep the tree shallow.
Fill both blanks to create a dictionary comprehension that maps each vertex to its parent after union-find operations.
parent_map = {vertex: [1](parent, vertex) for vertex in [2]The dictionary maps each vertex in the set vertices to its root parent found by the find function.
Fill all three blanks to complete the Kruskal's algorithm loop that adds edges to the MST.
for edge in [1]: u, v, weight = edge if find(parent, u) != find(parent, v): [2](parent, rank, u, v) mst_edges.append([3])
The loop goes through all edges, uses union to merge sets, and adds the current edge to the MST list.