Post · 4 min read· pp. 588–590

Finding articulation points in a graph in $O(N+M)$

We are given an undirected graph. An articulation point (or cut vertex) is defined as a vertex which, when removed along with associated edges, makes the graph disconnected (or more precisely, increases the number of connected components in the graph). The task is to find all articulation points in the given graph.

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.

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 v≠rootv\ne root. If the current edge (v,to)(v, to) is such that none of the vertices toto or its descendants in the DFS traversal tree has a back-edge to any of ancestors of vv, then vv is an articulation point. Otherwise, vv is not an articulation point.
  • Let's consider the remaining case of v=rootv=root. This vertex will be the point of articulation if and only if this vertex has more than one child in the DFS tree.

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 vv. We introduce an array low[v]low[v] which will let us check the fact for each vertex vv. 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 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 edgelow[v] = \min \begin{cases} tin[v] \\ tin[p] &\text{ for all }p\text{ for which }(v, p)\text{ is a back edge} \\ low[to]& \text{ for all }to\text{ for which }(v, to)\text{ is a tree edge} \end{cases}

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]low[to] < tin[v]. If low[to]=tin[v]low[to] = tin[v], the back edge comes directly to vv, otherwise it comes to one of the ancestors of vv.

Thus, the vertex vv in the DFS tree is an articulation point if and only if low[to]≥tin[v]low[to] \geq 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]=falsevisited[to] = false - the edge is part of DFS tree;
  • visited[to]=truevisited[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.

text
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++;
    int children=0;
    for (int to : adj[v]) {
        if (to == p) 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] && p!=-1)
                IS_CUTPOINT(v);
            ++children;
        }
    }
    if(p == -1 && children > 1)
        IS_CUTPOINT(v);
}

void find_cutpoints() {
    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_cutpoints; it performs necessary initialization and starts depth first search in each connected component of the graph.

Function IS_CUTPOINT(a) is some function that will process the fact that vertex aa is an articulation point, for example, print it (Caution that this can be called multiple times for a vertex).

Practice Problems