Fix mypy erros at strongly connected component (#4558)

This commit is contained in:
Hasanul Islam 2021-07-21 11:59:18 +06:00 committed by GitHub
parent bc09ba9abf
commit 7342b33658
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,7 +10,9 @@ test_graph_1 = {0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []}
test_graph_2 = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]} test_graph_2 = {0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]}
def topology_sort(graph: dict, vert: int, visited: list) -> list: def topology_sort(
graph: dict[int, list[int]], vert: int, visited: list[bool]
) -> list[int]:
""" """
Use depth first search to sort graph Use depth first search to sort graph
At this time graph is the same as input At this time graph is the same as input
@ -32,7 +34,9 @@ def topology_sort(graph: dict, vert: int, visited: list) -> list:
return order return order
def find_components(reversed_graph: dict, vert: int, visited: list) -> list: def find_components(
reversed_graph: dict[int, list[int]], vert: int, visited: list[bool]
) -> list[int]:
""" """
Use depth first search to find strongliy connected Use depth first search to find strongliy connected
vertices. Now graph is reversed vertices. Now graph is reversed
@ -52,7 +56,7 @@ def find_components(reversed_graph: dict, vert: int, visited: list) -> list:
return component return component
def strongly_connected_components(graph: dict) -> list: def strongly_connected_components(graph: dict[int, list[int]]) -> list[list[int]]:
""" """
This function takes graph as a parameter This function takes graph as a parameter
and then returns the list of strongly connected components and then returns the list of strongly connected components
@ -63,7 +67,7 @@ def strongly_connected_components(graph: dict) -> list:
""" """
visited = len(graph) * [False] visited = len(graph) * [False]
reversed_graph = {vert: [] for vert in range(len(graph))} reversed_graph: dict[int, list[int]] = {vert: [] for vert in range(len(graph))}
for vert, neighbours in graph.items(): for vert, neighbours in graph.items():
for neighbour in neighbours: for neighbour in neighbours:
@ -84,9 +88,3 @@ def strongly_connected_components(graph: dict) -> list:
components_list.append(component) components_list.append(component)
return components_list return components_list
if __name__ == "__main__":
import doctest
doctest.testmod()