HardNo. 1489

Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree — LeetCode 1489 Python Solution

Pattern
Union-Find
Topics
Union Find, Graph, Minimum Spanning Tree, Sorting, Strongly Connected Component
Reading time
7 min

The problem

Given a weighted undirected connected graph with n vertices numbered from 0 to n - 1, and an array edges where edges[i] = [ai, bi, weighti] represents a bidirectional and weighted edge between nodes ai and bi. 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.

Example

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.

Python solution

PythonSolution
class UnionFind:
    def __init__(self, n):
        self.p = list(range(n))
        self.n = n

    def union(self, a, b):
        if self.find(a) == self.find(b):
            return False
        self.p[self.find(a)] = self.find(b)
        self.n -= 1
        return True

    def find(self, x):
        if self.p[x] != x:
            self.p[x] = self.find(self.p[x])
        return self.p[x]


class Solution:
    def findCriticalAndPseudoCriticalEdges(
        self, n: int, edges: List[List[int]]
    ) -> List[List[int]]:
        for i, e in enumerate(edges):
            e.append(i)
        edges.sort(key=lambda x: x[2])
        uf = UnionFind(n)
        v = sum(w for f, t, w, _ in edges if uf.union(f, t))
        ans = [[], []]
        for f, t, w, i in edges:
            uf = UnionFind(n)
            k = sum(z for x, y, z, j in edges if j != i and uf.union(x, y))
            if uf.n > 1 or (uf.n == 1 and k > v):
                ans[0].append(i)
                continue

            uf = UnionFind(n)
            uf.union(f, t)
            k = w + sum(z for x, y, z, j in edges if j != i and uf.union(x, y))
            if k == v:
                ans[1].append(i)
        return ans

Complexity

Time
O(V+E)
Space · auxiliary
O(V)

Pattern: Union-Find

Merge groups and ask whether two things are connected, both in near-constant time. LeetCode 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree is filed here because LeetCode tags it Union Find, which is the vocabulary this hub collects.

The union-find guide has the Python template for the pattern and the 83 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree?

LeetCode 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree is rated Hard on LeetCode.

What is the time complexity of LeetCode 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree?

The Python solution on this page runs in O(V+E).

What is the space complexity of LeetCode 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree?

The Python solution on this page uses O(V) auxiliary space.

What topics does LeetCode 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree cover?

LeetCode 1489. Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree is tagged Union Find, Graph, Minimum Spanning Tree, Sorting and Strongly Connected Component on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.