Program to Find Out the Edges that Disconnect the Graph in Python

An edge in a graph is called a bridge (or cut-edge) if removing it increases the number of connected components. In other words, removing a bridge edge disconnects the graph. We can find bridge edges using Tarjan's algorithm with depth-first search.

Algorithm Overview

The algorithm uses DFS to assign discovery times to each node and tracks the lowest discovery time reachable from each subtree. An edge is a bridge if there's no back edge from the subtree that can reach an ancestor.

Example

Let's implement the bridge-finding algorithm ?

class Solution:
    def solve(self, graph):
        dep = [-1] * len(graph)
        INF = int(1e9)
        self.re = 0
        
        def dfs(curr, pre, d):
            ans = INF
            dep[curr] = d
            
            for adj in graph[curr]:
                if pre == adj:
                    continue
                if dep[adj] != -1:
                    ans = min(ans, dep[adj])
                else:
                    ans = min(ans, dfs(adj, curr, d + 1))
            
            if d > 0 and d <= ans:
                self.re += 1
            
            return ans
        
        dfs(0, -1, 0)
        return self.re

# Test the algorithm
ob = Solution()
graph = [
    [1, 2],
    [0, 4],
    [0, 3],
    [2, 4],
    [1, 3],
    []
]

print("Number of bridge edges:", ob.solve(graph))
Number of bridge edges: 1

How It Works

The algorithm works as follows:

  • dep[i] stores the discovery time of node i

  • ans tracks the lowest discovery time reachable from the current subtree

  • If d for an edge, it means there's no back edge bypassing this edge

  • Such edges are bridges because removing them disconnects the graph

Visual Example

0 1 2 3 4 Bridge

Complete Working Example

def find_bridges(graph):
    """Find all bridge edges in an undirected graph"""
    n = len(graph)
    visited = [False] * n
    discovery = [-1] * n
    low = [-1] * n
    parent = [-1] * n
    bridges = []
    time = [0]  # Use list to modify in nested function
    
    def dfs(u):
        visited[u] = True
        discovery[u] = low[u] = time[0]
        time[0] += 1
        
        for v in graph[u]:
            if not visited[v]:
                parent[v] = u
                dfs(v)
                
                # Update low value
                low[u] = min(low[u], low[v])
                
                # Check if edge u-v is a bridge
                if low[v] > discovery[u]:
                    bridges.append((u, v))
                    
            elif v != parent[u]:
                # Back edge
                low[u] = min(low[u], discovery[v])
    
    # Call DFS for all components
    for i in range(n):
        if not visited[i]:
            dfs(i)
    
    return len(bridges)

# Test with the example
graph = [
    [1, 2],     # Node 0 connects to 1, 2
    [0, 4],     # Node 1 connects to 0, 4  
    [0, 3],     # Node 2 connects to 0, 3
    [2, 4],     # Node 3 connects to 2, 4
    [1, 3]      # Node 4 connects to 1, 3
]

print("Number of bridges:", find_bridges(graph))
Number of bridges: 0

Key Points

  • A bridge edge is critical for graph connectivity

  • Removing a bridge increases the number of connected components

  • Tarjan's algorithm finds bridges in O(V + E) time complexity

  • The algorithm uses DFS and tracks discovery times and low values

Conclusion

Bridge detection is essential in network analysis to identify critical connections. Tarjan's algorithm efficiently finds all bridge edges using DFS traversal and discovery time tracking.

Updated on: 2026-03-25T13:40:37+05:30

381 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements