This function takes graph as a parameter and then returns the list of connected components >>> connected_components(test_graph_1) [[0, 1, 3, 2], [4, 5, 6]] >>> connected_components(test_graph_2) [[0, 1, 3, 2], [4], [5]]
(graph: dict)
| 31 | |
| 32 | |
| 33 | def connected_components(graph: dict) -> list: |
| 34 | """ |
| 35 | This function takes graph as a parameter |
| 36 | and then returns the list of connected components |
| 37 | >>> connected_components(test_graph_1) |
| 38 | [[0, 1, 3, 2], [4, 5, 6]] |
| 39 | >>> connected_components(test_graph_2) |
| 40 | [[0, 1, 3, 2], [4], [5]] |
| 41 | """ |
| 42 | |
| 43 | graph_size = len(graph) |
| 44 | visited = graph_size * [False] |
| 45 | components_list = [] |
| 46 | |
| 47 | for i in range(graph_size): |
| 48 | if not visited[i]: |
| 49 | i_connected = dfs(graph, i, visited) |
| 50 | components_list.append(i_connected) |
| 51 | |
| 52 | return components_list |
| 53 | |
| 54 | |
| 55 | if __name__ == "__main__": |