2015-11-28 48 views
4

我正在分析R中的无向图。我试图(最终)编写一个函数来获取最大连接组件的大小(顶点数)与任何随机图的最大双连通分量的大小。我能够提取最大连接组件的大小,但是遇到最大双连接组件的大小问题。我开始了使用上图克的igraph功能biconnected_components:在R中查找双连通组件的大小

bicomponent_list <- biconnected_components(g) 
bicomponent_list$components # lists all of the components, including size and vertex names 
length(bicomponent_list$components[[1]]) # returns number of vertices of first bicomponent 

然后我半生不熟的想法是要在降低顶点数莫名其妙订购此列表,这样我就可以随时调用长度(bicomponent_list $组件[[1]]),它将是最大的双连通组件。但我不知道如何正确分类。也许我必须将其转换为矢量?但我也不知道如何指定我想要向量中的顶点数。有谁知道,或有更好的方法来做到这一点?非常感谢!

library(igraph) 

# generating sample graph 
g1 <- barabasi.game(100, 1, 5) 
V(g1)$name <- as.character(1:100) 

g2 <- erdos.renyi.game(50, graph.density(g1), directed = TRUE) 
V(g2)$name <- as.character(101:200) 

g3 <- graph.union(g1, g2, byname = TRUE) 

# analyzing the bicomponents 
bicomponent_list <- biconnected_components(g3) 
bi_list <- as.list(bicomponent_list$components) 
bi_list <- lapply(bi_list, length) # lists the sizes of all of the components that I want to reorder 

我期望的结果将被订购bi_list这样length(bicomponent_list$components[[1]])回报最顶点的双组份。

+0

,如果你提供了一个最小的[重现性好,将是有益的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)与样本输入数据和该输入的所需结果。一定要包含所有相关的'library()'语句。 – MrFlick

+0

好的,我是这么做的。我希望这更有意义。也许我需要找出一些方法来获取长度列表(biconnected_components $ components [[i]])? – Audrey

回答

1

components属性是一个包含顶点列表的列表。可以遍历并找到他们的,像这样

sapply(bicomponent_list$components, length) 

长度,如果你只是想最大的,包裹在一个max()

max(sapply(bicomponent_list$components, length)) 
+0

太棒了。解决了。谢谢!! – Audrey