找出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))
点赞