【问题标题】:igraph: given a list of vertices, find the smallest connected subgraphigraph:给定一个顶点列表,找到最小的连通子图
【发布时间】:2015-07-30 08:00:19
【问题描述】:

我有以下问题:我有一个相对较大的图,并且想在给定一组顶点的情况下提取一个连通子图,这可能不是直接连接的。示例:

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 包中是否有一种简单的方法可以做到这一点? 感谢您的任何建议!

干杯,乔

【问题讨论】:

  • 最小的连通子图是单个顶点,不是吗?
  • 也许我不清楚,我的意思是包含所有顶点的子图(即"e""c""g" 以及连接它们的所有其他顶点)。

标签: igraph


【解决方案1】:

知道了!使用 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 包!

干杯,乔

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多