forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
critical-connections-in-a-network.py
34 lines (32 loc) · 1.18 KB
/
critical-connections-in-a-network.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Time: O(|V| + |E|)
# Space: O(|V| + |E|)
# variant of Tarjan's algorithm (https://www.geeksforgeeks.org/bridge-in-a-graph/)
class Solution(object):
def criticalConnections(self, n, connections):
"""
:type n: int
:type connections: List[List[int]]
:rtype: List[List[int]]
"""
def dfs(edges, parent, u, idx, lowlinks, lookup, result):
if lookup[u]:
return
lookup[u] = True
curr_idx = lowlinks[u] = idx[0]
idx[0] += 1
for v in edges[u]:
if v == parent:
continue
dfs(edges, u, v, idx, lowlinks, lookup, result)
lowlinks[u] = min(lowlinks[u], lowlinks[v])
if lowlinks[v] > curr_idx:
# if any lowlink of neighbors is larger than curr_idx
result.append([u, v])
edges = [[] for _ in xrange(n)]
idx, lowlinks, lookup = [0], [0]*n, [False]*n
result = []
for u, v in connections:
edges[u].append(v)
edges[v].append(u)
dfs(edges, -1, 0, idx, lowlinks, lookup, result)
return result