AlgoFrames

Critical and pseudo-critical edges

Challenge · Write code · about 30 min

An edge is critical when removing it makes every MST heavier (or leaves the graph disconnected). An edge is pseudo-critical when it is in some MST but not critical.

Return [critical, pseudoCritical]: two lists of edge indices into edges. Any order inside each list is accepted.

Example 1

Input:

[5,[[0,1,1],[1,2,1],[2,3,2],[0,3,2],[0,4,3],[3,4,3],[1,4,6]]]

Output:

[[0,1],[2,3,4,5]]

Edges 0 and 1 are in every MST of weight 7. Edge 6 is in none.

Example 2

Input:

[4,[[0,1,1],[1,2,1],[2,3,1],[0,3,1]]]

Output:

[[],[0,1,2,3]]

Any three of the four equal edges form an MST.

Constraints

  • 2 ≤ n ≤ 100
  • 1 ≤ edges.length ≤ 200
  • 1 ≤ w ≤ 1000
  • The graph is connected.

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

More Kruskal's algorithm problems