Finding islands in a networkx graph

I am looking for a function within the networkx module which will list out all the islands in my graph. Having searched for a while, I can’t seem to find anything.

These are the islands which I am looking to list

Answer

Using nx.connected_components(), you can draw and print your desired “islands” as follows:

import networkx as nx
from matplotlib import pyplot as plt
G = nx.Graph()
G.add_edges_from([(1,2),(2,3),(1,3),(1,4),(5,6),(6,7),(7,8),(8,5),(9,10),(10,11)])
for i, c in enumerate(nx.connected_components(G)):
    print(f"Island {i+1}: {c}")
nx.draw(G, with_labels=True)
plt.show()

#output
Island 1: {1, 2, 3, 4}
Island 2: {8, 5, 6, 7}
Island 3: {9, 10, 11}

enter image description here