MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / compute_ap

Function compute_ap

graphs/articulation_points.py:2–40  ·  view source on GitHub ↗
(graph)

Source from the content-addressed store, hash-verified

1# Finding Articulation Points in Undirected Graph
2def compute_ap(graph):
3 n = len(graph)
4 out_edge_count = 0
5 low = [0] * n
6 visited = [False] * n
7 is_art = [False] * n
8
9 def dfs(root, at, parent, out_edge_count):
10 if parent == root:
11 out_edge_count += 1
12 visited[at] = True
13 low[at] = at
14
15 for to in graph[at]:
16 if to == parent:
17 pass
18 elif not visited[to]:
19 out_edge_count = dfs(root, to, at, out_edge_count)
20 low[at] = min(low[at], low[to])
21
22 # AP found via bridge
23 if at < low[to]:
24 is_art[at] = True
25 # AP found via cycle
26 if at == low[to]:
27 is_art[at] = True
28 else:
29 low[at] = min(low[at], to)
30 return out_edge_count
31
32 for i in range(n):
33 if not visited[i]:
34 out_edge_count = 0
35 out_edge_count = dfs(i, i, -1, out_edge_count)
36 is_art[i] = out_edge_count > 1
37
38 for x in range(len(is_art)):
39 if is_art[x] is True:
40 print(x)
41
42
43# Adjacency list of graph

Callers 1

Calls 1

dfsFunction · 0.70

Tested by

no test coverage detected