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

Function strong_connect

graphs/tarjans_scc.py:45–73  ·  view source on GitHub ↗
(v: int, index: int, components: list[list[int]])

Source from the content-addressed store, hash-verified

43 lowlink_of = index_of[:]
44
45 def strong_connect(v: int, index: int, components: list[list[int]]) -> int:
46 index_of[v] = index # the number when this node is seen
47 lowlink_of[v] = index # lowest rank node reachable from here
48 index += 1
49 stack.append(v)
50 on_stack[v] = True
51
52 for w in g[v]:
53 if index_of[w] == -1:
54 index = strong_connect(w, index, components)
55 lowlink_of[v] = (
56 lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v]
57 )
58 elif on_stack[w]:
59 lowlink_of[v] = (
60 lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v]
61 )
62
63 if lowlink_of[v] == index_of[v]:
64 component = []
65 w = stack.pop()
66 on_stack[w] = False
67 component.append(w)
68 while w != v:
69 w = stack.pop()
70 on_stack[w] = False
71 component.append(w)
72 components.append(component)
73 return index
74
75 components: list[list[int]] = []
76 for v in range(n):

Callers 1

tarjanFunction · 0.85

Calls 2

appendMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected