Challenge - 5 Problems
Kruskal MST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kruskal's MST edges
What is the output list of edges included in the MST after running Kruskal's algorithm on the given graph?
DSA Python
edges = [(1, 2, 4), (1, 3, 1), (2, 3, 3), (2, 4, 2), (3, 4, 5)] parent = {} rank = {} def find(x): while parent[x] != x: x = parent[x] return x def union(a, b): rootA = find(a) rootB = find(b) if rootA != rootB: if rank[rootA] < rank[rootB]: parent[rootA] = rootB elif rank[rootA] > rank[rootB]: parent[rootB] = rootA else: parent[rootB] = rootA rank[rootA] += 1 return True return False nodes = {1, 2, 3, 4} for node in nodes: parent[node] = node rank[node] = 0 edges.sort(key=lambda x: x[2]) mst = [] for u, v, w in edges: if union(u, v): mst.append((u, v, w)) print(mst)
Attempts:
2 left
💡 Hint
Sort edges by weight and pick edges that don't form a cycle.
✗ Incorrect
Kruskal's algorithm sorts edges by weight and adds edges that connect different sets. The MST edges are those with weights 1, 2, and 3 connecting nodes (1,3), (2,4), and (2,3).
🧠 Conceptual
intermediateNumber of edges in MST for a connected graph
For a connected graph with 7 nodes, how many edges will the Minimum Spanning Tree (MST) contain?
Attempts:
2 left
💡 Hint
MST connects all nodes with minimum edges to avoid cycles.
✗ Incorrect
An MST for a connected graph with n nodes always has n-1 edges.
🔧 Debug
advancedIdentify the error in union-find implementation
What error will this union-find code cause when running Kruskal's algorithm?
DSA Python
def find(x): if parent[x] != x: return find(parent[x]) return x def union(a, b): rootA = find(a) rootB = find(b) if rootA != rootB: parent[rootB] = rootA return True return False parent = {1:1, 2:2, 3:3} rank = {1:0, 2:0, 3:0} edges = [(1, 2, 1), (2, 3, 2)] for u, v, w in edges: union(u, v) print(parent)
Attempts:
2 left
💡 Hint
Check if rank is used to balance trees in union.
✗ Incorrect
The code runs without error but does not use rank to balance trees, which can cause inefficient find operations.
❓ Predict Output
advancedOutput of MST total weight calculation
What is the total weight of the MST after running Kruskal's algorithm on this graph?
DSA Python
edges = [(1, 2, 10), (1, 3, 6), (1, 4, 5), (2, 4, 15), (3, 4, 4)] parent = {} rank = {} def find(x): while parent[x] != x: x = parent[x] return x def union(a, b): rootA = find(a) rootB = find(b) if rootA != rootB: if rank[rootA] < rank[rootB]: parent[rootA] = rootB elif rank[rootA] > rank[rootB]: parent[rootB] = rootA else: parent[rootB] = rootA rank[rootA] += 1 return True return False nodes = {1, 2, 3, 4} for node in nodes: parent[node] = node rank[node] = 0 edges.sort(key=lambda x: x[2]) mst_weight = 0 for u, v, w in edges: if union(u, v): mst_weight += w print(mst_weight)
Attempts:
2 left
💡 Hint
Sum weights of edges chosen by Kruskal's algorithm.
✗ Incorrect
Edges chosen are (3,4,4), (1,4,5), and (1,2,10) totaling 19.
🧠 Conceptual
expertWhy does Kruskal's algorithm use union-find?
What is the main reason Kruskal's algorithm uses the union-find data structure?
Attempts:
2 left
💡 Hint
Think about cycle detection during edge addition.
✗ Incorrect
Union-find helps quickly determine if two nodes belong to the same connected component, preventing cycles.
