2015-11-28 46 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