AAlgoLoopSpaced repetition for LeetCode
HARDUnion-Find (Disjoint Set)LeetCode ↗

Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree

The key idea

Compute the base MST weight once. An edge is critical if forcing its removal makes the MST heavier (or disconnects the graph); it is pseudo-critical if forcing its inclusion still yields an MST of the base weight. Test each edge with two re-runs of Kruskal.

Problem

You are given a weighted undirected connected graph with n vertices numbered from 0 to n - 1, and an array edges where edges[i] = [from_i, to_i, weight_i] represents a bidirectional and weighted edge between nodes from_i and to_i.

A minimum spanning tree (MST) is a subset of the graph edges that connects all the vertices together, without any cycles, and with the minimum possible total edge weight.

Find all the critical and pseudo-critical edges in the given graph's minimum spanning tree. An MST edge whose deletion from the graph would cause the MST weight to increase is called a critical edge. On the other hand, a pseudo-critical edge is one that can appear in some MSTs but not all.

Note that you can return the indices of the edges in any order. The answer is [critical, pseudo_critical], where the indices refer to positions in the input edges array.

Constraints

Examples

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]]
Input: n = 4, edges = [[0,1,1],[1,2,1],[2,3,1],[0,3,1]] Output: [[],[0,1,2,3]]

Complexity

Time: O(E^2 * alpha(V)) Space: O(V + E)

See the full solution

410310
Step-by-step visualization
Start free →

More Union-Find (Disjoint Set) problems