Last active
May 18, 2022 19:43
-
-
Save Kiruha01/759eb48a6152411b5a456107d51b14c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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