Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree is a Hard Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Bridges & Articulation Points (Tarjan low-link) subpattern.
Problem statement
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.
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 <= 100 1 <= edges.length <= min(200, n (n - 1) / 2) edges[i].length == 3 0 <= ai < bi < n 1 <= weighti <= 1000 All pairs (ai, bi) are distinct.
Topics and companies
Topics: Depth-first Search, Union Find.
Reported in interviews at Amazon.
How to practise Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree on Thita.ai
Starter code is provided in C, C#, C++, dart, elixir, erlang, Go, Java, JavaScript, Kotlin, PHP, Python, python3, racket, Ruby, Rust, Scala, Swift and TypeScript. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(nlogn) time and O(n) space.
Open Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree in the code editor, or read the Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree editorial for a worked solution with its approach and complexity analysis.
Where Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree sits in the DSA pattern sheet
- Graph Traversal Patterns (DFS & BFS) pattern guide
- Bridges & Articulation Points (Tarjan low-link) subpattern
- The full DSA patterns sheet
- All coding practice problems
Problems related to Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
Other problems that use the same Bridges & Articulation Points (Tarjan low-link) and Graph Traversal Patterns (DFS & BFS) techniques:
- Critical Connections in a Network — Hard, same subpattern
- Accounts Merge — Medium, same pattern
- Alien Dictionary — Hard, same pattern
- All Paths from Source Lead to Destination — Medium, same pattern
- Build a Matrix With Conditions — Hard, same pattern
- Bus Routes — Hard, same pattern