Program to find out the critical and pseudo-critical edges in a graph in Python

A Minimum Spanning Tree (MST) contains the minimum weight edges that connect all vertices in a graph. In this problem, we need to identify critical edges (edges whose removal increases MST weight) and pseudo-critical edges (edges that can appear in some MSTs but not all).

Problem Understanding

Given an undirected weighted graph with n vertices (0 to n-1), we need to find:

  • Critical edges: Removing them increases the MST weight
  • Pseudo-critical edges: Can be part of an MST but aren't required
0 1 2 3 4 10 10 10 10 10

Algorithm Approach

The solution uses Prim's algorithm to find MST with these steps:

  1. Find the original MST weight
  2. For each edge, check if removing it increases MST weight (critical)
  3. For each edge, check if including it first gives same MST weight (pseudo-critical)

Implementation

from heapq import heappop, heappush

def solve(num_vertices, edges):
    # Build adjacency list representation
    graph = dict()
    for u, v, w in edges:
        graph.setdefault(u, []).append((v, w))
        graph.setdefault(v, []).append((u, w))
    
    # Find original MST weight
    original_mst_weight = find_mst(num_vertices, graph)
    
    critical_edges, pseudo_critical_edges = [], []
    
    for i in range(len(edges)):
        # Check if edge is critical (removing it increases MST weight)
        if find_mst(num_vertices, graph, exl=edges[i][:2]) > original_mst_weight:
            critical_edges.append(i)
        # Check if edge is pseudo-critical (can be part of MST)
        elif find_mst(num_vertices, graph, init=edges[i]) == original_mst_weight:
            pseudo_critical_edges.append(i)
    
    return [critical_edges, pseudo_critical_edges]

def find_mst(num_vertices, graph, init=None, exl=None):
    def visit(u):
        visited[u] = True
        for v, w in graph.get(u, []):
            # Skip excluded edge
            if exl and u in exl and v in exl:
                continue
            if not visited[v]:
                heappush(heap, (w, u, v))
    
    result = 0
    visited = [False] * num_vertices
    heap = []
    
    # If we need to include a specific edge first
    if init:
        u, v, w = init
        result += w
        visited[u] = visited[v] = True
        visit(u)
        visit(v)
    else:
        visit(0)
    
    # Prim's algorithm
    while heap:
        weight, u, v = heappop(heap)
        if visited[u] and visited[v]:
            continue
        result += weight
        if not visited[u]:
            visit(u)
        if not visited[v]:
            visit(v)
    
    # Return infinity if graph is not connected
    return result if all(visited) else float('inf')

# Test the function
edges = [[0,1,10],[1,2,10],[2,3,10],[3,4,10],[4,0,10]]
result = solve(5, edges)
print(result)
[[], [0, 1, 2, 3, 4]]

How It Works

In the example graph, all edges have weight 10. Since any 4 edges can form an MST with total weight 40:

  • No critical edges: Removing any single edge still allows an MST with weight 40
  • All edges are pseudo-critical: Each edge can be part of some valid MST

Time Complexity

The algorithm runs Prim's algorithm multiple times:

  • Time Complexity: O(E² log V) where E is edges and V is vertices
  • Space Complexity: O(V + E) for graph representation

Conclusion

This solution identifies critical and pseudo-critical edges by testing MST weight with and without each edge. Critical edges are essential for minimum weight, while pseudo-critical edges provide alternative MST paths.

Updated on: 2026-03-26T14:02:22+05:30

834 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements