R: Extract connected graphs -
i've undirected graph composed of ~300 nodes. graph composed of multiple connected graphs (example below undirected graph composed of 5 connected graph). using r, how can extract each connected sub-graph, , node , edge count. graph represented two-column data.frame:
node1 node2 node1 node3 node2 node5 ...
with example below, expected result be
subgraph nodecount edgecount 1 2 1 2 4 3 3 2 1 4 2 1 5 2 1
thanks
you can this:
library(igraph) set.seed(1) g <- simplify(graph.compose(graph.ring(10), graph.star(5, mode = "undirected"))) + edge("7", "8") dg <- decompose.graph(g) t(sapply(seq_along(dg), function(x) c(subgraph = x, nodecount = vcount(dg[[x]]), edgecount = ecount(dg[[x]])))) # subgraph nodecount edgecount # [1,] 1 7 12 # [2,] 2 2 1 # [3,] 3 1 0
the example graph looks (plot(g)
):
Comments
Post a Comment