![]() |
VOOZH | about |
Given a weighted undirected graph with n vertices numbered from 0 to n - 1, and an array of edges where edges[i] = [u, v, weight] represents a bidirectional and weighted edge between nodes u and v. Find all the critical and pseudo-critical edges in the given graph's MST.
An MST edge whose deletoin 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.
Examples:
Input: Vertices: 5
Edges: [u v weight]=[[0 1 1],[1 2 1],[2 3 2],[0 3 2],[0 4 3],[3 4 3],[1 4 6]]
👁 1Output: critical : [0,1], psedo critical : [2 3 4 5]
Explanation:
👁 2The two edges 0 and 1 appear in all MSTs, therefore they are critical edges
The edges 2, 3, 4, and 5 are only part of some MSTs, therefore they are considered pseudo-critical edges.
Input: Vertices = 4, Edges: [u v weight]= [[0,1,1],[1,2,1],[2,3,1],[0,3,1]]
Output: critcal : [ ] pseudo critical: [0 1 2 3]
Explanation: It's noticeable that when all four edges possess equal weight, selecting any three edges out of the provided four will result in a minimum spanning tree (MST). As a consequence, all four edges can be classified as pseudo-critical and there is no critical edges
Approach:
This problem can be solved using a variation of Union Find Algorithm to store the edges and then use Kruskal's algorithm to find the MST.
Below is the implementation of the above approach (the explanation of the code is given at the end of this implementation):
[0 1 ] [2 3 4 5 ]
Below is the code explanation to solve the problem:
Time complexity: O(E * log(V)) where E is the number of edges and V is the number of vertices.
Auxiliary Space Complexity: O(E+V) where E is the number of edges and V is the number of vertices.