10
10
test_graph_2 = {0 : [1 , 2 , 3 ], 1 : [2 ], 2 : [0 ], 3 : [4 ], 4 : [5 ], 5 : [3 ]}
11
11
12
12
13
- def topology_sort (graph : dict , vert : int , visited : list ) -> list :
13
+ def topology_sort (
14
+ graph : dict [int , list [int ]], vert : int , visited : list [bool ]
15
+ ) -> list [int ]:
14
16
"""
15
17
Use depth first search to sort graph
16
18
At this time graph is the same as input
@@ -32,7 +34,9 @@ def topology_sort(graph: dict, vert: int, visited: list) -> list:
32
34
return order
33
35
34
36
35
- def find_components (reversed_graph : dict , vert : int , visited : list ) -> list :
37
+ def find_components (
38
+ reversed_graph : dict [int , list [int ]], vert : int , visited : list [bool ]
39
+ ) -> list [int ]:
36
40
"""
37
41
Use depth first search to find strongliy connected
38
42
vertices. Now graph is reversed
@@ -52,7 +56,7 @@ def find_components(reversed_graph: dict, vert: int, visited: list) -> list:
52
56
return component
53
57
54
58
55
- def strongly_connected_components (graph : dict ) -> list :
59
+ def strongly_connected_components (graph : dict [ int , list [ int ]] ) -> list [ list [ int ]] :
56
60
"""
57
61
This function takes graph as a parameter
58
62
and then returns the list of strongly connected components
@@ -63,7 +67,7 @@ def strongly_connected_components(graph: dict) -> list:
63
67
"""
64
68
65
69
visited = len (graph ) * [False ]
66
- reversed_graph = {vert : [] for vert in range (len (graph ))}
70
+ reversed_graph : dict [ int , list [ int ]] = {vert : [] for vert in range (len (graph ))}
67
71
68
72
for vert , neighbours in graph .items ():
69
73
for neighbour in neighbours :
@@ -84,9 +88,3 @@ def strongly_connected_components(graph: dict) -> list:
84
88
components_list .append (component )
85
89
86
90
return components_list
87
-
88
-
89
- if __name__ == "__main__" :
90
- import doctest
91
-
92
- doctest .testmod ()
0 commit comments