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]
)
| 11 | |
| 12 | |
| 13 | def 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 | |
| 37 | def find_components( |
no test coverage detected