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 Disjoint Set Union (DSU) or Union-Find data structure in Kruskal's Algorithm?
DSU 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) mainly due to sorting edges, where E is the number of edges. Union-Find operations are almost O(1) amortized.
Click to reveal answer
beginner
In Kruskal's Algorithm, what happens if adding an edge creates a cycle?
If adding an edge creates a cycle, the edge is skipped and not added to the MST to keep the tree acyclic.
Click to reveal answer
What is the first step in Kruskal's Algorithm?
APick any vertex as the starting point
BSort all edges by their weight
CAdd all edges to the MST
DRemove all cycles from the graph
✗ Incorrect
Kruskal's Algorithm starts by sorting all edges from smallest to largest weight.
Which data structure is commonly used to detect cycles in Kruskal's Algorithm?
ADisjoint Set Union (Union-Find)
BQueue
CStack
DBinary Search Tree
✗ Incorrect
Disjoint Set Union helps efficiently check if two vertices belong to the same connected component, detecting cycles.
What does Kruskal's Algorithm guarantee about the resulting spanning tree?
AIt has the minimum total edge weight
BIt contains all edges of the graph
CIt has the maximum total edge weight
DIt is always a complete graph
✗ Incorrect
Kruskal's Algorithm finds a spanning tree with the minimum total edge weight.
If the graph has V vertices, how many edges will the MST have?
AV
B2 * V
CV - 1
DV + 1
✗ Incorrect
A spanning tree always has exactly V - 1 edges.
What happens if two edges have the same weight in Kruskal's Algorithm?
AOnly the first edge is chosen
BAlgorithm fails
CEdges are ignored
DAny of the edges can be chosen without affecting correctness
✗ Incorrect
Edges with the same weight can be chosen in any order; MST correctness is maintained.
Explain step-by-step how Kruskal's Algorithm builds a Minimum Spanning Tree.
Think about sorting edges and using DSU to avoid cycles.
You got /5 concepts.
Describe how the Disjoint Set Union (Union-Find) data structure helps in Kruskal's Algorithm.
Focus on how DSU manages groups of connected vertices.
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 minimum spanning tree of a connected weighted graph
B. To find the shortest path between two nodes
C. To detect cycles in a graph
D. To sort the vertices of a graph
Solution
Step 1: Understand the purpose of Kruskal's Algorithm
Kruskal's Algorithm is designed to find a subset of edges that connects all vertices with the minimum total edge weight and no cycles.
Step 2: Compare with other graph algorithms
Unlike shortest path or cycle detection, Kruskal's specifically builds a minimum spanning tree.
Final Answer:
To find the minimum spanning tree of a connected weighted graph -> Option A
Quick Check:
Minimum Spanning Tree = Find minimum edges connecting all nodes [OK]
Hint: Kruskal connects all nodes with least total edge weight [OK]
Common Mistakes:
Confusing MST with shortest path
Thinking it detects cycles only
Assuming it sorts vertices
2. Which of the following is the correct way to sort edges by weight in C++ for Kruskal's Algorithm?
easy
A. std::sort(edges.begin(), edges.end(), [](auto &a, auto &b){ return a.weight < b.weight; });
B. std::sort(edges, edges + n, [](auto a, auto b){ return a.weight > b.weight; });
C. sort(edges.begin(), edges.end(), compareEdges); // where compareEdges returns a.weight > b.weight
D. edges.sort([](auto a, auto b){ return a.weight < b.weight; });
Solution
Step 1: Identify correct sorting syntax for vector of edges
Using std::sort with iterators edges.begin() and edges.end() is correct for vectors.
Step 2: Check lambda comparator correctness
The comparator should return true if first edge weight is less than second for ascending order.
Final Answer:
std::sort(edges.begin(), edges.end(), [](auto &a, auto &b){ return a.weight < b.weight; }); -> Option A
Quick Check:
Sort edges ascending by weight using std::sort with correct lambda [OK]
Hint: Use std::sort with begin/end and lambda returning a.weight < b.weight [OK]
Common Mistakes:
Using wrong iterator syntax for vector
Reversing comparator logic
Calling sort on vector object directly
3. Given the following edges of a graph: edges = { {1, 2, 3}, {2, 3, 1}, {1, 3, 2} } where each tuple is (u, v, weight). What is the total weight of the MST found by Kruskal's Algorithm?
medium
A. 4
B. 5
C. 3
D. 6
Solution
Step 1: Sort edges by weight
Sorted edges by weight: (2,3,1), (1,3,2), (1,2,3)
Step 2: Pick edges without forming cycles
Pick (2,3,1), then (1,3,2). Adding (1,2,3) forms a cycle, so skip.
Final Answer:
1 + 2 = 3 -> Option C
Quick Check:
Sum of selected edges = 1 + 2 = 3 [OK]
Hint: Pick edges from smallest weight, skip if cycle forms [OK]
Common Mistakes:
Adding all edges without checking cycles
Choosing edges in wrong order
Misreading edge weights
4. Consider this snippet for Kruskal's Algorithm union-find step:
What is the likely error if the MST weight is incorrect?
medium
A. Union operation is called before checking if sets are different
B. The find function does not use path compression
C. The unionSet function does not update parent correctly
D. Edges are not sorted by weight before processing
Solution
Step 1: Analyze union-find usage
The code correctly checks if u and v belong to different sets before union and adding weight.
Step 2: Check prerequisites for Kruskal's correctness
Kruskal requires edges sorted by weight ascending before processing; missing this causes incorrect MST weight.
Final Answer:
Edges are not sorted by weight before processing -> Option D
Quick Check:
Sorting edges first is essential for correct MST [OK]
Hint: Always sort edges by weight before union-find steps [OK]
Common Mistakes:
Ignoring edge sorting
Assuming union-find alone fixes MST
Confusing path compression with correctness
5. You have a graph with 5 nodes and edges: {(1,2,2), (1,3,3), (2,3,1), (2,4,4), (3,5,5), (4,5,7)} Using Kruskal's Algorithm, which edges will be included in the MST?