Articulation Points in Graph in DSA Go - 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 number of nodes and edges increases?
Analyze the time complexity of the following code snippet.
func APUtil(u int, visited []bool, disc []int, low []int, parent []int, ap []bool, graph [][]int, time *int) {
visited[u] = true
*time++
disc[u] = *time
low[u] = *time
children := 0
for _, v := range graph[u] {
if !visited[v] {
children++
parent[v] = u
APUtil(v, visited, disc, low, parent, ap, graph, time)
if low[v] >= disc[u] {
ap[u] = true
}
if low[v] < low[u] {
low[u] = low[v]
}
} else if v != parent[u] {
if disc[v] < low[u] {
low[u] = disc[v]
}
}
}
}
This code finds articulation points using a depth-first search and tracks discovery and low times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Depth-first search (DFS) traversal of the graph.
- How many times: Each node and edge is visited once during DFS.
As the number of nodes (n) and edges (e) grow, the DFS visits each node and edge once.
| Input Size (n, e) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | ~25 operations (visits) |
| 100 nodes, 200 edges | ~300 operations |
| 1000 nodes, 3000 edges | ~4000 operations |
Pattern observation: The work grows roughly in proportion to the sum of nodes and edges.
Time Complexity: O(n + e)
This means the algorithm's time grows linearly with the number of nodes plus edges in the graph.
[X] Wrong: "The algorithm checks every pair of nodes, so it must be O(n²)."
[OK] Correct: The algorithm uses DFS to visit each node and edge only once, not every pair, so it is faster than O(n²).
Understanding this linear time complexity helps you explain how graph traversal algorithms efficiently find critical points in networks.
"What if the graph was represented using 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 BQuick Check:
Articulation point = vertex increasing components when removed [OK]
- Confusing articulation point with highest degree vertex
- Thinking articulation point is a vertex with no edges
- Mixing articulation point with cycle vertices
Solution
Step 1: Identify correct data structure for adjacency list
An adjacency list maps each vertex to a list of connected vertices, so map[int][]int is correct.Step 2: Check other options
graph := make([][]int, n) is a slice of slices, which can work but usually fixed size; graph := make(map[int]int) is map[int]int (single int), not list; graph := make([]int, n) is slice of ints, not adjacency list.Final Answer:
graph := make(map[int][]int) -> Option AQuick Check:
Adjacency list = map vertex to list of neighbors [OK]
- Using map[int]int instead of map[int][]int
- Using slice of ints instead of slice of slices or map
- Confusing slice initialization with map initialization
package main
import "fmt"
func main() {
n := 4
graph := make(map[int][]int)
graph[0] = []int{1}
graph[1] = []int{0, 2, 3}
graph[2] = []int{1}
graph[3] = []int{1}
articulationPoints := []int{}
// Assume articulation point detection returns [1]
articulationPoints = append(articulationPoints, 1)
fmt.Println(articulationPoints)
}Solution
Step 1: Analyze the graph structure
Vertices 0,1,2,3 with edges 0-1, 1-2, 1-3 form a star with center 1.Step 2: Identify articulation points
Removing vertex 1 disconnects the graph, so 1 is an articulation point; others are not.Final Answer:
[1] -> Option CQuick Check:
Center vertex in star graph is articulation point [OK]
- Including leaf nodes as articulation points
- Forgetting that removing center disconnects graph
- Assuming multiple articulation points without checking connectivity
func dfs(u, parent int, graph map[int][]int, visited []bool, disc []int, low []int, time *int, ap []bool) {
visited[u] = true
disc[u] = *time
low[u] = *time
*time = *time + 1
children := 0
for _, v := range graph[u] {
if !visited[v] {
children++
dfs(v, u, graph, visited, disc, low, time, ap)
low[u] = min(low[u], low[v])
if parent != -1 && low[v] >= disc[u] {
ap[u] = true
}
} else if v != parent {
low[u] = min(low[u], disc[v])
}
}
if parent == -1 && children > 1 {
ap[u] = true
}
}
What is the error?Solution
Step 1: Check pointer increment syntax
The line '*time++' is invalid in Go; pointer dereference and increment must be done as '*time = *time + 1'.Step 2: Verify other parts
Visited array initialization, if conditions, and DFS base case are correct.Final Answer:
Incorrect increment syntax for time pointer (*time++) -> Option DQuick Check:
Pointer increment in Go uses '*time = *time + 1' [OK]
- Using '*time++' which is invalid in Go
- Not initializing visited slice before DFS
- Confusing parent check condition
- Missing DFS base case for recursion
Solution
Step 1: Analyze the graph structure
Vertices 0,1,2 form a triangle (cycle), so none are articulation points there. Vertices 3,4,5 form another cycle. Vertex 1 connects these two parts.Step 2: Identify articulation points
Removing vertex 1 disconnects {0,2} from {3,4,5}. Removing vertex 3 disconnects {0,1,2} from {4,5}. Removing vertex 4 does not disconnect since 5 connects back to 3.Final Answer:
[1,3] -> Option AQuick Check:
Vertices connecting cycles or bridges are articulation points [OK]
- Including vertices inside cycles as articulation points
- Missing vertex 3 as articulation point
- Assuming all cycle vertices are articulation points
Ready to prove it?
Pass the test and this pattern moves unknown
