Bird
Raised Fist0
DSA Javaprogramming~10 mins

Minimum Spanning Tree Kruskal's Algorithm in DSA Java - Interactive Practice

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

Complete the code to declare the Edge class with weight attribute.

DSA Java
class Edge implements Comparable<Edge> {
    int src, dest, weight;
    Edge(int s, int d, int w) {
        src = s;
        dest = d;
        [1] = w;
    }

    public int compareTo(Edge other) {
        return this.weight - other.weight;
    }
}
Drag options to blanks, or click blank then click option'
Adest
Bw
Cweight
Dsrc
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to the wrong variable like 'dest = w;'
Using the parameter name instead of the instance variable
2fill in blank
medium

Complete the code to initialize the parent array for union-find.

DSA Java
int[] parent = new int[V];
for (int i = 0; i < V; i++) {
    parent[i] = [1];
}
Drag options to blanks, or click blank then click option'
Ai
B-1
C0
DV
Attempts:
3 left
💡 Hint
Common Mistakes
Setting all parents to i or 0 which breaks union-find logic
3fill in blank
hard

Fix the error in the find method to correctly find the root parent.

DSA Java
int find(int[] parent, int i) {
    if (parent[i] == [1]) {
        return i;
    }
    return find(parent, parent[i]);
}
Drag options to blanks, or click blank then click option'
A-1
Bi
Cparent[i]
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Checking parent[i] == i which is incorrect for this union-find implementation
4fill in blank
hard

Fill both blanks to correctly perform union operation.

DSA Java
void union(int[] parent, int x, int y) {
    int xset = find(parent, x);
    int yset = find(parent, y);
    if (xset != yset) {
        parent[[1]] = [2];
    }
}
Drag options to blanks, or click blank then click option'
Axset
Byset
Cx
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Using x or y instead of root parents xset or yset
Assigning parent[yset] = xset instead of parent[xset] = yset (both valid but must be consistent)
5fill in blank
hard

Fill all three blanks to complete the Kruskal's algorithm loop for MST.

DSA Java
int e = 0; // count of edges in MST
int i = 0; // index for sorted edges
while (e < V - 1 && i < edges.length) {
    Edge nextEdge = edges[[1]];
    int x = find(parent, nextEdge.src);
    int y = find(parent, nextEdge.dest);
    if (x != y) {
        union(parent, x, y);
        mstEdges[[2]] = nextEdge;
        e[3];
    }
    i++;
}
Drag options to blanks, or click blank then click option'
Ai
Be
C++
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices for arrays
Decrementing e instead of incrementing

Practice

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

    Shortest path and cycle detection are different problems solved by other algorithms like Dijkstra or DFS.
  3. Final Answer:

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

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

Solution

  1. Step 1: Identify the need for set operations

    Kruskal's algorithm requires checking if two vertices belong to the same set and merging sets without cycles.
  2. Step 2: Match data structure to operations

    Disjoint Set (Union-Find) supports fast union and find operations, ideal for this purpose.
  3. Final Answer:

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

    Union-Find = fast set checks and merges [OK]
Hint: Use Union-Find to detect cycles fast in Kruskal [OK]
Common Mistakes:
  • Using ArrayList for set operations
  • Confusing PriorityQueue with Union-Find
  • Using HashMap which is not efficient here
3. What is the output MST edges after running Kruskal's algorithm on this graph?
Vertices: 1, 2, 3, 4
Edges with weights:
1-2: 1
1-3: 3
2-3: 2
3-4: 4
2-4: 5
medium
A. [1-2, 2-3, 3-4]
B. [1-3, 2-3, 3-4]
C. [1-2, 1-3, 2-4]
D. [1-2, 2-4, 3-4]

Solution

  1. Step 1: Sort edges by weight

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

    Pick 1-2, then 2-3 (no cycle), then 1-3 creates cycle so skip, pick 3-4 (no cycle), skip 2-4 (cycle)
  3. Final Answer:

    [1-2, 2-3, 3-4] -> Option A
  4. Quick Check:

    Edges chosen by increasing weight without cycles [OK]
Hint: Sort edges by weight, pick smallest without cycle [OK]
Common Mistakes:
  • Picking edges that form cycles
  • Not sorting edges by weight
  • Choosing heavier edges first
4. Identify the error in this Java snippet for Kruskal's algorithm:
for (Edge e : edges) {
  if (findSet(e.u) != findSet(e.v)) {
    union(e.u, e.v);
    mst.add(e);
  }
}
Collections.sort(edges);
medium
A. Union should be called before findSet
B. Edges should be sorted before the loop
C. mst.add(e) should be outside the if condition
D. The loop should iterate over mst, not edges

Solution

  1. Step 1: Check order of sorting and iteration

    Kruskal's requires edges sorted by weight before processing to pick smallest edges first.
  2. Step 2: Identify mistake in code order

    Sorting is done after the loop, so edges are not processed in correct order.
  3. Final Answer:

    Edges should be sorted before the loop -> Option B
  4. Quick Check:

    Sort edges first, then pick edges [OK]
Hint: Sort edges before loop to pick smallest first [OK]
Common Mistakes:
  • Sorting edges after processing
  • Calling union before checking sets
  • Adding edges outside condition
5. Given a graph with edges:
Edges: (A-B, 4), (A-C, 3), (B-C, 2), (B-D, 5), (C-D, 7), (C-E, 8), (D-E, 6)

Using Kruskal's algorithm, what is the total weight of the minimum spanning tree?
hard
A. 18
B. 14
C. 20
D. 16

Solution

  1. Step 1: Sort edges by weight

    Sorted edges: B-C(2), A-C(3), A-B(4), B-D(5), D-E(6), C-D(7), C-E(8)
  2. Step 2: Pick edges without cycles until all vertices connected

    Pick B-C(2), A-C(3), A-B(4) forms cycle so skip, pick B-D(5), pick D-E(6). Total edges chosen: B-C, A-C, B-D, D-E.
  3. Step 3: Calculate total weight

    2 + 3 + 5 + 6 = 16. But A-B(4) skipped due to cycle, so MST edges are B-C, A-C, B-D, D-E.
  4. Step 4: Re-check cycle with A-B(4)

    Actually, A-B(4) creates cycle with A-C and B-C, so skip it. MST weight is 2+3+5+6=16.
  5. Final Answer:

    16 -> Option D
  6. Quick Check:

    Sum of chosen edges weights = 16 [OK]
Hint: Sort edges, pick smallest without cycle, sum weights [OK]
Common Mistakes:
  • Including edges that form cycles
  • Misordering edges by weight
  • Adding all edges without checking cycles
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