|
| 1 | +""" |
| 2 | +https://en.wikipedia.org/wiki/Strongly_connected_component |
| 3 | +
|
| 4 | +Finding strongly connected components in directed graph |
| 5 | +
|
| 6 | +""" |
| 7 | + |
| 8 | +test_graph_1 = { |
| 9 | + 0: [2, 3], |
| 10 | + 1: [0], |
| 11 | + 2: [1], |
| 12 | + 3: [4], |
| 13 | + 4: [], |
| 14 | +} |
| 15 | + |
| 16 | +test_graph_2 = { |
| 17 | + 0: [1, 2, 3], |
| 18 | + 1: [2], |
| 19 | + 2: [0], |
| 20 | + 3: [4], |
| 21 | + 4: [5], |
| 22 | + 5: [3], |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +def topology_sort(graph: dict, vert: int, visited: list) -> list: |
| 27 | + """ |
| 28 | + Use depth first search to sort graph |
| 29 | + At this time graph is the same as input |
| 30 | + >>> topology_sort(test_graph_1, 0, 5 * [False]) |
| 31 | + [1, 2, 4, 3, 0] |
| 32 | + >>> topology_sort(test_graph_2, 0, 6 * [False]) |
| 33 | + [2, 1, 5, 4, 3, 0] |
| 34 | + """ |
| 35 | + |
| 36 | + visited[vert] = True |
| 37 | + order = [] |
| 38 | + |
| 39 | + for neighbour in graph[vert]: |
| 40 | + if not visited[neighbour]: |
| 41 | + order += topology_sort(graph, neighbour, visited) |
| 42 | + |
| 43 | + order.append(vert) |
| 44 | + |
| 45 | + return order |
| 46 | + |
| 47 | + |
| 48 | +def find_components(reversed_graph: dict, vert: int, visited: list) -> list: |
| 49 | + """ |
| 50 | + Use depth first search to find strongliy connected |
| 51 | + vertices. Now graph is reversed |
| 52 | + >>> find_components({0: [1], 1: [2], 2: [0]}, 0, 5 * [False]) |
| 53 | + [0, 1, 2] |
| 54 | + >>> find_components({0: [2], 1: [0], 2: [0, 1]}, 0, 6 * [False]) |
| 55 | + [0, 2, 1] |
| 56 | + """ |
| 57 | + |
| 58 | + visited[vert] = True |
| 59 | + component = [vert] |
| 60 | + |
| 61 | + for neighbour in reversed_graph[vert]: |
| 62 | + if not visited[neighbour]: |
| 63 | + component += find_components(reversed_graph, neighbour, visited) |
| 64 | + |
| 65 | + return component |
| 66 | + |
| 67 | + |
| 68 | +def strongly_connected_components(graph: dict) -> list: |
| 69 | + """ |
| 70 | + This function takes graph as a parameter |
| 71 | + and then returns the list of strongly connected components |
| 72 | + >>> strongly_connected_components(test_graph_1) |
| 73 | + [[0, 1, 2], [3], [4]] |
| 74 | + >>> strongly_connected_components(test_graph_2) |
| 75 | + [[0, 2, 1], [3, 5, 4]] |
| 76 | + """ |
| 77 | + |
| 78 | + visited = len(graph) * [False] |
| 79 | + reversed_graph = {vert: [] for vert in range(len(graph))} |
| 80 | + |
| 81 | + for vert, neighbours in graph.items(): |
| 82 | + for neighbour in neighbours: |
| 83 | + reversed_graph[neighbour].append(vert) |
| 84 | + |
| 85 | + order = [] |
| 86 | + for i, was_visited in enumerate(visited): |
| 87 | + if not was_visited: |
| 88 | + order += topology_sort(graph, i, visited) |
| 89 | + |
| 90 | + components_list = [] |
| 91 | + visited = len(graph) * [False] |
| 92 | + |
| 93 | + for i in range(len(graph)): |
| 94 | + vert = order[len(graph) - i - 1] |
| 95 | + if not visited[vert]: |
| 96 | + component = find_components(reversed_graph, vert, visited) |
| 97 | + components_list.append(component) |
| 98 | + |
| 99 | + return components_list |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + import doctest |
| 104 | + |
| 105 | + doctest.testmod() |
0 commit comments