Reading history

Articulation Points and Bridges in C

6 min read 1 views

Find articulation points and bridges in an undirected graph in C with Tarjan's discovery and low-link values in one DFS, checked against brute force.

In a road or computer network, some places are critical: if one of them fails, the network splits into separate pieces. A vertex like this is called an articulation point (or cut vertex), and an edge like this is called a bridge. Finding them shows where a network needs backup links. Checking every vertex one by one is slow. Tarjan’s method finds all of them in a single depth-first search, in O(V + E) time.

In this lesson you will learn

  • What articulation points and bridges are
  • Discovery times and low-link values
  • The rule for a bridge: low[v] > disc[u]
  • The rules for an articulation point, including the root
  • How to check the answer by brute force

You should know graph BFS and DFS and strongly connected components.

The Example Graph

0123456bridgebridgearticulation pointbridge
  • Removing vertex 1 separates the triangle 0-1-2 from the rest, so 1 is an articulation point. The same is true of 3 and 4.
  • Edge 1–3 is a bridge: it is the only link between the two triangles. Edge 4–6 is also a bridge.
  • Edges inside a triangle are never bridges, because the other two sides give another route.

Checking by Brute Force

The direct method removes each vertex in turn and counts how many pieces remain.

#include <stdio.h>
#include <string.h>

#define N 7
static int adj[N][N], seen[N];

/* Count connected components, pretending vertex 'skip' has been removed */
static void dfs(int u, int skip) {
    seen[u] = 1;
    for (int v = 0; v < N; v++)
        if (adj[u][v] && !seen[v] && v != skip) dfs(v, skip);
}
static int components(int skip) {
    memset(seen, 0, sizeof seen);
    int c = 0;
    for (int v = 0; v < N; v++)
        if (v != skip && !seen[v]) { c++; dfs(v, skip); }
    return c;
}

int main(void) {
    int e[][2] = {{0,1},{1,2},{2,0},{1,3},{3,4},{4,5},{5,3},{4,6}};
    for (int i = 0; i < 8; i++) adj[e[i][0]][e[i][1]] = adj[e[i][1]][e[i][0]] = 1;

    printf("Brute-force check: remove each vertex and count the pieces\n");
    printf("  whole graph: %d piece\n", components(-1));
    for (int v = 0; v < N; v++) {
        int c = components(v);
        printf("  remove %d -> %d piece%s%s\n", v, c, c == 1 ? "" : "s",
               c > 1 ? "   <- articulation point" : "");
    }
    printf("This takes O(V(V + E)); Tarjan's method takes O(V + E).\n");
    return 0;
}

Output

Brute-force check: remove each vertex and count the pieces
  whole graph: 1 piece
  remove 0 -> 1 piece
  remove 1 -> 2 pieces   <- articulation point
  remove 2 -> 1 piece
  remove 3 -> 2 pieces   <- articulation point
  remove 4 -> 2 pieces   <- articulation point
  remove 5 -> 1 piece
  remove 6 -> 1 piece
This takes O(V(V + E)); Tarjan's method takes O(V + E).

This works, but it runs a whole DFS for each vertex. For a large network, that is far too slow.

Tarjan’s Method: One DFS

During the DFS, we give each vertex two numbers:

  • disc[u]: the time when u is first visited.
  • low[u]: the smallest discovery time that u, or anything below it in the DFS tree, can reach using at most one back edge (an edge to an earlier vertex that is not the parent).

For a tree edge u → v:

  • If low[v] > disc[u], nothing below v can reach u or anything above it. The edge u–v is the only way back, so it is a bridge.
  • If low[v] ≥ disc[u] and u is not the root, the part below v can only reach the rest of the graph through u. So u is an articulation point.
  • The root is an articulation point only if it has two or more children in the DFS tree.
#include <stdio.h>

#define N 7
/* Undirected graph with 7 vertices and 8 edges:
     triangle 0-1-2, triangle 3-4-5, link 1-3, and a leaf 6 hanging off 4 */
static int adj[N][N], disc[N], low[N], timer_ = 0, is_ap[N];

static int min(int a, int b) { return a < b ? a : b; }

static void dfs(int u, int parent) {
    disc[u] = low[u] = ++timer_;
    int children = 0;
    for (int v = 0; v < N; v++) {
        if (!adj[u][v]) continue;
        if (!disc[v]) {                      /* tree edge */
            children++;
            dfs(v, u);
            low[u] = min(low[u], low[v]);
            if (low[v] > disc[u])
                printf("  bridge: %d - %d\n", u, v);
            if (parent != -1 && low[v] >= disc[u]) is_ap[u] = 1;
        } else if (v != parent) {            /* back edge */
            low[u] = min(low[u], disc[v]);
        }
    }
    if (parent == -1 && children > 1) is_ap[u] = 1;   /* root rule */
}

int main(void) {
    int e[][2] = {{0,1},{1,2},{2,0},{1,3},{3,4},{4,5},{5,3},{4,6}};
    for (int i = 0; i < 8; i++) adj[e[i][0]][e[i][1]] = adj[e[i][1]][e[i][0]] = 1;
    printf("Bridges:\n");
    dfs(0, -1);
    printf("Articulation points:");
    for (int v = 0; v < N; v++) if (is_ap[v]) printf(" %d", v);
    printf("\n\nvertex: ");
    for (int v = 0; v < N; v++) printf("%3d", v);
    printf("\ndisc:   ");
    for (int v = 0; v < N; v++) printf("%3d", disc[v]);
    printf("\nlow:    ");
    for (int v = 0; v < N; v++) printf("%3d", low[v]);
    printf("\n");
    return 0;
}

Output

Bridges:
  bridge: 4 - 6
  bridge: 1 - 3
Articulation points: 1 3 4

vertex:   0  1  2  3  4  5  6
disc:     1  2  3  4  5  6  7
low:      1  1  1  4  4  4  7
  • Vertex 2 has a back edge to 0, so low[2] = 1. That pulls low[1] down to 1 as well. The whole triangle 0-1-2 can reach time 1.
  • Vertex 3 was discovered at time 4. Its subtree (3, 4, 5, 6) can only reach back to time 4, through the edge 5–3. So low[3] = 4 > disc[1] = 2, and 1–3 is a bridge.
  • Vertex 6 is a leaf with no back edge, so low[6] = 7 > disc[4] = 5, and 4–6 is a bridge.
  • The bridges print in the order the DFS finishes them, which is why 4–6 appears first.
  • The results match the brute-force check: articulation points 1, 3 and 4.

Where This Is Used

AreaQuestion answered
Computer networksWhich routers or cables are single points of failure?
Road and power networksWhich roads or lines would cut off a region if they failed?
Social networksWhich people connect otherwise separate groups?
Graph algorithmsSplitting a graph into 2-edge-connected or biconnected components

Common Mistakes

  1. Treating the parent edge as a back edge, which makes every low value too small.
  2. Using the articulation rule on the root; the root needs the “two children” rule.
  3. Mixing up > and ≥: bridges need low[v] > disc[u], articulation points need low[v] ≥ disc[u].
  4. Forgetting disconnected graphs: start a DFS from every unvisited vertex.

Practice Exercises

Exercise 1. In a simple path 0 – 1 – 2 – 3, which vertices are articulation points and which edges are bridges?

Show answer

Vertices 1 and 2 are articulation points. All three edges are bridges, because a path has no cycles.

Exercise 2. Adding which single edge to the example graph removes every bridge?

Show answer

Add an edge from 6 to any vertex in the first triangle, such as 6–0. Then 0-1-3-4-6-0 forms a cycle that covers both bridges, so neither 1–3 nor 4–6 is a bridge any more.

Frequently Asked Questions

Is every end of a bridge an articulation point?

Not always. In the example, 6 is an end of the bridge 4–6, but removing 6 leaves the rest connected. A bridge end is an articulation point only if it has other edges.

How is this related to Tarjan’s SCC algorithm?

Both use discovery times and low-link values in one DFS. The SCC version works on directed graphs and uses a stack; this version works on undirected graphs and ignores the parent edge.

Summary

  • An articulation point or bridge is a vertex or edge whose removal disconnects the graph.
  • disc[u] is the visit time; low[u] is the earliest time reachable from u’s subtree.
  • Bridge: low[v] > disc[u]. Articulation point: low[v] ≥ disc[u], or a root with two or more children.
  • One DFS finds everything in O(V + E).

Next: LRU Cache in C.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *