MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / computeAP

Function computeAP

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

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 1

dfsFunction · 0.70

Tested by

no test coverage detected