In: Computer Science
Given a connected graph G with n vertices. We say an edge of G is a bridge if the graph becomes a disconnected graph after removing the edge. Give an O(m + n) time algorithm that finds all the bridges. (Partial credits will be given for a polynomial time algorithm.) (Hint: Use DFS)
A bridge is defined as an edge which, when removed, makes the graph disconnected (or more precisely, increases the number of connected components in the graph).
Informally, the problem is formulated as follows: given a map of cities connected with roads, find all "important" roads, i.e. roads which, when removed, cause disappearance of a path between some pair of cities.
The algorithm described here is based on depth first search and has O(N+M)O(N+M) complexity, where N is the number of vertices and MM is the number of edges in the graph.
Note that there also is also the article Finding Bridges Online - unlike the offline algorithm described here, the online algorithm is able to maintain the list of all bridges in a changing graph (assuming that the only type of change is addition of new edges).
Algorithm;
Pick an arbitrary vertex of the graph root and run depth first search from it. Note the following fact (which is easy to prove):
Now we have to learn to check this fact for each vertex efficiently. We'll use "time of entry into node" computed by the depth first search.
So, let tin[v]tin[v] denote entry time for node v. We introduce an array low which will let us check the fact for each vertex v. low[v]low[v] is the minimum of tin[v]tin[v], the entry times tin[p]tin[p] for each node pp that is connected to node vv via a back-edge (v,p)(v,p) and the values of low[to]low[to] for each vertex to which is a direct descendant of v in the DFS tree:
low[v]=min⎧⎩⎨tin[v]tin[p]low[to] for all p for which (v,p) is a back edge for all to for which (v,to) is a tree edgelow[v]=min{tin[v]tin[p] for all p for which (v,p) is a back edgelow[to] for all to for which (v,to) is a tree edge
Now, there is a back edge from vertex v or one of its descendants to one of its ancestors if and only if vertex v has a child to for which low[to]≤tin[v]low[to]≤tin[v]. If low[to]=tin[v]low[to]=tin[v], the back edge comes directly to v, otherwise it comes to one of the ancestors of v.
Thus, the current edge (v,to)(v,to) in the DFS tree is a bridge if and only if low[to]>tin[v]low[to]>tin[v].
Implementation
The implementation needs to distinguish three cases: when we go down the edge in DFS tree, when we find a back edge to an ancestor of the vertex and when we return to a parent of the vertex. These are the cases:
To implement this, we need a depth first search function which accepts the parent vertex of the current node.
Main function is find_bridges; it performs necessary initialization and starts depth first search in each connected component of the graph.
Function IS_BRIDGE(a, b) is some function that will process the fact that edge (a,b)(a,b) is a bridge, for example, print it.
Note that this implementation malfunctions if the graph has multiple edges, since it ignores them. Of course, multiple edges will never be a part of the answer, so IS_BRIDGE can check additionally that the reported bridge is not a multiple edge. Alternatively it's possible to pass to dfs the index of the edge used to enter the vertex instead of the parent vertex (and store the indices of all vertices).