r - 创建分层网络

标签 r igraph

在 R 中有一种方法可以创建这样的分层网络吗?

enter image description here

也就是说,在它生成 5 个节点的集线器之前,其他 4 个集线器连接到该集线器,依此类推,直到获得通用网络。

enter image description here

我想我可以使用make_tree igraph 函数,但我不知道如何迭代添加集线器。 这就是我所做的。

net <- make_tree(n = 5, children = 4, mode = "undirected")
plot(net)

enter image description here

谢谢

最佳答案

不确定这有多优雅。要使图表看起来像您的图表需要做更多的工作,但我认为图表是正确的。

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/

相关文章:

r - 如何从图中删除顶点并在其邻居之间创建边?

r - 在 r 中使用 "/"分隔符将列分成两部分

r - ggplot没有绘制正确的颜色

r - R 中的 tile/valueBox 可视化将保存为 png

r - 在 R 中向 igraph 添加文本

r - 如何从图中获取从节点到节点的边权重之和

r - 将自定义颜色渐变映射到 POSIXct 值

r - 从 R 中的 tibble 创建列表列表(tidyverse)

python - 在Python中将元组转换为字典

r - 使用 R 从 GTFS 数据创建 iGraph 图