You can leverage on igraph
to find the different clusters of networks
library(igraph)
g <- graph_from_data_frame(df, FALSE)
cg <- clusters(g)$membership
df$id3 <- cg[df$id1]
df
output:
id1 id2 id3
1 1 a 1
2 1 b 1
3 2 a 1
4 2 c 1
5 3 c 1
6 3 d 1
7 4 x 2
8 4 y 2
9 5 y 2
10 5 z 2