Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree - Problem
Network Infrastructure Analysis: You're designing a critical network infrastructure connecting n cities. Given a weighted undirected graph where edges[i] = [ai, bi, weighti] represents a connection between cities ai and bi with cost weighti.

Your task is to analyze the Minimum Spanning Tree (MST) - the most cost-effective way to connect all cities. You need to classify edges into two categories:

πŸ”΄ Critical Edges: Removing these edges would increase the total MST cost (network becomes more expensive)
🟑 Pseudo-Critical Edges: These edges can appear in some MSTs but aren't essential (alternative equally good solutions exist)

Goal: Return two lists containing the indices of critical and pseudo-critical edges respectively.

Input & Output

example_1.py β€” Basic Network
$ Input: n = 4, edges = [[0,1,1],[1,2,1],[2,3,2],[0,3,2],[0,3,4],[2,3,1],[1,3,2]]
β€Ί Output: [[0,5],[1,2,3,4,6]]
πŸ’‘ Note: Edge 0 connects nodes 0-1 with weight 1, and edge 5 connects nodes 2-3 with weight 1. These are critical because removing either would force using heavier alternatives. The remaining edges can appear in some MSTs but aren't essential.
example_2.py β€” Triangle Graph
$ Input: n = 5, edges = [[0,1,1],[1,2,1],[2,3,1],[3,4,1],[0,4,1]]
β€Ί Output: [[],[0,1,2,3,4]]
πŸ’‘ Note: This forms a cycle where any 4 edges can create a valid MST with weight 4. No edges are critical, but all are pseudo-critical since each can be part of an optimal solution.
example_3.py β€” Bridge Network
$ Input: n = 4, edges = [[0,1,1],[1,2,1],[2,3,1]]
β€Ί Output: [[0,1,2],[]]
πŸ’‘ Note: This is a path graph where every edge is essential for connectivity. All edges are critical, and none are merely pseudo-critical.

Visualization

Tap to expand
ABCDECritical: $1MCritical: $2MAlt: $3MAlt: $2MAlt: $3MAlt: $1MNetwork Analysis ResultsπŸ”΄ Critical RoadsA-B: Essential backboneB-C: No alternativesRemoving increases costMust build for connectivity🟑 Pseudo-CriticalA-D, B-D, B-E, C-EAlternative valid routesSame total cost possibleFlexible implementationπŸ“Š Optimal StrategyMin Cost: $3M totalMust include: A-B, B-CChoose 2 from alternativesMultiple valid solutions
Understanding the Visualization
1
πŸ—ΊοΈ Survey the Territory
Identify all possible road connections between cities with their construction costs
2
πŸ“Š Find Optimal Network
Use Kruskal's algorithm to determine the minimum cost to connect all cities
3
πŸ” Test Road Importance
For each road, see what happens if we remove it - does cost increase?
4
πŸ›£οΈ Test Alternative Routes
For non-critical roads, see if including them first still gives optimal cost
5
πŸ“‹ Classify Infrastructure
Critical roads are mandatory, pseudo-critical roads are optional but viable
Key Takeaway
🎯 Key Insight: An edge is critical if removing it breaks the optimal cost, and pseudo-critical if it can be part of an optimal solution but isn't mandatory. Use Kruskal's algorithm with systematic edge testing to classify each connection efficiently.

Time & Space Complexity

Time Complexity
⏱️
O(EΒ² log E)

For each of E edges, we run Kruskal's algorithm which takes O(E log E) time

n
2n
⚑ Linearithmic
Space Complexity
O(E + V)

Space for Union-Find structure and edge sorting

n
2n
βœ“ Linear Space

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 edge weights are distinct (no duplicate weights)
  • There is no repeated edge in the graph
Asked in
Google 45
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen