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.
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:
For every edge (u, v), do the following:
Remove (u, v) from the graph
See if the graph remains connected (either uses BFS or DFS)
Add (u, v) back to the graph.
Time Complexity: O(E*(V+E)) for a graph represented by an adjacency list. Auxiliary Space: O(V+E)
Find Bridges in a graph using Tarjan’s Algorithm.
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.
To implement this algorithm, we need the following data structures –
visited[ ] = to keep track of the visited vertices to implement DFS
disc[ ] = to keep track when for the first time that particular vertex is reached
low[ ] = to keep track of the lowest possible time by which we can reach that vertex ‘other than parent’ so that if edge from parent is removed can the particular node can be reached other than parent.
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’ 3 cases arise –
1. v is parent of u then,
skip that iteration.
2. v is visited then,
update the low of v i.e. low[u] = min( low[u] , disc[v]) this arises when a node can be visited by more than one node, but low is to keep track of the lowest possible time so we will update it.
3. v is not visited then,
call the DFS to traverse ahead
now update the low[u] = min( low[u], low[v] ) as we know v can’t be parent cause we have handled that case first.
now check if ( low[v] > disc[u] ) i.e. the lowest possible to time to reach ‘v’ is greater than ‘u’ this means we can’t reach ‘v’ without ‘u’ so the edge u -> v is a bridge.
Below is the implementation of the above approach:
// A C++ program to find bridges in a given undirected graph
// A class that represents an undirected graph
intV; // No. of vertices
list<int> *adj; // A dynamic array of adjacency lists
Please Login to comment...