Bird
Raised Fist0
DSA Cprogramming~20 mins

Minimum Spanning Tree Kruskal's Algorithm in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kruskal MST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Kruskal's MST edges
What edges are included in the Minimum Spanning Tree (MST) after running Kruskal's algorithm on the given graph?
DSA C
Edges with weights: (0-1:4), (0-2:3), (1-2:1), (1-3:2), (2-3:4), (3-4:2), (4-5:6)
Run Kruskal's MST and list edges in MST in order of selection.
A(0-2:3), (1-2:1), (1-3:2), (3-4:2), (4-5:6)
B(1-2:1), (0-1:4), (1-3:2), (3-4:2), (4-5:6)
C(1-3:2), (3-4:2), (0-2:3), (1-2:1), (4-5:6)
D(1-2:1), (1-3:2), (3-4:2), (0-2:3), (4-5:6)
Attempts:
2 left
💡 Hint
Sort edges by weight and pick edges that don't form cycles.
🧠 Conceptual
intermediate
1:30remaining
Why does Kruskal's algorithm use a disjoint set?
What is the main purpose of using a disjoint set (union-find) data structure in Kruskal's algorithm?
ATo find the shortest path between two vertices
BTo store the graph adjacency list
CTo quickly check if adding an edge creates a cycle
DTo sort edges by their weights efficiently
Attempts:
2 left
💡 Hint
Think about how Kruskal avoids cycles when adding edges.
🔧 Debug
advanced
2:30remaining
Identify the error in this Kruskal's MST code snippet
What error will this code cause when running Kruskal's MST? int parent[5] = {-1, -1, -1, -1, -1}; int find(int x) { if (parent[x] == -1) return x; return find(parent[x]); } void union_set(int x, int y) { int xset = find(x); int yset = find(y); parent[xset] = yset; } // In main, edges are processed and union_set called without checking cycles.
AThe code may create cycles because it unions without checking if sets are different
BStack overflow due to infinite recursion in find function
CCompilation error due to missing return type in union_set
DArray index out of bounds accessing parent array
Attempts:
2 left
💡 Hint
Check if union is done only when sets are different.
❓ Predict Output
advanced
2:00remaining
Output of MST total weight calculation
What is the total weight of the MST after running Kruskal's algorithm on this graph? Edges: (0-1:10), (0-2:6), (0-3:5), (1-3:15), (2-3:4)
A25
B19
C20
D15
Attempts:
2 left
💡 Hint
Add weights of edges selected by Kruskal's MST.
🚀 Application
expert
2:30remaining
Kruskal's algorithm on a disconnected graph
If Kruskal's algorithm is run on a graph with two disconnected components, what will be the result?
AIt will produce a Minimum Spanning Forest containing MSTs for each component
BIt will fail with an error because the graph is not connected
CIt will produce a single MST connecting all vertices including disconnected ones
DIt will only process the first component and ignore the second
Attempts:
2 left
💡 Hint
Think about how MST applies to disconnected graphs.

Practice

(1/5)
1. What is the main goal of Kruskal's algorithm in graph theory?
easy
A. To sort the vertices of a graph
B. To find the shortest path between two nodes
C. To detect cycles in a graph
D. To find the minimum spanning tree of a graph

Solution

  1. 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 and no cycles.
  2. Step 2: Compare with other graph algorithms

    Unlike shortest path or cycle detection, Kruskal's focuses on building a minimum spanning tree.
  3. Final Answer:

    To find the minimum spanning tree of a graph -> Option D
  4. Quick Check:

    Kruskal's algorithm = Minimum Spanning Tree [OK]
Hint: Kruskal's always builds minimum spanning tree by edges [OK]
Common Mistakes:
  • Confusing MST with shortest path
  • Thinking it detects cycles only
  • Assuming it sorts vertices
2. Which data structure is commonly used in Kruskal's algorithm to detect cycles efficiently?
easy
A. Disjoint Set (Union-Find)
B. Stack
C. Queue
D. Priority Queue

Solution

  1. Step 1: Identify cycle detection method in Kruskal's

    Kruskal's algorithm uses a Disjoint Set (Union-Find) data structure to check if adding an edge creates a cycle by checking if two vertices belong to the same set.
  2. Step 2: Compare other data structures

    Queue and stack do not help detect cycles efficiently here; priority queue is used for sorting edges but not cycle detection.
  3. Final Answer:

    Disjoint Set (Union-Find) -> Option A
  4. Quick Check:

    Cycle detection in Kruskal's = Union-Find [OK]
Hint: Use Union-Find to quickly check cycles in Kruskal's [OK]
Common Mistakes:
  • Confusing priority queue with cycle detection
  • Using stack or queue for cycle detection
  • Not knowing Union-Find purpose
3. Consider the following edges of a graph with weights:
Edges = [(A-B, 1), (B-C, 4), (A-C, 3), (C-D, 2)]
What is the total weight of the minimum spanning tree found by Kruskal's algorithm?
medium
A. 7
B. 6
C. 8
D. 10

Solution

  1. Step 1: Sort edges by weight

    Sorted edges: (A-B,1), (C-D,2), (A-C,3), (B-C,4)
  2. Step 2: Pick edges without cycles

    Pick (A-B,1), then (C-D,2), then (A-C,3). Adding (B-C,4) creates a cycle, so skip it.
  3. Step 3: Calculate total weight

    Total weight = 1 + 2 + 3 = 6
  4. Final Answer:

    6 -> Option B
  5. Quick Check:

    Sum of MST edges = 6 [OK]
Hint: Sort edges, pick smallest without cycle until all connected [OK]
Common Mistakes:
  • Adding all edges without checking cycles
  • Choosing edges in wrong order
  • Miscounting total weight
4. The following C code snippet is part of Kruskal's algorithm. What is the error?
int findParent(int parent[], int i) {
    if (parent[i] == i)
        return i;
    return findParent(parent, parent[i]);
}

void unionSet(int parent[], int x, int y) {
    int xset = findParent(parent, x);
    int yset = findParent(parent, y);
    parent[xset] = yset;
}
medium
A. unionSet should swap xset and yset
B. unionSet should not change parent array
C. Missing path compression in findParent
D. findParent should return void

Solution

  1. Step 1: Analyze findParent function

    findParent recursively finds the root parent but does not update parent[i] to root, missing path compression optimization.
  2. Step 2: Understand unionSet function

    unionSet correctly merges sets by updating parent of one root to another; no swap needed.
  3. Final Answer:

    Missing path compression in findParent -> Option C
  4. Quick Check:

    Path compression missing = findParent optimization [OK]
Hint: Add path compression to speed up findParent [OK]
Common Mistakes:
  • Thinking unionSet must swap parents
  • Believing findParent should return void
  • Ignoring path compression importance
5. Given a graph with vertices {1, 2, 3, 4} and edges with weights:
(1-2, 5), (1-3, 3), (2-3, 4), (2-4, 2), (3-4, 7)
Using Kruskal's algorithm, which edges will be included in the minimum spanning tree?
hard
A. (2-4), (1-3), (2-3)
B. (1-3), (2-4), (1-2)
C. (2-4), (1-3), (1-2)
D. (1-2), (2-3), (3-4)

Solution

  1. Step 1: Sort edges by weight

    Sorted edges: (2-4,2), (1-3,3), (2-3,4), (1-2,5), (3-4,7)
  2. Step 2: Pick edges without cycles

    Pick (2-4,2), then (1-3,3). Next (2-3,4) connects 2 and 3 which are in different sets, so pick it. After these three edges, all vertices are connected and MST is complete.
  3. Step 3: Verify options

    (2-4), (1-3), (2-3) matches edges (2-4), (1-3), (2-3). (2-4), (1-3), (1-2) includes (1-2) which is heavier than (2-3), so incorrect.
  4. Final Answer:

    (2-4), (1-3), (2-3) -> Option A
  5. Quick Check:

    Pick smallest edges connecting all vertices without cycle [OK]
Hint: Sort edges, pick smallest connecting new vertices until all connected [OK]
Common Mistakes:
  • Choosing heavier edges over lighter ones
  • Adding edges that create cycles
  • Not stopping after n-1 edges
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard