Articulation Points in Graph in DSA Java - Time & Space Complexity
We want to understand how the time needed to find articulation points changes as the graph grows.
How does the algorithm's work increase when the graph has more nodes and edges?
Analyze the time complexity of the following code snippet.
void dfs(int u, int parent) {
visited[u] = true;
disc[u] = low[u] = ++time;
int children = 0;
for (int v : graph[u]) {
if (!visited[v]) {
children++;
dfs(v, u);
low[u] = Math.min(low[u], low[v]);
if (parent == -1 && children > 1) articulation[u] = true;
if (parent != -1 && low[v] >= disc[u]) articulation[u] = true;
} else if (v != parent) {
low[u] = Math.min(low[u], disc[v]);
}
}
}
This code finds articulation points using DFS, tracking discovery and low times for each node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Depth-first search (DFS) visits each node and its edges.
- How many times: Each node is visited once, and each edge is checked once or twice.
As the graph grows, the algorithm visits all nodes and edges once.
| Input Size (n nodes, m edges) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | About 25 operations |
| 100 nodes, 200 edges | About 300 operations |
| 1000 nodes, 5000 edges | About 6000 operations |
Pattern observation: Operations grow roughly in proportion to nodes plus edges.
Time Complexity: O(n + m)
This means the time grows linearly with the number of nodes and edges in the graph.
[X] Wrong: "The algorithm checks every possible path, so it must be exponential."
[OK] Correct: The DFS visits each node and edge only once, avoiding repeated work, so it runs in linear time.
Understanding this linear time complexity helps you explain how graph traversal algorithms efficiently find key points in networks.
"What if the graph is disconnected and we run DFS from every node? 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 which, when removed, increases the number of connected components matches this definition exactly.Final Answer:
A vertex which, when removed, increases the number of connected components -> Option DQuick Check:
Articulation point = vertex breaking connectivity [OK]
- Confusing articulation point with highest degree vertex
- Thinking articulation point forms cycles
- Assuming articulation point connects to all vertices
Solution
Step 1: Understand adjacency list structure
An adjacency list for a graph is usually a list of lists, where each inner list holds neighbors of a vertex.Step 2: Check Java syntax for adjacency list
ArrayList graph = new ArrayList<>(); correctly declares an ArrayList of ArrayLists of Integers, suitable for adjacency list.Final Answer:
ArrayList graph = new ArrayList<>(); -> Option CQuick Check:
Adjacency list = list of lists [OK]
- Using 2D array without size initialization
- Using HashMap with wrong value type
- Trying to instantiate interface List directly
int V = 4; ArrayList graph = new ArrayList<>(); for (int i = 0; i < V; i++) graph.add(new ArrayList<>()); graph.get(0).add(1); graph.get(1).add(0); graph.get(1).add(2); graph.get(2).add(1); graph.get(1).add(3); graph.get(3).add(1); // Articulation points found: 1
Solution
Step 1: Analyze the graph structure
The graph has vertices 0,1,2,3 with edges 0-1, 1-2, 1-3. Vertex 1 connects to all others.Step 2: Identify articulation points
Removing vertex 1 disconnects the graph into three parts, so 1 is an articulation point. Others are not.Final Answer:
[1] -> Option BQuick Check:
Only vertex 1 breaks connectivity [OK]
- Assuming leaf nodes are articulation points
- Including vertices with degree 1 as articulation points
- Ignoring connectivity after removal
void dfs(int u, int parent) {
visited[u] = true;
disc[u] = low[u] = time++;
int children = 0;
for (int v : 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])
articulation[u] = true;
} else if (v != parent) {
low[u] = Math.min(low[u], disc[v]);
}
}
if (parent == -1 && children > 1)
articulation[u] = true;
}Solution
Step 1: Check initialization of time variable
The variable 'time' is used to assign discovery times but must be initialized before DFS starts.Step 2: Verify other parts of DFS
Conditions for root and back edges are correct; visited[u] is marked true properly.Final Answer:
The time variable is not initialized before DFS -> Option AQuick Check:
Uninitialized time causes wrong discovery times [OK]
- Forgetting to initialize time before DFS
- Misusing parent check for root node
- Not updating low values for back edges
Solution
Step 1: Analyze the graph structure
Vertices 0,1,2 form a triangle (cycle). Edges 1-3 and 3-4 form a chain attached to vertex 1.Step 2: Identify articulation points
Removing vertex 1 disconnects 3 and 4 from the cycle, so 1 is an articulation point. Removing 3 disconnects 4, so 3 is also an articulation point. Vertices 0 and 2 are in a cycle, so not articulation points.Final Answer:
[1, 3] -> Option AQuick Check:
Vertices breaking connectivity: 1 and 3 [OK]
- Assuming cycle vertices are articulation points
- Missing vertex 3 as articulation point
- Only considering vertex 1 as articulation point
Ready to prove it?
Pass the test and this pattern moves unknown
