2018-10-19 12:48:28 +00:00
|
|
|
# Finding Articulation Points in Undirected Graph
|
2024-04-01 19:36:41 +00:00
|
|
|
def compute_ap(l):
|
2018-10-19 12:48:28 +00:00
|
|
|
n = len(l)
|
2022-10-12 22:54:20 +00:00
|
|
|
out_edge_count = 0
|
2018-10-19 12:48:28 +00:00
|
|
|
low = [0] * n
|
|
|
|
visited = [False] * n
|
2022-10-12 22:54:20 +00:00
|
|
|
is_art = [False] * n
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
def dfs(root, at, parent, out_edge_count):
|
2018-10-19 12:48:28 +00:00
|
|
|
if parent == root:
|
2022-10-12 22:54:20 +00:00
|
|
|
out_edge_count += 1
|
2018-10-19 12:48:28 +00:00
|
|
|
visited[at] = True
|
|
|
|
low[at] = at
|
|
|
|
|
|
|
|
for to in l[at]:
|
|
|
|
if to == parent:
|
|
|
|
pass
|
|
|
|
elif not visited[to]:
|
2022-10-12 22:54:20 +00:00
|
|
|
out_edge_count = dfs(root, to, at, out_edge_count)
|
2018-10-19 12:48:28 +00:00
|
|
|
low[at] = min(low[at], low[to])
|
|
|
|
|
|
|
|
# AP found via bridge
|
|
|
|
if at < low[to]:
|
2022-10-12 22:54:20 +00:00
|
|
|
is_art[at] = True
|
2018-10-19 12:48:28 +00:00
|
|
|
# AP found via cycle
|
|
|
|
if at == low[to]:
|
2022-10-12 22:54:20 +00:00
|
|
|
is_art[at] = True
|
2018-10-19 12:48:28 +00:00
|
|
|
else:
|
|
|
|
low[at] = min(low[at], to)
|
2022-10-12 22:54:20 +00:00
|
|
|
return out_edge_count
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
for i in range(n):
|
|
|
|
if not visited[i]:
|
2022-10-12 22:54:20 +00:00
|
|
|
out_edge_count = 0
|
|
|
|
out_edge_count = dfs(i, i, -1, out_edge_count)
|
|
|
|
is_art[i] = out_edge_count > 1
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
for x in range(len(is_art)):
|
|
|
|
if is_art[x] is True:
|
2018-10-19 12:48:28 +00:00
|
|
|
print(x)
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
# Adjacency list of graph
|
2020-05-22 06:10:11 +00:00
|
|
|
data = {
|
2019-10-05 05:14:13 +00:00
|
|
|
0: [1, 2],
|
|
|
|
1: [0, 2],
|
|
|
|
2: [0, 1, 3, 5],
|
|
|
|
3: [2, 4],
|
|
|
|
4: [3],
|
|
|
|
5: [2, 6, 8],
|
|
|
|
6: [5, 7],
|
|
|
|
7: [6, 8],
|
|
|
|
8: [5, 7],
|
|
|
|
}
|
2022-10-12 22:54:20 +00:00
|
|
|
compute_ap(data)
|