MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / topology_sort

Function topology_sort

graphs/strongly_connected_components.py:13–34  ·  view source on GitHub ↗

Use depth first search to sort graph At this time graph is the same as input >>> topology_sort(test_graph_1, 0, 5 * [False]) [1, 2, 4, 3, 0] >>> topology_sort(test_graph_2, 0, 6 * [False]) [2, 1, 5, 4, 3, 0]

(
    graph: dict[int, list[int]], vert: int, visited: list[bool]
)

Source from the content-addressed store, hash-verified

11
12
13def topology_sort(
14 graph: dict[int, list[int]], vert: int, visited: list[bool]
15) -> list[int]:
16 """
17 Use depth first search to sort graph
18 At this time graph is the same as input
19 >>> topology_sort(test_graph_1, 0, 5 * [False])
20 [1, 2, 4, 3, 0]
21 >>> topology_sort(test_graph_2, 0, 6 * [False])
22 [2, 1, 5, 4, 3, 0]
23 """
24
25 visited[vert] = True
26 order = []
27
28 for neighbour in graph[vert]:
29 if not visited[neighbour]:
30 order += topology_sort(graph, neighbour, visited)
31
32 order.append(vert)
33
34 return order
35
36
37def find_components(

Callers 1

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected