AlgoInsight

DSA problem

Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree

HardMinimum Spanning TreePattern: Minimum Spanning Treetop600AmazonGoogleMicrosoftBloombergByteDance

Given a weighted undirected connected graph with n vertices and an array edges where edges[i] = [ai, bi, weighti] represents a bidirectional and weighted edge between nodes ai and bi, find the set of critical and pseudo-critical edges for the graph's Minimum Spanning Tree (MST).

An MST edge whose removal would increase the MST weight (or disconnect the graph) is critical. An MST edge that appears in some MST but not in every MST is pseudo-critical — its removal still allows an MST of the same total weight, but it can also be the chosen edge in an alternate MST of the same weight. Return the indices of these edges in two lists.

The straightforward approach exploits a classical fact: an edge e is critical iff removing it from the graph either disconnects the graph or yields an MST with strictly greater weight than the base MST. An edge is pseudo-critical iff forcing it into the MST first still produces an MST of base weight, AND it is not critical.

So for each edge index i: compute the base MST weight W once with Kruskal's. Then for each i, recompute the MST while skipping edge i — if the result exceeds W (or the graph is disconnected), i is critical. Otherwise recompute while forcing edge i to be the first chosen edge — if the result equals W, i is pseudo-critical. With at most m edges, the total work is O(m * E α(n)), comfortably fast under the constraints.

Examples

Example 1
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: Edges 0 and 1 are in every MST (removing either disconnects the graph or raises the cost) so they are critical. Edges 2, 3, 4, 5 are pseudo-critical — each appears in some MST of the minimum total weight 7 but not in all of them.
Example 2
Input: n = 4, edges = [[0,1,1],[1,2,1],[2,3,1],[0,3,1]]
Output: [[],[0,1,2,3]]
Explanation: All four edges form a cycle of equal weight 1. Any three of them form an MST of weight 3, so no edge is critical and all four are pseudo-critical.
Example 3
Input: n = 2, edges = [[0,1,5]]
Output: [[0],[]]
Explanation: The single edge is the only way to connect the two vertices, so it is critical and there are no pseudo-critical edges.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

minimum-spanning-treegraphkruskalsunion-findsorting

Browse all DSA problems