What is a Graph? · Definitions

Lesson 7

Nikolai Chukhin · Alexander S. Kulikov

It has two connected components: \(\{5, 1, 2\}\) and \(\{3, 4, 8, 7, 6\}\).

from networkx import connected_components, Graph

edges = [(1, 2), (3, 8), (4, 6), (3, 6), (2, 5), (3, 7), (4, 8), (5, 1)]
print(list(connected_components(Graph(edges))))

[{1, 2, 5}, {3, 4, 6, 7, 8}]