A Minimum Spanning Tree is a subset of edges in a connected, weighted graph that connects all vertices together without any cycles and with the minimum possible total edge weight.
Click to reveal answer
beginner
What is the main idea behind Kruskal's Algorithm?
Kruskal's Algorithm builds the MST by sorting all edges by weight and adding the smallest edge to the MST if it doesn't create a cycle, repeating until all vertices are connected.
Click to reveal answer
intermediate
Why do we use a Union-Find (Disjoint Set) data structure in Kruskal's Algorithm?
Union-Find helps efficiently check if adding an edge creates a cycle by tracking which vertices are in the same connected component, allowing quick union and find operations.
Click to reveal answer
intermediate
What is the time complexity of Kruskal's Algorithm?
The time complexity is O(E log E), where E is the number of edges, mainly due to sorting edges. Union-Find operations are almost constant time.
Click to reveal answer
intermediate
What happens if the graph is not connected when running Kruskal's Algorithm?
Kruskal's Algorithm will produce a Minimum Spanning Forest, which is a set of MSTs for each connected component, since it cannot connect disconnected parts.
Click to reveal answer
What is the first step in Kruskal's Algorithm?
AAdd all edges to the MST
BPick any vertex as the starting point
CSort all edges by their weight
DRemove cycles from the graph
✗ Incorrect
Kruskal's Algorithm starts by sorting all edges from smallest to largest weight.
How does Kruskal's Algorithm avoid cycles when adding edges?
ABy checking if the edge connects two different sets using Union-Find
BBy only adding edges connected to the starting vertex
CBy removing the heaviest edges first
DBy adding edges randomly
✗ Incorrect
It uses Union-Find to check if two vertices belong to different sets before adding an edge.
What data structure is commonly used to efficiently manage connected components in Kruskal's Algorithm?
AUnion-Find (Disjoint Set)
BStack
CQueue
DBinary Search Tree
✗ Incorrect
Union-Find helps quickly find and union sets to avoid cycles.
If a graph has 5 vertices and 7 edges, what is the maximum number of edges in its MST?
A6
B5
C7
D4
✗ Incorrect
An MST for a graph with V vertices has exactly V-1 edges, so 4 edges here.
What does Kruskal's Algorithm produce if the graph is disconnected?
AMinimum Spanning Tree
BMinimum Spanning Forest
CNo output
DCycle-free graph
✗ Incorrect
It produces a Minimum Spanning Forest, MSTs for each connected component.
Explain step-by-step how Kruskal's Algorithm builds a Minimum Spanning Tree.
Think about sorting edges and using Union-Find to avoid cycles.
You got /5 concepts.
Describe how the Union-Find data structure helps in Kruskal's Algorithm.
Focus on cycle detection and set management.
You got /4 concepts.
Practice
(1/5)
1. What is the main goal of Kruskal's algorithm in graph theory?
easy
A. To find the shortest path between two nodes
B. To find the minimum spanning tree of a graph
C. To detect cycles in a graph
D. To find the maximum flow in a network
Solution
Step 1: Understand Kruskal's algorithm purpose
Kruskal's algorithm is designed to find a subset of edges that connects all vertices with the minimum total edge weight.
Step 2: Compare with other graph problems
Shortest path, cycle detection, and max flow are different problems with different algorithms.
Final Answer:
To find the minimum spanning tree of a graph -> Option B
Quick Check:
Kruskal's algorithm = Minimum Spanning Tree [OK]
Hint: Kruskal always builds minimum spanning tree by edge weights [OK]
Common Mistakes:
Confusing MST with shortest path
Thinking it detects cycles only
Mixing with max flow problems
2. Which of the following is the correct syntax to sort edges by weight in Python for Kruskal's algorithm?
easy
A. edges.sort(key=lambda x: x[2])
B. edges.sort(key=lambda x: x[1])
C. edges.sort()
D. edges.sort(key=lambda x: x[0])
Solution
Step 1: Identify edge structure
Edges are usually stored as tuples (node1, node2, weight), so weight is at index 2.
Step 2: Use sort with key on weight
Sorting by weight means sorting by the third element, so key=lambda x: x[2] is correct.
Final Answer:
edges.sort(key=lambda x: x[2]) -> Option A
Quick Check:
Sort edges by weight = key=lambda x: x[2] [OK]
Hint: Weight is usually third element in edge tuple [OK]
Common Mistakes:
Sorting by wrong tuple index
Using sort() without key when edges are tuples
Confusing node indices with weight index
3. What is the output of the following code snippet that applies Kruskal's algorithm on a graph with edges [(0, 1, 10), (0, 2, 6), (0, 3, 5), (1, 3, 15), (2, 3, 4)]?
edges = [(0, 1, 10), (0, 2, 6), (0, 3, 5), (1, 3, 15), (2, 3, 4)]
edges.sort(key=lambda x: x[2])
parent = list(range(4))
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:
parent[rootB] = rootA
return True
return False
mst = []
for u, v, w in edges:
if union(u, v):
mst.append((u, v, w))
print(mst)
Step 2: Apply union-find to select edges without cycles
Pick (2,3,4), union success; pick (0,3,5), union success; pick (0,2,6), union fails (cycle); pick (0,1,10), union success; stop as MST has 3 edges for 4 nodes.
Final Answer:
[(2, 3, 4), (0, 3, 5), (0, 1, 10)] -> Option D
Quick Check:
Edges chosen = minimum weights without cycles [OK]
Hint: Sort edges, pick smallest without cycle until MST complete [OK]
Common Mistakes:
Not sorting edges correctly
Adding edges that form cycles
Stopping too early or too late
4. Identify the bug in this Kruskal's algorithm snippet and select the fix:
edges = [(0,1,1), (1,2,2), (0,2,3)]
parent = [0, 1, 2]
def find(x):
if parent[x] == x:
return x
return find(parent[x])
def union(a, b):
rootA = find(a)
rootB = find(b)
if rootA != rootB:
parent[a] = b # Bug here
return True
return False
mst = []
for u,v,w in sorted(edges, key=lambda x: x[2]):
if union(u,v):
mst.append((u,v,w))
print(mst)
medium
A. Change 'parent[a] = b' to 'parent[rootB] = rootA'
B. Change 'parent[a] = b' to 'parent[rootA] = rootB'
C. Change 'parent[a] = b' to 'parent[b] = a'
D. No bug, code is correct
Solution
Step 1: Analyze union function
Union should link roots of sets, not original nodes directly.
Step 2: Fix parent assignment
Assign parent of rootB to rootA to merge sets correctly: 'parent[rootB] = rootA'.
Final Answer:
Change 'parent[a] = b' to 'parent[rootB] = rootA' -> Option A
Quick Check:
Union sets by root parents, not nodes [OK]
Hint: Always union by root parents, not direct nodes [OK]
Common Mistakes:
Assigning parent of node instead of root
Confusing rootA and rootB in assignment
Skipping path compression in find
5. Given a graph with vertices 0 to 4 and edges [(0,1,2), (0,3,6), (1,2,3), (1,3,8), (1,4,5), (2,4,7), (3,4,9)], what is the total weight of the minimum spanning tree found by Kruskal's algorithm?