我有以下问题:我有一个相对较大的图形,并且想要在给定一组顶点的情况下提取连接的子图,这些顶点可能没有直接连接。例如:
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)
现在我想提取包含例如的(最小的)子图。顶点"e"
,"c"
和"g"
。
在igraph包中有没有简单的方法呢?
谢谢你的任何建议!
答案 0 :(得分:0)
知道了!用igraph很容易:
subnodes <- c("e", "c", "g")
needNodes <- character()
## loop through all nodes and calculate the path to 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 the graph
subGr <- induced_subgraph(Test, vids=needNodes)
## looks good:
plot(subGr, layout=layout_as_tree)
感谢好的igraph包!
欢呼,乔