Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree Problem
Description
LeetCode Problem 1489.
Given a weighted undirected connected graph with n vertices numbered from 0 to n - 1, and an array edges where edges[i] = [a_i, b_i, weight_i] represents a bidirectional and weighted edge between nodes a_i and b_i. 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 minimum spanning tree (MST). 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 that which can appear in some MSTs but not all.
Note that you can return the indices of the edges in any order.
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: The figure above describes the graph.
The following figure shows all the possible MSTs:

Notice that the two edges 0 and 1 appear in all MSTs, therefore they are critical edges, so we return them in the first list of the output.
The edges 2, 3, 4, and 5 are only part of some MSTs, therefore they are considered pseudo-critical edges. We add them to the second list of the output.
Example 2:

Input: n = 4, edges = [[0,1,1],[1,2,1],[2,3,1],[0,3,1]]
Output: [[],[0,1,2,3]]
Explanation: We can observe that since all 4 edges have equal weight, choosing any 3 edges from the given 4 will yield an MST. Therefore all 4 edges are pseudo-critical.
Constraints:
2 <= n <= 1001 <= edges.length <= min(200, n * (n - 1) / 2)edges[i].length == 30 <= a_i < b_i < n1 <= weight_i <= 1000- All pairs
(a_i, b_i)are distinct.
Solution
Approach: Kruskal's Algorithm, Once per Edge
The graph is small (n <= 100, at most 200 edges), so we can afford to build an MST a few hundred times. First run Kruskal's algorithm once to get the minimum weight best: sort the edges by weight and add each one that joins two different components, tracked with union-find.
Then, for every edge e:
- Critical? Build an MST without
e. If the graph is no longer connected, or the weight is now larger thanbest, then every MST must containe, so it is critical. - Pseudo-critical? Otherwise, build an MST that is forced to take
efirst and then continues with Kruskal as usual. If the weight is stillbest, some MST containse, but since it is not critical, not all of them do. Soeis pseudo-critical.
An edge that fails both tests appears in no MST, and is in neither list.
The order of the checks matters: a critical edge also passes the "forced" test, so we only test pseudo-critical for edges that are not critical. Keep the original edge indices while sorting (sort a list of indices by weight), since the answer is reported by index.
Complexity: O(m2 * α(n)) time after the O(m log m) sort, where m is the number of edges (two Kruskal passes per edge), and O(n + m) extra space.
class Solution:
def findCriticalAndPseudoCriticalEdges(self, n: int, edges: List[List[int]]) -> List[List[int]]:
order = sorted(range(len(edges)), key=lambda i: edges[i][2]) # edge indices by weight
def mst_weight(skip=-1, force=-1):
"""Kruskal's weight without edge `skip` and with edge `force` taken first; inf if disconnected."""
parent = list(range(n))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
total, parts = 0, n
for i in ([force] if force >= 0 else []) + order:
if i == skip:
continue
a, b, w = edges[i]
ra, rb = find(a), find(b)
if ra != rb:
parent[ra] = rb
total += w
parts -= 1
return total if parts == 1 else float("inf")
best = mst_weight()
critical, pseudo = [], []
for i in range(len(edges)):
if mst_weight(skip=i) > best: # every MST needs this edge
critical.append(i)
elif mst_weight(force=i) == best: # some MST can use it
pseudo.append(i)
return [critical, pseudo]