Find bridges or critical connections in a network graph using Tarjan's bridge-finding algorithm. Use a single DFS traversal to compute discovery_time and low_link values where a bridge exists if low_link[child] > discovery_time[parent].
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a Python implementation of Tarjan's algorithm that finds all bridges (critical connections) in an undirected graph using a single DFS traversal with discovery and low link times:
python
def criticalConnections(n, connections):
from collections import defaultdict
adj = defaultdict(list)
for u, v in connections:
adj[u].append(v)
adj[v].append(u)
time = 0
disc = [-1] * n
low = [-1] * n
bridges = []
def dfs(u, parent):
nonlocal time
disc[u] = low[u] = time
time += 1
for v in adj[u]:
if v == parent:
continue
if disc[v] == -1:
dfs(v, u)
low[u] = min(low[u], low[v])
if low[v] > disc[u]:
bridges.append([u, v])
else:
low[u] = min(low[u], disc[v])
for i in range(n):
if disc[i] == -1:
dfs(i, -1)
return bridges
This runs in O(n + m) time for n nodes and m edges, using an adjacency list and DFS to compute discovery times and low links, identifying edges whose removal increases connected components.
A “bridge” (also called a critical connection) is an edge whose removal disconnects the graph. Tarjan’s bridge–finding algorithm computes, in one depth–first traversal, for each vertex (u):
a discovery time disc[u] (the time you first enter the vertex), and
a low–link value low[u] (the minimum discovery time reachable from (u) by following zero or more tree edges and at most one back edge).
For an edge ((u,v)) where (v) is a child of (u) in the DFS tree, ((u,v)) is a bridge if and only if low[v] > disc[u]: there is no back edge from (v) or its descendants that reaches (u) or an ancestor of (u).
Here is a concrete implementation in Python given a list of undirected edges as connections:
python
from collections import defaultdict
def critical_connections(n, connections):
# build an adjacency list
adj = defaultdict(list)
for u, v in connections:
adj[u].append(v)
adj[v].append(u)
disc = [-1] * n # discovery times
low = [-1] * n # low-link values
time = 0
bridges = []
def dfs(u, parent):
nonlocal time
disc[u] = low[u] = time
time += 1
for v in adj[u]:
if v == parent:
continue # don't immediately revisit the edge we came from
if disc[v] == -1:
# Tree edge
dfs(v, u)
# After returning, update low-link value for u
low[u] = min(low[u], low[v])
# Check bridge condition
if low[v] > disc[u]:
bridges.append((u, v))
else:
# Back edge to an ancestor
low[u] = min(low[u], disc[v])
# handle disconnected graphs by looping over all vertices
for i in range(n):
if disc[i] == -1:
dfs(i, -1)
return bridges
# Example usage:
n = 5
edges = [(0, 1), (1, 2), (2, 0), (1, 3), (3, 4)]
print(critical_connections(n, edges))
# Output: [(3, 4), (1, 3)] because removing either disconnects the graph
The key invariant inside dfs is that after exploring a child v, low[v] tells you the earliest discovery time reachable from v. If low[v] is greater than disc[u], then there is no alternate path from v back to u or any of u’s ancestors, so the edge (u,v) is a bridge. The algorithm runs in (O(n + m)) time for (n) vertices and (m) edges.