Bird
Raised Fist0
Data Structures Theoryknowledge~10 mins

Minimum spanning tree (Kruskal's) in Data Structures Theory - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select the next edge with the smallest weight in Kruskal's algorithm.

Data Structures Theory
edges.sort(key=lambda edge: edge.[1])
Drag options to blanks, or click blank then click option'
Aweight
Bcost
Clength
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' or 'cost' instead of 'weight' which is the standard term.
Trying to sort by 'value' which is not a property of edges.
2fill in blank
medium

Complete the code to check if adding an edge creates a cycle using the union-find data structure.

Data Structures Theory
if find(parent, u) != [1](parent, v):
Drag options to blanks, or click blank then click option'
Amerge
Bunion
Cconnect
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'union' instead of 'find' to check parents.
Using unrelated functions like 'connect' or 'merge'.
3fill in blank
hard

Fix the error in the union operation to correctly merge two sets in Kruskal's algorithm.

Data Structures Theory
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
Drag options to blanks, or click blank then click option'
Au
Broot_u
Croot_v
Dv
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning parent[root_u] = root_u which causes a cycle.
Using the original vertices u or v instead of their roots.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each vertex to its parent after union-find operations.

Data Structures Theory
parent_map = {vertex: [1](parent, vertex) for vertex in [2]
Drag options to blanks, or click blank then click option'
Afind
Bvertices
Cnodes
Dget_parent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get_parent' which is not a standard function.
Using 'nodes' instead of the variable that holds all vertices.
5fill in blank
hard

Fill all three blanks to complete the Kruskal's algorithm loop that adds edges to the MST.

Data Structures Theory
for edge in [1]:
    u, v, weight = edge
    if find(parent, u) != find(parent, v):
        [2](parent, rank, u, v)
        mst_edges.append([3])
Drag options to blanks, or click blank then click option'
Aedges
Bunion
Cedge
Dgraph
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'graph' instead of 'edges' to iterate.
Appending 'u' or 'v' instead of the whole edge.

Practice

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

Solution

  1. Step 1: Understand the purpose of Kruskal's algorithm

    Kruskal's algorithm is designed to connect all points in a graph with the smallest total edge weight without forming any loops.
  2. Step 2: Compare with other graph problems

    Unlike shortest path or cycle detection alone, Kruskal's focuses on building a minimum spanning tree, which is a tree connecting all vertices with minimal cost.
  3. Final Answer:

    To find the minimum spanning tree connecting all vertices without cycles -> Option C
  4. Quick Check:

    Kruskal's goal = minimum spanning tree [OK]
Hint: Kruskal's connects all points cheaply without loops [OK]
Common Mistakes:
  • Confusing minimum spanning tree with shortest path
  • Thinking it detects cycles only
  • Assuming it sorts vertices instead of edges
2. Which of the following is the correct first step in Kruskal's algorithm?
easy
A. Pick any vertex and start a depth-first search
B. Sort all edges in increasing order of their weights
C. Remove all vertices with degree 1
D. Sort vertices by their labels alphabetically

Solution

  1. Step 1: Identify the initial step of Kruskal's algorithm

    Kruskal's algorithm begins by sorting all edges by their weight from smallest to largest to ensure the cheapest edges are considered first.
  2. Step 2: Eliminate incorrect options

    Depth-first search and vertex removal are not part of Kruskal's initial steps, and sorting vertices alphabetically is irrelevant.
  3. Final Answer:

    Sort all edges in increasing order of their weights -> Option B
  4. Quick Check:

    First step = sort edges by weight [OK]
Hint: Always start by sorting edges by weight ascending [OK]
Common Mistakes:
  • Sorting vertices instead of edges
  • Starting with a search instead of sorting
  • Removing vertices prematurely
3. Given the edges with weights: (A-B: 1), (B-C: 4), (A-C: 3), (C-D: 2), what edges will Kruskal's algorithm select for the minimum spanning tree?
medium
A. (A-C), (B-C), (C-D)
B. (A-B), (C-D), (B-C)
C. (A-B), (C-D), (A-C), (B-C)
D. (A-B), (C-D), (A-C)

Solution

  1. Step 1: Sort edges by weight

    Edges sorted: (A-B:1), (C-D:2), (A-C:3), (B-C:4).
  2. Step 2: Add edges without creating cycles

    Add (A-B), then (C-D). Adding (A-C) connects A and C without cycle. Adding (B-C) would create a cycle, so skip it.
  3. Final Answer:

    (A-B), (C-D), (A-C) -> Option D
  4. Quick Check:

    Edges chosen = smallest weights without cycles [OK]
Hint: Pick edges from smallest weight, skip if cycle forms [OK]
Common Mistakes:
  • Including all edges regardless of cycles
  • Choosing heavier edges before lighter ones
  • Not checking for cycles before adding edges
4. In Kruskal's algorithm, what is the common error if the algorithm adds an edge that creates a cycle?
medium
A. The disjoint set structure is not used or updated correctly
B. Edges are not sorted by weight
C. Vertices are not labeled properly
D. The graph is directed instead of undirected

Solution

  1. Step 1: Understand cycle prevention in Kruskal's

    Kruskal's algorithm uses a disjoint set (union-find) data structure to track connected components and avoid cycles.
  2. Step 2: Identify the cause of cycle creation

    If an edge creates a cycle, it means the disjoint set was not used or updated properly to detect that the two vertices are already connected.
  3. Final Answer:

    The disjoint set structure is not used or updated correctly -> Option A
  4. Quick Check:

    Cycle error = faulty disjoint set use [OK]
Hint: Use disjoint sets to detect cycles before adding edges [OK]
Common Mistakes:
  • Ignoring disjoint set updates
  • Assuming sorting fixes cycles
  • Confusing graph direction with cycle detection
5. You have a graph with edges: (X-Y: 2), (Y-Z: 2), (X-Z: 3), (Z-W: 1), (W-X: 4). Using Kruskal's algorithm, which set of edges forms the minimum spanning tree?
hard
A. (Z-W), (X-Y), (Y-Z)
B. (X-Y), (Y-Z), (X-Z)
C. (Z-W), (X-Z), (W-X)
D. (Z-W), (X-Y), (Y-Z), (W-X)

Solution

  1. Step 1: Sort edges by weight

    Edges sorted: (Z-W:1), (X-Y:2), (Y-Z:2), (X-Z:3), (W-X:4).
  2. Step 2: Add edges without cycles until all vertices connected

    Add (Z-W), then (X-Y), then (Y-Z). Now all vertices X, Y, Z, W are connected. Adding (W-X) or (X-Z) would create cycles, so skip them.
  3. Final Answer:

    (Z-W), (X-Y), (Y-Z) -> Option A
  4. Quick Check:

    Minimum spanning tree = edges with smallest weights connecting all nodes [OK]
Hint: Add smallest edges until all vertices connected, avoid cycles [OK]
Common Mistakes:
  • Adding extra edges causing cycles
  • Skipping necessary edges to connect all vertices
  • Choosing heavier edges before lighter ones
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