Algo-Sim › Advanced Graphs

Articulation Points (Cut Vertices)

Hard · Advanced Graphs · step through it in Java, Python and C++ on your own input

Find every node whose removal disconnects the graph with the same disc/low DFS as bridges, plus a special rule for the DFS root.

Problem

Given an undirected graph on nodes 0 … n − 1, return its articulation points in increasing order. A node is an articulation point (a cut vertex) when removing it, with its edges, leaves more connected pieces than before. The graph may be disconnected.

Constraints

Edge cases to test

Follow-up questions

Why ≥ for vertices but > for bridges?
If low[v] equals disc[u], v’s subtree reaches u itself but nothing higher. Cutting u still strands it, but cutting the edge does not, since the back edge to u remains.
What are biconnected components?
Maximal pieces with no articulation point. Keep a stack of edges in the same DFS and pop one component each time low[v] ≥ disc[u]: still O(V + E).

Solution

Java

List<List<Integer>> adj = new ArrayList<>();
int[] disc, low;
boolean[] cut;
int time = 0;

public List<Integer> articulationPoints(int n, int[][] edges) {
    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]);
    }
    disc = new int[n];
    low = new int[n];
    cut = new boolean[n];
    Arrays.fill(disc, -1);
    for (int u = 0; u < n; u++) {
        if (disc[u] == -1) dfs(u, -1);
    }
    List<Integer> out = new ArrayList<>();
    for (int u = 0; u < n; u++) if (cut[u]) out.add(u);
    return out;
}

private void dfs(int u, int parent) {
    disc[u] = low[u] = time++;
    int children = 0;
    for (int v : adj.get(u)) {
        if (v == parent) continue;
        if (disc[v] == -1) {
            children++;
            dfs(v, u);
            low[u] = Math.min(low[u], low[v]);
            if (parent != -1 && low[v] >= disc[u]) cut[u] = true;
        } else {
            low[u] = Math.min(low[u], disc[v]);
        }
    }
    if (parent == -1 && children >= 2) cut[u] = true;
}

Python

def articulation_points(n, edges):
    adj = [[] for _ in range(n)]
    for u, v in edges:
        adj[u].append(v)
        adj[v].append(u)
    disc = [-1] * n
    low = [0] * n
    cut = [False] * n
    time = 0

    def dfs(u, parent):
        nonlocal time
        disc[u] = low[u] = time
        time += 1
        children = 0
        for v in adj[u]:
            if v == parent:
                continue
            if disc[v] == -1:
                children += 1
                dfs(v, u)
                low[u] = min(low[u], low[v])
                if parent != -1 and low[v] >= disc[u]:
                    cut[u] = True
            else:
                low[u] = min(low[u], disc[v])
        if parent == -1 and children >= 2:
            cut[u] = True

    for u in range(n):
        if disc[u] == -1:
            dfs(u, -1)
    return [u for u in range(n) if cut[u]]

C++

#include <bits/stdc++.h>
using namespace std;

class Solution {
    vector<vector<int>> adj = {};
    vector<int> disc = {}, low = {};
    vector<bool> cut = {};
    int time = 0;

    void dfs(int u, int parent) {
        disc[u] = low[u] = time++;
        int children = 0;
        for (int v : adj[u]) {
            if (v == parent)
                continue;
            if (disc[v] == -1) {
                children++;
                dfs(v, u);
                low[u] = min(low[u], low[v]);
                if (parent != -1 && low[v] >= disc[u])
                    cut[u] = true;
            } else {
                low[u] = min(low[u], disc[v]);
            }
        }
        if (parent == -1 && children >= 2)
            cut[u] = true;
    }

  public:
    vector<int> articulationPoints(int n, vector<vector<int>> &edges) {
        adj.assign(n, {});
        for (vector<int> &e : edges) {
            adj[e[0]].push_back(e[1]);
            adj[e[1]].push_back(e[0]);
        }
        disc.assign(n, -1);
        low.assign(n, 0);
        cut.assign(n, false);
        for (int u = 0; u < n; u++) {
            if (disc[u] == -1)
                dfs(u, -1);
        }
        vector<int> out = {};
        for (int u = 0; u < n; u++)
            if (cut[u])
                out.push_back(u);
        return out;
    }
};

More Advanced Graphs problems