📖 Scenario: You are working as a network engineer. You want to find critical connections in a network. These connections, if removed, will split the network into two or more parts. Such connections are called bridges.We will use Tarjan's algorithm to find all bridges in a network represented as an undirected graph.
🎯 Goal: Build a Python program that finds all bridges in a given undirected graph using Tarjan's algorithm.
📋 What You'll Learn
Create a graph using adjacency list representation
Use Tarjan's algorithm with discovery and low time arrays
Identify and store all bridges in the graph
Print the list of bridges as pairs of nodes
💡 Why This Matters
🌍 Real World
Network engineers use bridge detection to find critical connections that, if broken, can disrupt communication.
💼 Career
Understanding Tarjan's algorithm and graph traversal is important for roles in network security, infrastructure, and software engineering.
Progress0 / 4 steps
1
Create the graph as an adjacency list
Create a dictionary called graph representing the undirected graph with these exact edges: 0-1, 1-2, 2-0, 1-3, 3-4. Use adjacency list format where each key is a node and the value is a list of connected nodes.
DSA Python
Hint
Use a dictionary where keys are node numbers and values are lists of connected nodes.
2
Initialize helper variables for Tarjan's algorithm
Create these variables: time set to 0, discovery as a dictionary with all nodes from graph set to -1, low as a dictionary with all nodes set to -1, and an empty list called bridges.
DSA Python
Hint
Use dictionary comprehensions to initialize discovery and low with -1 for each node.
3
Implement the DFS function for Tarjan's algorithm
Define a function called dfs with parameters u and parent. Inside it, use the global time. Set discovery[u] and low[u] to time, then increment time. For each v in graph[u], if discovery[v] is -1, recursively call dfs(v, u), update low[u] to the minimum of low[u] and low[v]. If low[v] is greater than discovery[u], append the tuple (u, v) to bridges. Else if v is not parent, update low[u] to the minimum of low[u] and discovery[v].
DSA Python
Hint
Follow the Tarjan's algorithm steps carefully inside the DFS function.
4
Run DFS and print the bridges
Use a for loop to iterate over each node u in graph. If discovery[u] is -1, call dfs(u, -1). Then print the bridges list.
DSA Python
Hint
Run DFS from each unvisited node and then print the list of bridges.
Practice
(1/5)
1. What is the main purpose of Tarjan's algorithm when finding bridges in a graph?
easy
A. To find the shortest path between two nodes
B. To find edges that, if removed, increase the number of connected components
C. To detect cycles in a graph
D. To count the total number of nodes in the graph
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: Purpose of Tarjan's algorithm for bridges
Tarjan's algorithm efficiently finds all such edges (bridges) in a graph using DFS and low-link values.
Final Answer:
To find edges that, if removed, increase the number of connected components -> Option B
Quick Check:
Bridges = edges increasing components when removed [OK]
Hint: Bridges disconnect graph if removed [OK]
Common Mistakes:
Confusing bridges with cycles
Thinking it finds shortest paths
Assuming it counts nodes
2. Which of the following is the correct way to initialize the discovery time and low arrays in Tarjan's algorithm in Python?
easy
A. discovery = [-1] * n; low = [-1] * n
B. discovery = [0] * n; low = [0] * n
C. discovery = list(range(n)); low = list(range(n))
D. discovery = None; low = None
Solution
Step 1: Understand initialization purpose
Discovery and low arrays store times; initializing with -1 means unvisited nodes.
Step 2: Choose correct initialization
Using [-1] * n marks all nodes as unvisited initially, which is standard practice.
Final Answer:
discovery = [-1] * n; low = [-1] * n -> Option A
Quick Check:
Unvisited nodes marked with -1 [OK]
Hint: Use -1 to mark unvisited nodes [OK]
Common Mistakes:
Initializing with zeros (means visited)
Using range values (incorrect meaning)
Setting to None (causes errors)
3. What will be the output bridges for the following undirected graph using Tarjan's algorithm?
Nodes 0,1,2 form a triangle (cycle), so no bridges there. Edges (2,3) and (3,4) connect to nodes outside the cycle.
Step 2: Identify bridges
Removing edge (2,3) or (3,4) disconnects the graph, so these are bridges.
Final Answer:
[(2, 3), (3, 4)] -> Option C
Quick Check:
Edges breaking connectivity = bridges [OK]
Hint: Edges outside cycles are likely bridges [OK]
Common Mistakes:
Marking cycle edges as bridges
Missing edges connecting components
Confusing node pairs order
4. Identify the bug in this Tarjan's algorithm snippet for finding bridges:
def dfs(u, parent):
visited[u] = True
disc[u] = time
low[u] = time
time += 1
for v in graph[u]:
if not visited[v]:
dfs(v, u)
low[u] = min(low[u], low[v])
if low[v] > disc[u]:
bridges.append((u, v))
elif v != parent:
low[u] = min(low[u], disc[v])
medium
A. visited array is not updated correctly
B. The condition low[v] > disc[u] should be low[v] >= disc[u]
C. The parent check should be if v == parent, not v != parent
D. time variable is not declared global or passed, causing UnboundLocalError
Solution
Step 1: Check variable usage
The variable 'time' is incremented inside dfs but not declared global or passed, causing an error.
Step 2: Understand Python variable scope
Without 'global time' or passing time as argument, Python treats 'time' as local, causing UnboundLocalError.
Final Answer:
time variable is not declared global or passed, causing UnboundLocalError -> Option D
Quick Check:
Modify time scope to fix error [OK]
Hint: Declare time global or pass as argument [OK]