在 R 中有一种方法可以创建这样的分层网络吗?
也就是说,在它生成 5 个节点的集线器之前,其他 4 个集线器连接到该集线器,依此类推,直到获得通用网络。
我想我可以使用make_tree
igraph 函数,但我不知道如何迭代添加集线器。
这就是我所做的。
net <- make_tree(n = 5, children = 4, mode = "undirected")
plot(net)
谢谢
最佳答案
不确定这有多优雅。要使图表看起来像您的图表需要做更多的工作,但我认为图表是正确的。
library(igraph)
#' This adds the gadd graph to the main graph, g, and wires all of its vertices
#' to the central vertex of g
attach_to_center <- function(g, gadd) {
g <- g + gadd + edges(as.vector(rbind(1, gorder(g) + 1:gorder(gadd))))
}
nIter <- 2
nChild <- 4
# The initial graph
g <- make_empty_graph(5, directed = FALSE) + edges(1,2,1,3,1,4,1,5,2,3,3,4,4,5,5,2)
for (j in 1:nIter) {
g0 <- g
for (i in 1:nChild) {
g <- attach_to_center(g, g0)
}
}
plot(g, vertex.label = NA)
它看起来足够快(尽管绘图会失控超过 3 或 4 次迭代)。
关于r - 创建分层网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36837101/