![]() |
VOOZH | about |
Given an undirected Graph, The task is to find the Bridges in this Graph.
An edge in an undirected connected graph is a bridge if removing it disconnects the graph. For a disconnected undirected graph, the definition is similar, a bridge is an edge removal that increases the number of disconnected components.
Like Articulation Points, bridges represent vulnerabilities in a connected network and are useful for designing reliable networks.
Examples:
Input:
Output: (0, 3) and (3, 4)
Input:
Output: (1, 6)
Input:
Output: (0, 1), (1, 2), and (2, 3)
Naive Approach: Below is the idea to solve the problem:
One by one remove all edges and see if the removal of an edge causes a disconnected graph.
Follow the below steps to Implement the idea:
Time Complexity: O(E*(V+E)) for a graph represented by an adjacency list.
Auxiliary Space: O(V+E)
Before heading towards the approach understand which edge is termed as bridge. Suppose there exists a edge from u -> v, now after removal of this edge if v can't be reached by any other edges then u -> v edge is bridge. Our approach is based on this intuition, so take time and grasp it.
ALGORITHM: -
To implement this algorithm, we need the following data structures -
We will traverse the graph using DFS traversal but with slight modifications i.e. while traversing we will keep track of the parent node by which the particular node is reached because we will update the low[node] = min(low[all it's adjacent node except parent]) hence we need to keep track of the parent.
While traversing adjacent nodes let 'v' of a particular node let 'u', then 3 cases arise -
1. v is parent of u then,
2. v is visited then,
3. v is not visited then,
Below is the implementation of the above approach:
Bridges in first graph 3 4 0 3 Bridges in second graph 2 3 1 2 0 1 Bridges in third graph 1 6
Time Complexity: O(V+E),
Auxiliary Space: O(V) is used for visited, disc and low arrays.