Bridges in Graph Tarjan's Algorithm in DSA C - Time & Space Complexity
We want to understand how the time needed to find bridges in a graph grows as the graph gets bigger.
Specifically, how does the algorithm's work change when the number of nodes and edges increases?
Analyze the time complexity of the following code snippet.
void dfs(int u, int parent) {
visited[u] = true;
disc[u] = low[u] = ++time;
for (int v : graph[u]) {
if (!visited[v]) {
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] > disc[u]) {
// (u, v) is a bridge
}
} else if (v != parent) {
low[u] = min(low[u], disc[v]);
}
}
}
This code runs a depth-first search to find bridges by tracking discovery and low times for each node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The DFS visits each node once and checks all its edges.
- How many times: Each edge is checked at most twice (once from each end).
As the number of nodes (n) and edges (m) grow, the algorithm visits each node once and explores all edges connected to it.
| Input Size (n, m) | Approx. Operations |
|---|---|
| 10 nodes, 15 edges | About 25 operations (10 nodes + 15 edges) |
| 100 nodes, 200 edges | About 300 operations (100 + 200) |
| 1000 nodes, 5000 edges | About 6000 operations (1000 + 5000) |
Pattern observation: The work grows roughly in a straight line with the number of nodes plus edges.
Time Complexity: O(n + m)
This means the time needed grows linearly with the total number of nodes and edges in the graph.
[X] Wrong: "The algorithm checks every possible pair of nodes, so it must be O(n²)."
[OK] Correct: The algorithm only visits each edge once or twice, not all pairs of nodes, so it does not do n squared work.
Understanding this linear time complexity helps you explain why Tarjan's algorithm is efficient for large graphs and shows your grasp of graph traversal techniques.
"What if the graph was represented with an adjacency matrix instead of adjacency lists? How would the time complexity change?"
Practice
Solution
Step 1: Understand the definition of a bridge
A bridge is an edge that, when removed, increases the number of connected components in a graph.Step 2: Relate Tarjan's algorithm to bridges
Tarjan's algorithm efficiently finds such edges by using DFS and low-link values.Final Answer:
To find edges that, if removed, increase the number of connected components -> Option BQuick Check:
Bridges = edges increasing components when removed [OK]
- Confusing bridges with shortest paths
- Thinking bridges detect cycles
- Assuming bridges count edges
Solution
Step 1: Recall C array declaration syntax
In C, arrays are declared with type and size like int arr[size];Step 2: Match with discovery time array
So, int disc[MAX]; is correct for fixed size MAX.Final Answer:
int disc[MAX]; -> Option CQuick Check:
C arrays use type name[size] [OK]
- Using Java or C++ style array declarations in C
- Not specifying array size
- Using dynamic allocation syntax incorrectly
void dfs(int u, int parent) {
disc[u] = low[u] = ++time;
for (int v : adj[u]) {
if (disc[v] == -1) {
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] > disc[u])
printf("Bridge: %d - %d\n", u, v);
} else if (v != parent) {
low[u] = min(low[u], disc[v]);
}
}
}
// Graph edges:
// 0-1, 1-2, 2-0, 1-3
// Start dfs(0, -1);Solution
Step 1: Analyze the graph structure
The graph has a triangle 0-1-2 and an edge 1-3. Triangle edges are not bridges because removing them doesn't disconnect the graph.Step 2: Apply Tarjan's condition for bridges
Edge 1-3 is a bridge because removing it disconnects node 3.Final Answer:
Bridge: 1 - 3 -> Option AQuick Check:
Only edge 1-3 disconnects graph [OK]
- Thinking all edges in a cycle are bridges
- Ignoring parent check in DFS
- Misinterpreting low and disc values
void dfs(int u, int parent) {
disc[u] = low[u] = ++time;
for (int v : adj[u]) {
if (disc[v] == -1) {
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= disc[u])
printf("Bridge: %d - %d\n", u, v);
} else if (v != parent) {
low[u] = min(low[u], disc[v]);
}
}
}Solution
Step 1: Understand bridge condition in Tarjan's algorithm
Bridge condition is low[v] > disc[u], meaning no back edge from v or its descendants to u or above.Step 2: Identify the bug in condition
Using >= incorrectly marks edges that are not bridges (like parent edges in cycles).Final Answer:
The condition should be low[v] > disc[u], not >=. -> Option AQuick Check:
Bridge if low[v] > disc[u] [OK]
- Using >= instead of > in bridge condition
- Not initializing discovery times
- Ignoring parent node in DFS
Solution
Step 1: Visualize the graph
Edges: 0-1, 1-2, 2-3, 3-4, 1-4. The edge 1-4 creates a cycle with 1-2-3-4.Step 2: Identify bridges
Removing 0-1 disconnects node 0, so 0-1 is a bridge. Edges 2-3 and 3-4 are in cycle with 1-4, so not bridges.Final Answer:
[0-1] -> Option DQuick Check:
Only 0-1 disconnects graph [OK]
- Marking cycle edges as bridges
- Ignoring connectivity after edge removal
- Confusing bridge with articulation point
Ready to prove it?
Pass the test and this pattern moves unknown
