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

Function connected_components

graphs/connected_components.py:33–52  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

31
32
33def 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
55if __name__ == "__main__":

Callers

nothing calls this directly

Calls 2

dfsFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected