Articulation Points in Graph in DSA Typescript - Time & Space Complexity
We want to understand how the time needed to find articulation points changes as the graph grows.
Specifically, how does the algorithm's work increase with more nodes and edges?
Analyze the time complexity of the following code snippet.
function findArticulationPoints(graph: number[][]): number[] {
const n = graph.length;
const visited = new Array(n).fill(false);
const disc = new Array(n).fill(-1);
const low = new Array(n).fill(-1);
const parent = new Array(n).fill(-1);
const ap = new Array(n).fill(false);
let time = 0;
function dfs(u: number) {
visited[u] = true;
disc[u] = low[u] = ++time;
let children = 0;
for (const v of graph[u]) {
if (!visited[v]) {
children++;
parent[v] = u;
dfs(v);
low[u] = Math.min(low[u], low[v]);
if (parent[u] === -1 && children > 1) ap[u] = true;
if (parent[u] !== -1 && low[v] >= disc[u]) ap[u] = true;
} else if (v !== parent[u]) {
low[u] = Math.min(low[u], disc[v]);
}
}
}
for (let i = 0; i < n; i++) {
if (!visited[i]) dfs(i);
}
return ap.map((isAp, idx) => isAp ? idx : -1).filter(x => x !== -1);
}
This code finds all articulation points in a graph using depth-first search and tracking discovery and low times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Depth-first search (DFS) visits each node and explores its neighbors.
- How many times: Each node and edge is visited once during DFS traversal.
As the graph grows, the algorithm visits every node and edge once.
| Input Size (n nodes, m edges) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | About 25 visits (nodes + edges) |
| 100 nodes, 200 edges | About 300 visits |
| 1000 nodes, 5000 edges | About 6000 visits |
Pattern observation: The work grows roughly in proportion to the number of nodes plus edges.
Time Complexity: O(n + m)
This means the time grows linearly with the total number of nodes and edges in the graph.
[X] Wrong: "The algorithm checks every pair of nodes, so it must be O(n²)."
[OK] Correct: The DFS only visits each edge and node once, not every pair, so it is more efficient than checking all pairs.
Understanding this linear time complexity helps you explain how graph traversal algorithms efficiently find key points in networks.
"What if the graph was represented as an adjacency matrix instead of adjacency lists? How would the time complexity change?"
Practice
Solution
Step 1: Understand the definition of articulation point
An articulation point is a vertex that, if removed, breaks the graph into more pieces (connected components).Step 2: Compare options with definition
Only A vertex that, when removed, increases the number of connected components matches this definition exactly.Final Answer:
A vertex that, when removed, increases the number of connected components -> Option AQuick Check:
Articulation point = vertex increasing components when removed [OK]
- Confusing articulation point with highest degree vertex
- Thinking articulation point connects to all vertices
- Assuming isolated vertex is articulation point
Solution
Step 1: Identify the graph representation type
An adjacency list is best represented as a Map from node number to array of connected nodes.Step 2: Check function signature compatibility
function findArticulationPoints(graph: Map): number[] uses Map and returns number[] which fits the problem.Final Answer:
function findArticulationPoints(graph: Map<number, number[]>): number[] -> Option DQuick Check:
Adjacency list as Map with number arrays returns number array [OK]
- Using string type for graph input
- Returning void instead of list of articulation points
- Passing graph as a single number
const graph = new Map(); graph.set(0, [1, 2]); graph.set(1, [0, 2, 3]); graph.set(2, [0, 1]); graph.set(3, [1]); const articulationPoints = findArticulationPoints(graph); console.log(articulationPoints);
Solution
Step 1: Analyze the graph structure
The graph has a triangle between 0,1,2 and a leaf node 3 connected to 1.Step 2: Identify articulation points
Removing node 1 disconnects node 3 from the rest, so 1 is an articulation point. Nodes 0 and 2 are part of a cycle, so not articulation points. Node 3 is a leaf, not articulation point.Final Answer:
[1] -> Option CQuick Check:
Only node 1 disconnects graph when removed [OK]
- Thinking nodes in cycle are articulation points
- Including leaf nodes as articulation points
- Ignoring connectivity after removal
function dfs(u: number, parent: number) {
visited[u] = true;
disc[u] = time;
low[u] = time;
time++;
let children = 0;
for (const v of graph.get(u) || []) {
if (!visited[v]) {
children++;
dfs(v, u);
low[u] = Math.min(low[u], low[v]);
if (parent !== -1 && low[v] >= disc[u]) {
articulationPoints.add(u);
}
} else if (v !== parent) {
low[u] = Math.min(low[u], disc[v]);
}
}
if (parent === -1 && children > 1) {
articulationPoints.add(u);
}
}Solution
Step 1: Review DFS variables initialization
The code uses 'visited' array but does not show its initialization before DFS calls.Step 2: Identify missing initialization impact
If 'visited' is not initialized to false for all nodes, DFS may skip nodes or behave incorrectly.Final Answer:
The 'visited' array is not initialized before DFS -> Option AQuick Check:
Uninitialized visited array causes DFS errors [OK]
- Assuming time increments incorrectly
- Misunderstanding root node articulation condition
- Changing condition from >= to > incorrectly
Solution
Step 1: Analyze graph components and cycles
Nodes 0,1,2 form a triangle (cycle). Nodes 3,4,5 form another cycle. Node 6 is connected only to 4.Step 2: Identify articulation points
Removing 1 disconnects node 3 and its cycle from 0,2. Removing 3 disconnects 4,5,6 from 1. Removing 4 disconnects node 6 from rest. So articulation points are 1, 3, and 4.Final Answer:
[1, 3, 4] -> Option BQuick Check:
Nodes connecting cycles and leaf nodes are articulation points [OK]
- Including nodes inside cycles as articulation points
- Missing nodes connecting leaf nodes
- Confusing leaf nodes as articulation points
Ready to prove it?
Pass the test and this pattern moves unknown
