Post · 5 min read· pp. 579–581

Finding bridges in a graph in $O(N+M)$

We are given an undirected graph. 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). The task is to find all bridges in the given 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 NN is the number of vertices and MM is the number of edges in the graph.

Note that there 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 rootroot and run depth first search from it. Note the following fact (which is easy to prove):

  • Let's say we are in the DFS, looking through the edges starting from vertex vv. The current edge (v,to)(v, to) is a bridge if and only if none of the vertices toto and its descendants in the DFS traversal tree has a back-edge to vertex vv or any of its ancestors. Indeed, this condition means that there is no other way from vv to toto except for edge (v,to)(v, to).

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]\mathtt{tin}[v] denote entry time for node vv. We introduce an array low\mathtt{low} which will let us store the earliest entry time of the node found in the DFS search that a node vv can reach with a single edge from itself or its descendants. low[v]\mathtt{low}[v] is the minimum of tin[v]\mathtt{tin}[v], the entry times tin[p]\mathtt{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]\mathtt{low}[to] for each vertex toto which is a direct descendant of vv in the DFS tree:

low[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}\mathtt{low}[v] = \min \left\{ \begin{array}{l} \mathtt{tin}[v] \\ \mathtt{tin}[p] &\text{ for all }p\text{ for which }(v, p)\text{ is a back edge} \\ \mathtt{low}[to] &\text{ for all }to\text{ for which }(v, to)\text{ is a tree edge} \end{array} \right\}

Now, there is a back edge from vertex vv or one of its descendants to one of its ancestors if and only if vertex vv has a child toto for which low[to]≤tin[v]\mathtt{low}[to] \leq \mathtt{tin}[v]. If low[to]=tin[v]\mathtt{low}[to] = \mathtt{tin}[v], the back edge comes directly to vv, otherwise it comes to one of the ancestors of vv.

Thus, the current edge (v,to)(v, to) in the DFS tree is a bridge if and only if low[to]>tin[v]\mathtt{low}[to] > \mathtt{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:

  • visited[to]=false\mathtt{visited}[to] = false - the edge is part of DFS tree;
  • visited[to]=true\mathtt{visited}[to] = true && to≠parentto \neq parent - the edge is back edge to one of the ancestors;
  • to=parentto = parent - the edge leads back to parent in DFS tree.

To implement this, we need a depth first search function which accepts the parent vertex of the current node.

For the cases of multiple edges, we need to be careful when ignoring the edge from the parent. To solve this issue, we can add a flag parent_skipped which will ensure we only skip the parent once.

text
void IS_BRIDGE(int v,int to); // some function to process the found bridge
int n; // number of nodes
vector<vector<int>> adj; // adjacency list of graph

vector<bool> visited;
vector<int> tin, low;
int timer;

void dfs(int v, int p = -1) {
    visited[v] = true;
    tin[v] = low[v] = timer++;
    bool parent_skipped = false;
    for (int to : adj[v]) {
        if (to == p && !parent_skipped) {
            parent_skipped = true;
            continue;
        }
        if (visited[to]) {
            low[v] = min(low[v], tin[to]);
        } else {
            dfs(to, v);
            low[v] = min(low[v], low[to]);
            if (low[to] > tin[v])
                IS_BRIDGE(v, to);
        }
    }
}

void find_bridges() {
    timer = 0;
    visited.assign(n, false);
    tin.assign(n, -1);
    low.assign(n, -1);
    for (int i = 0; i < n; ++i) {
        if (!visited[i])
            dfs(i);
    }
}

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.

The parent_skipped flag makes this implementation handle multiple edges between a vertex and its parent: it skips only the DFS tree edge used to enter the vertex, while an additional parallel edge is processed as a back 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 edges).

Practice Problems