minoo minoo - 1 year ago 51
R Question

converting components of a graph to independent graphs

I have a unconnected graph and I want to convert each component to a independent graph. This is an example, I have wrote which I couldn't reach to the result:

gr<- graph( edges=c(1,2, 2,3,3, 1,4, 5), n=5, directed=F )
is.connected(gr)

cl <- clusters(gr)
f<-induced.subgraph(gr1,which(cl$membership == which.max(cl$csize)))
sapply(gr, function(f) f)


Any idea to it?

Answer Source
gr <- graph( edges = c(1,2, 2,3, 3,1, 4,5), n = 5, directed = F)
is.connected(gr)

cl <- clusters(gr)

graph_splitting <- function(k, gr, cl = clusters(gr)){
    induced.subgraph(gr, cl$membership == k)
}

sapply(1:max(cl$membership), graph_splitting, gr = gr, cl = cl, simplify = FALSE)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download