DATA STRUCTURES PROGRAM • GRAPHS

Find Articulation Points with Tarjan's DFS

Learn how to find articulation points with tarjan's dfs using a clear C program.

AdvancedArticulation pointDiscovery timeLow link

PROBLEM UNDERSTANDING

Input and expected output

Sample input
No input required
Sample output
Articulation points: 0 3

COMPLETE C PROGRAM

Complete C implementation

dsa-articulation-points.c
Open in compiler
#include <stdio.h>

void ap_dfs(int vertex, int graph[5][5], int visited[5], int discovery[5],
            int low[5], int parent[5], int articulation[5], int *time)
{
    visited[vertex] = 1; discovery[vertex] = low[vertex] = ++(*time);
    int children = 0;
    for (int neighbour = 0; neighbour < 5; neighbour++) if (graph[vertex][neighbour]) {
        if (!visited[neighbour]) {
            children++; parent[neighbour] = vertex;
            ap_dfs(neighbour, graph, visited, discovery, low, parent, articulation, time);
            if (low[neighbour] < low[vertex]) low[vertex] = low[neighbour];
            if (parent[vertex] == -1 && children > 1) articulation[vertex] = 1;
            if (parent[vertex] != -1 && low[neighbour] >= discovery[vertex]) articulation[vertex] = 1;
        } else if (neighbour != parent[vertex] && discovery[neighbour] < low[vertex])
            low[vertex] = discovery[neighbour];
    }
}

int main(void)
{
    int graph[5][5] = {
        {0,1,1,1,0}, {1,0,1,0,0}, {1,1,0,0,0}, {1,0,0,0,1}, {0,0,0,1,0}
    };
    int visited[5] = {0}, discovery[5], low[5], parent[5] = {-1,-1,-1,-1,-1};
    int articulation[5] = {0}, time = 0;
    ap_dfs(0, graph, visited, discovery, low, parent, articulation, &time);
    printf("Articulation points: ");
    for (int vertex = 0; vertex < 5; vertex++) if (articulation[vertex]) printf("%d ", vertex);
    putchar('\n');
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

Articulation points: 0 3
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Use low-link values to detect vertices whose DFS child cannot reach an ancestor.
  3. Display the computed result.

Use low-link values to detect vertices whose DFS child cannot reach an ancestor.

EFFICIENCY

Time and space complexity

Time complexity

O(V²) with matrix

Auxiliary space

O(V)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.