Articulation point in graph

Pattern: Bridges and articulation points

hard

A vertex is an articulation point when removing it disconnects the graph. With the same tin and low as for bridges: a non-root u is one if some child v has low[v] ≥ tin[u]; the DFS root is one if it has two or more DFS children.

The problem

Return all articulation points in increasing order, or [-1] if there are none. Vertices are numbered from 0 and each edge is given as [u, v].

Think first

  • How is this different from a bridge?
  • What is special about the DFS root?
  • Why >= here but > for bridges?

Hints

    Brute force, and why it fails

    Remove each vertex and count components: O(V × (V + E)).

    The key insight

    The same single DFS as for bridges answers it, with the root handled separately.

    Dry run

    Edges 0-1, 1-4, 4-2, 2-3, 3-4. DFS from 0: 0, 1, 4, 2, 3.

    Step 1 of 3

    The plan

    dfs(u, parent): ... children += 1; if parent != -1 and low[v] >= tin[u]: cut[u]
    root: cut if children > 1
    

    Complexity

    Time
    O(V + E)
    Space
    O(V)

    One DFS.

    Edge cases

    • A cycle: [-1].
    • A path: every inner vertex.
    • A single vertex: [-1].
    I've built my logic, show the code

    Java

    import java.util.*;
    
    class Solution {
        private int timer;
        private int[] tin, low;
        private boolean[] cut;
        private List<List<Integer>> adj;
    
        public List<Integer> articulationPoints(int n, int[][] edges) {
            adj = new ArrayList<>();
            for (int i = 0; i < n; i++) adj.add(new ArrayList<>());
            for (int[] e : edges) { adj.get(e[0]).add(e[1]); adj.get(e[1]).add(e[0]); }
            tin = new int[n];
            low = new int[n];
            cut = new boolean[n];
            Arrays.fill(tin, -1);
            timer = 0;
            for (int i = 0; i < n; i++) if (tin[i] == -1) dfs(i, -1);
            List<Integer> out = new ArrayList<>();
            for (int i = 0; i < n; i++) if (cut[i]) out.add(i);
            if (out.isEmpty()) out.add(-1);
            return out;
        }
    
        private void dfs(int u, int parent) {
            tin[u] = low[u] = timer++;
            int children = 0;
            for (int v : adj.get(u)) {
                if (v == parent) continue;
                if (tin[v] == -1) {
                    dfs(v, u);
                    low[u] = Math.min(low[u], low[v]);
                    children++;
                    if (parent != -1 && low[v] >= tin[u]) cut[u] = true;   // v's side cannot get above u without u
                } else low[u] = Math.min(low[u], tin[v]);
            }
            if (parent == -1 && children > 1) cut[u] = true;   // a root with two separate subtrees
        }
    }

    C++

    class Solution {
    public:
        int timer;
        vector<int> tin, low;
        vector<bool> cut;
        vector<vector<int>> adj;
    
        vector<int> articulationPoints(int n, vector<vector<int>>& edges) {
            adj.assign(n, vector<int>());
            for (const vector<int>& e : edges) { adj[e[0]].push_back(e[1]); adj[e[1]].push_back(e[0]); }
            tin.assign(n, -1);
            low.assign(n, 0);
            cut.assign(n, false);
            timer = 0;
            for (int i = 0; i < n; i++) if (tin[i] == -1) dfs(i, -1);
            vector<int> out;
            for (int i = 0; i < n; i++) if (cut[i]) out.push_back(i);
            if (out.empty()) out.push_back(-1);
            return out;
        }
    
        void dfs(int u, int parent) {
            tin[u] = low[u] = timer++;
            int children = 0;
            for (int v : adj[u]) {
                if (v == parent) continue;
                if (tin[v] == -1) {
                    dfs(v, u);
                    low[u] = min(low[u], low[v]);
                    children++;
                    if (parent != -1 && low[v] >= tin[u]) cut[u] = true;   // v's side cannot get above u without u
                } else low[u] = min(low[u], tin[v]);
            }
            if (parent == -1 && children > 1) cut[u] = true;   // a root with two separate subtrees
        }
    };

    Python

    class Solution:
        def articulationPoints(self, n: int, edges: list[list[int]]) -> list[int]:
            adj = [[] for _ in range(n)]
            for u, v in edges:
                adj[u].append(v)
                adj[v].append(u)
            tin, low, cut, timer = [-1] * n, [0] * n, [False] * n, 0
    
            def dfs(u, parent):
                nonlocal timer
                tin[u] = low[u] = timer
                timer += 1
                children = 0
                for v in adj[u]:
                    if v == parent:
                        continue
                    if tin[v] == -1:
                        dfs(v, u)
                        low[u] = min(low[u], low[v])
                        children += 1
                        if parent != -1 and low[v] >= tin[u]:
                            cut[u] = True           # v's side cannot get above u without u
                    else:
                        low[u] = min(low[u], tin[v])
                if parent == -1 and children > 1:
                    cut[u] = True                   # a root with two separate subtrees
    
            for i in range(n):
                if tin[i] == -1:
                    dfs(i, -1)
            return [i for i in range(n) if cut[i]] or [-1]

    Common mistakes

    • Applying the child rule to the root.
    • Reporting the same vertex twice.

    Similar problems