Skip to content

Instantly share code, notes, and snippets.

@Kiruha01
Last active May 18, 2022 19:43
Show Gist options
  • Save Kiruha01/759eb48a6152411b5a456107d51b14c5 to your computer and use it in GitHub Desktop.
Save Kiruha01/759eb48a6152411b5a456107d51b14c5 to your computer and use it in GitHub Desktop.
def weak_conns(graph: BaseGraph) -> List[List[int]]:
"""Поиск компонент слабой связности"""
unvisited = copy.deepcopy(graph.get_all_vertices())
comps = []
while unvisited:
start = unvisited.pop()
comps.append([])
stack = deque([start])
while stack:
v = stack[-1]
unvisited.discard(v)
for out_edge in graph.outgoing_adj_list[v]:
if out_edge.end in unvisited:
stack.append(out_edge.end)
break
else:
for in_edge in graph.incoming_adj_list[v]:
if in_edge.start in unvisited:
stack.append(in_edge.start)
break
else:
stack.pop()
comps[-1].append(v)
return comps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment