I don't write Python, so I don't know if this
networkx
library has a function for finding subgraphs. If not, here's what I would do. The subgraphs will end up being numbered 1...n, where n is the number of nodes in the graph.
for i from 0 to n-1
node[i].subgraph = 0 # subgraph assigned to node i
u = n # number of nodes not yet reached
s = 0 # number of the current subgraph
while u > 0
i = the first node whose node[i].subgraph is 0
s = s + 1
node[i].subgraph = s
# find all nodes that belong to the new subgraph
added = false
repeat
for i from 0 to n-1
if node[i].subgraph is 0
for each node j that is adjacent to node[i]
if node[j].subgraph is s
node[i].subgraph = s
added = true
u = u - 1
until added is false
This can be improved by replacing u with the set of unreached nodes. When a node is assigned to a subgraph, remove it from the set. The algorithm stops when the set is empty.