📖 Scenario: Imagine a city's road network represented as a graph. Each intersection is a node, and roads are edges connecting them. Some intersections are critical: if closed, they split the city into disconnected parts. These are called articulation points.
🎯 Goal: You will build a Java program to find all articulation points in a given undirected graph. This helps identify critical intersections in the city network.
📋 What You'll Learn
Create a graph using adjacency lists with 5 nodes and specified edges
Add variables to track discovery times, low values, and visited nodes
Implement a Depth-First Search (DFS) to find articulation points using Tarjan's algorithm
Print all articulation points found in the graph
💡 Why This Matters
🌍 Real World
Finding articulation points helps city planners identify critical intersections that, if blocked, disrupt traffic flow.
💼 Career
Understanding graph algorithms like articulation points is important for software engineers working on network reliability, infrastructure planning, and cybersecurity.
Progress0 / 4 steps
1
Create the graph with adjacency lists
Create an integer variable V and set it to 5 for the number of nodes. Then create an adjacency list array called graph of type ArrayList<Integer>[] with size V. Initialize each element of graph as a new ArrayList<Integer>(). Add edges to graph to represent these connections: 0-1, 0-2, 1-2, 1-3, 3-4.
DSA Java
Hint
Use a loop to initialize each element of graph as a new ArrayList<Integer>(). Then add edges in both directions because the graph is undirected.
2
Add variables for DFS tracking
Add integer arrays disc and low of size V to store discovery times and low values. Add a boolean array visited of size V to track visited nodes. Add a boolean array ap of size V to mark articulation points. Also, create an integer variable time and set it to 0.
DSA Java
Hint
Initialize arrays with size V. The time variable starts at zero to count discovery order.
3
Implement DFS to find articulation points
Write a static method dfs with parameters int u, int parent, int[] disc, int[] low, boolean[] visited, boolean[] ap, ArrayList<Integer>[] graph, and an integer array time of size 1 to track discovery time. In main, call dfs(0, -1, disc, low, visited, ap, graph, new int[]{0}). Implement the DFS logic to update disc, low, and mark articulation points in ap using Tarjan's algorithm.
DSA Java
Hint
Use Tarjan's algorithm rules: update discovery and low times, count children, and mark articulation points when conditions meet.
4
Print all articulation points
Add a for loop in main to iterate over all nodes from 0 to V-1. Inside the loop, print the node number if ap[node] is true. Use System.out.print(node + " ") to print all articulation points on one line separated by spaces.
DSA Java
Hint
Loop through ap array and print the index if ap[index] is true. This shows all articulation points.
Practice
(1/5)
1. What is an articulation point in a graph?
easy
A. A vertex that forms a cycle in the graph
B. A vertex with the highest degree in the graph
C. A vertex connected to all other vertices
D. A vertex which, when removed, increases the number of connected components
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 D
Quick Check:
Articulation point = vertex breaking connectivity [OK]
Hint: Articulation point breaks graph connectivity when removed [OK]
Common Mistakes:
Confusing articulation point with highest degree vertex
Thinking articulation point forms cycles
Assuming articulation point connects to all vertices
2. Which of the following is the correct way to declare a graph adjacency list in Java for articulation point detection?
easy
A. int graph[][] = new int[];
B. HashMap graph = new HashMap<>();
C. ArrayList graph = new ArrayList<>();
D. List<Integer> graph = new List<>();
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 C
Quick Check:
Adjacency list = list of lists [OK]
Hint: Adjacency list uses list of lists in Java [OK]
Common Mistakes:
Using 2D array without size initialization
Using HashMap with wrong value type
Trying to instantiate interface List directly
3. Consider the following Java code snippet for articulation point detection. What will be the output when the graph has edges: 0-1, 1-2, 1-3?
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
medium
A. []
B. [1]
C. [2, 3]
D. [0, 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 B
Quick Check:
Only vertex 1 breaks connectivity [OK]
Hint: Check which vertex removal splits graph [OK]
Common Mistakes:
Assuming leaf nodes are articulation points
Including vertices with degree 1 as articulation points
Ignoring connectivity after removal
4. The following Java code snippet is intended to find articulation points but has a bug. What is the error?
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;
}
medium
A. The time variable is not initialized before DFS
B. The condition for root articulation point is incorrect
C. The low array is not updated correctly for back edges
D. The visited array is not marked true for vertex u
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 A
Quick Check:
Uninitialized time causes wrong discovery times [OK]
Hint: Initialize time counter before DFS call [OK]
Common Mistakes:
Forgetting to initialize time before DFS
Misusing parent check for root node
Not updating low values for back edges
5. Given a connected undirected graph with 5 vertices and edges: 0-1, 1-2, 2-0, 1-3, 3-4, which vertices are articulation points?
hard
A. [1, 3]
B. [0, 2]
C. [3]
D. [1]
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 A
Quick Check:
Vertices breaking connectivity: 1 and 3 [OK]
Hint: Check vertices whose removal splits graph into parts [OK]
Common Mistakes:
Assuming cycle vertices are articulation points
Missing vertex 3 as articulation point
Only considering vertex 1 as articulation point
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.