VOOZH about

URL: https://www.geeksforgeeks.org/dsa/number-of-critical-and-pseudo-critical-edges/

⇱ Number of Critical and Pseudo-Critical edges - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Number of Critical and Pseudo-Critical edges

Last Updated : 23 Jul, 2025

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]]

👁 1

Output: critical : [0,1], psedo critical : [2 3 4 5]

Explanation:

👁 2

The 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.

  • Create a new array to store the edges and corresponding indices.
  • Sort the edges array.
  • Then find the Weight of Actual MST.
  • Then, find the Critical and Pseudo Critical Edges

Below is the implementation of the above approach (the explanation of the code is given at the end of this implementation):


Output
[0 1 ]
[2 3 4 5 ]

Below is the code explanation to solve the problem:

  • The disjoint() function initializes a disjoint-set data structure for a given size. It creates a parent array where each element initially points to itself, indicating that every vertex is in its own disjoint set.
  • The find() function is to find the operation of the disjoint-set data structure. It finds the root (parent) of the set to which vertex u belongs. It uses path compression to optimize future find operations.
  • The merge() function merges two disjoint sets represented by vertices u and v. It does this by making the parent of one set point to the parent of the other set.
  • help1() function computes the MST weight of the graph when edge j is excluded. It uses Kruskal's algorithm to construct the MST while excluding edge j. It returns the weight of the resulting MST.
  • help2() function computes the MST weight of the graph when only edge j is included. It starts with edge j as part of the MST and then continues to add edges using Kruskal's algorithm. It returns the weight of the resulting MST.
  • findCriticalAndPseudoCriticalEdges(int n, vector<vector<int>>& e) function that finds critical and pseudo-critical edges.
  • It iterates through all edges in the graph and, for each edge, calculates two MST weights: one when the edge is excluded (new_weight1) and one when only that edge is included (new_weight2).
  • Based on these weights, it determines whether the edge is critical or pseudo-critical and adds it to the corresponding lists v1 or v2.
  • Finally, it returns these lists in the ans vector.

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.

Comment
Article Tags: