AlgoFrames

Spot the bug: linking the wrong vertex

Standard · Find the bug · about 8 min

This Kruskal should return the MST weight, or -1 for a graph that is not connected. It returns -1 for some connected graphs. Click the line with the bug, then fix it.

Example 1

Input:

[{"n":3,"edges":[[0,1,1],[2,1,2],[0,2,3]],"directed":false}]

Output:

3

The buggy code returns -1 here.

Constraints

  • 1 ≤ n ≤ 100
  • 0 ≤ edges.length ≤ 500

Learn the idea first: Kruskal's algorithm, a visual lesson.

More Kruskal's algorithm problems