Similar Problems
Similar Problems not available
Find Critical And Pseudo Critical Edges In Minimum Spanning Tree - Leetcode Solution
Companies:
LeetCode: Find Critical And Pseudo Critical Edges In Minimum Spanning Tree Leetcode Solution
Difficulty: Hard
Topics: union-find sorting graph
Problem Statement:
Given a weighted undirected connected graph with n vertices. You are given the edge list edges, where edges[i] = [ui, vi, weighti] represents a bidirectional edge between nodes ui and vi with weight weighti.
A minimum spanning tree (MST) is a subset of the edges of the graph that connects all vertices without cycles and with the minimum possible total edge weight.
Find all the critical and pseudo-critical edges in the minimum spanning tree (MST) of the given graph. 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 not critical, but its inclusion in any MST is mandatory.
you need to return a 2D list of size (E, 2) where E is the total number of edges in the graph. The first column of the list represents the critical edges, and the second column represents the pseudo-critical edges. The order of the edges in the output does not matter.
Constraints: -> 2 <= n <= 100 -> 1 <= edges.length <= min(200, n * (n - 1) / 2) -> edges[i].length == 3 -> 0 <= ui, vi < n -> ui != vi -> 1 <= weighti <= 1000 -> All pairs (ui, vi) are distinct.
Solution:
First, we need to generate the Minimum Spanning Tree using Kruskal's algorithm. The steps are:
- Sort the edges based on their weight in ascending order.
- Initialize the number of sets as the number of vertices.
- Traverse the sorted edges list and join the sets of corresponding vertices, if joined add edges to MST, until the number of sets becomes one(no cycles).
- Iterate the edges of MST (not necessarily as Kruskal's algorithm order) and check if they are critical, pseudo-critical, or neither.
To check if an edge is critical:
- Remove the current edge from the graph and generate the MST.
- If the new MST weight is strictly greater than the weight of the previous MST, the edge is critical.
- Else, it is not the critical edge.
To check if an edge is pseudo-critical:
- Add the current edge to the graph (cannot use edges from the existing MST because it will make the new tree have cycles and will not be a valid MST).
- Generate the MST of the new graph.
- If the weight of the MST equals the weight of the previous MST, the edge is pseudo-critical.
- Else, it is not a pseudo-critical edge.
Finally, return the two lists: critical edges, and pseudo-critical edges.
Code:
Find Critical And Pseudo Critical Edges In Minimum Spanning Tree Solution Code
1