igraph: given a list of vertices, find the smallest connected subgraph -
i have following problem: have relatively large graph , extract connected subgraph given set of vertices, might not directly connected. example:
library(igraph) test <- graph(c("a", "b", "a", "c", "a", "d", "b", "e", "b", "f", "c", "g", "c", "h", "d", "i")) plot(test, layout=layout_as_tree)
now extract (smallest) subgraph contains e.g. vertices "e"
, "c"
, "g"
. there easy way in igraph package? suggestions!
cheers, jo
got it! it's easy igraph:
subnodes <- c("e", "c", "g") neednodes <- character() ## loop through nodes , calculate path each other node for(i in 1:(length(subnodes)-1)){ paths <- shortest_paths(test, from=subnodes[i], to=subnodes[(i+1):length(subnodes)], mode="all") neednodes <- unique(c(neednodes, unlist(lapply(paths$vpath, names)))) } ## subset graph subgr <- induced_subgraph(test, vids=neednodes) ## looks good: plot(subgr, layout=layout_as_tree)
thanks nice igraph package!
cheers, jo
Comments
Post a Comment