gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:12:46 25 4
gpt4 key购买 nike

我正在分析 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))

关于r - 在 R 中查找双连通分量的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33966826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com