在 R 中查找双连通分量的大小

Finding the size of biconnected components in R

我正在分析 R 中的无向图。我正在尝试(最终)编写一个函数来获取最大连通分量的大小(顶点数)与最大双连通分量的大小之比- 任何随机图。我能够提取最大连通分量的大小,但在处理最大双连通分量的大小时遇到​​了问题。我开始在图 g:

上使用 igraph 函数 biconnected_components
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

Then my half-baked idea was to somehow order this list in decreasing number of vertices, so that I can always call length(bicomponent_list$components[[1]]) and it will be the largest biconnected component. But I don't know how to sort this correctly. Perhaps I have to convert it to a vector? But I also don't know how to specify that I want the number of vertices in the vector. Does anyone know, or have a better way to do it? Thanks so much!

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

My desired outcome would be ordering bi_list such that length(bicomponent_list$components[[1]]) returns the bicomponent with the most vertices.

components 属性 是一个包含顶点列表的列表。您可以遍历它们并找到它们的长度

sapply(bicomponent_list$components, length)

如果您只想要最大的,请将其包装在 max()

max(sapply(bicomponent_list$components, length))