Skip to content

Fix mypy errors at strongly connected component #4558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions graphs/strongly_connected_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
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
At this time graph is the same as input
Expand All @@ -32,7 +34,9 @@ def topology_sort(graph: dict, vert: int, visited: list) -> list:
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
vertices. Now graph is reversed
Expand All @@ -52,7 +56,7 @@ def find_components(reversed_graph: dict, vert: int, visited: list) -> list:
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
and then returns the list of strongly connected components
Expand All @@ -63,7 +67,7 @@ def strongly_connected_components(graph: dict) -> list:
"""

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 neighbour in neighbours:
Expand All @@ -84,9 +88,3 @@ def strongly_connected_components(graph: dict) -> list:
components_list.append(component)

return components_list


if __name__ == "__main__":
import doctest

doctest.testmod()