Critical and Pseudo-Critical Edges in MST
You are given a weighted, undirected, and connected graph with n vertices numbered from 0 to n - 1. You are also given an array edges where edges[i] = [u, v, weight] represents a bidirectional edge between nodes u and v.
A Minimum Spanning Tree (MST) is a subset of the graph's edges that connects all vertices without cycles and with the minimum possible total edge weight.
Find all the critical and pseudo-critical edges in the given graph's MST:
- Critical Edge: An edge whose deletion from the graph causes the total MST weight to strictly increase (or breaks the graph's connectivity). It appears in all possible MSTs.
- Pseudo-Critical Edge: An edge that can appear in some valid MSTs, but not all of them.
Return a 2D array of exactly two lists: [critical_edges, pseudo_critical_edges]. To guarantee a deterministic output, both lists must contain the original indices of the edges, sorted in strictly ascending order.
Example 1:
Input: n = 2, edges = [ [0,1,5] ]
Output: [ [0], [ ] ]
Explanation:
There are only 2 nodes and 1 edge connecting them. We must use this edge to keep the graph connected. If we drop it, we can't form a spanning tree at all. Therefore, it is absolutely critical.
Example 2:
Input: n = 5, edges = [ [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] ]
Explanation:
The base MST weight is 7. Edges 0 and 1 (weight 1) must be used in every single valid MST to keep the weight at 7, making them critical. Edges 2, 3, 4, and 5 can be swapped around to form different MSTs that all equal 7, making them pseudo-critical. Edge 6 (weight 6) is completely useless.
Now Your Turn!
Pick the correct output for the given inputInput: n = 3, edges = [ [0,1,1], [1,2,1], [0,2,1] ]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 2 <= n <= 100
- 1 <= edges.length <= min(200, n * (n - 1) / 2)
- edges[i].length == 3
- 0 <= u < v < n
- 1 <= weight <= 1000
- All pairs (u, v) are distinct.