-
Notifications
You must be signed in to change notification settings - Fork 65
/
connected_components.py
43 lines (35 loc) · 1.01 KB
/
connected_components.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
""" Finding Connected Components using BFS Graph Traversal Algorithms
"""
def find_components(graph):
""" breadth first search graph traversal algorithm """
connected_components = []
vertices = list(graph)
while vertices:
start_node = vertices.pop()
queue = [start_node]
visited = [start_node]
while queue:
node = queue.pop(0)
for neighbour in graph[node]:
if neighbour not in visited:
queue.append(neighbour)
visited.append(neighbour)
vertices.remove(neighbour)
connected_components.append(visited)
return connected_components
def main():
""" operational function """
graph = {
'1': ['2', '3'],
'2': ['3', '1'],
'3': ['1', '2', '4'],
'4': ['3'],
'5': ['7', '6'],
'6': ['5'],
'7': ['5'],
'8': [],
'9': [],
}
print(find_components(graph))
if __name__ == "__main__":
main()