G1 = nx.Graph()
G1.add_edges_from([('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('f', 'g')])
sorted(nx.connected_components(G1), key=len, reverse=True)
How to aggregate matching pairs into "connected components" in Python