如何根据组件节点的最大程度在图的组件之间添加边

How to add an edge between components of a graph based on the maximum degree of a node of the component

我有一个图表,其中包含 4 个不同簇大小的组件。

我可以使用下面的代码查看详细信息

cl <- components(graph1)

详情是这样的

$membership
ID_00104 ID_00136 ID_00169 ID_00178 ID_00180 ID_06663 ID_06791 ID_09099 ID_00910 ID_00790 ID_01013 ID_01130 ID_01260 ID_00394 ID_00860 ID_00959 ID_01222 ID_00288 ID_00324 ID_00663 ID_00846 ID_01047 ID_06781 ID_06786 
       1        2        2        3        4        1        1        1        2        3        4        4        4        4        4        4        4        4        4        4        4        4        4        4 

$csize
[1]  4  3  2 15

$no
[1] 4

我还可以使用下面的代码

获取节点的数量degree
degree(graph1)

输出为

ID_00104 ID_00136 ID_00169 ID_00178 ID_00180 ID_06663 ID_06791 ID_09099 ID_00910 ID_00790 ID_01013 ID_01130 ID_01260 ID_00394 ID_00860 ID_00959 ID_01222 ID_00288 ID_00324 ID_00663 ID_00846 ID_01047 ID_06781 ID_06786 
       3        2        2        1       14        1        1        1        2        1        1        1        1        1        1        1        1        1        1        1        1        1        1        1 

我可以使用以下代码添加所有组件(从 2 个组件中随机选择 2 个节点)(来自我之前 之一的解决方案)

graph1 <-  graph_from_data_frame(g, directed = FALSE)
E(graph1)$weight <- g$values
cl <- components(graph1)
graph2 <- with(
  stack(membership(cl)),
  add.edges(
    graph1,
    c(combn(sapply(split(ind, values), sample, size = 1), 2)),
    weight = 0.01))

现在,我想在 degree 数量最多的那些 nodes 之间添加一个 edge,例如:ID_00180 的度数为 14(附加图像的左侧组件)并且 ID_00104 的度数为 3(附加图像的顶部组件)。在组合这两个组件时,我想在 ID_00180ID_00104 之间添加一条边(而不是随机取).

如果任何组件具有多个相同数量的最高度数,例如:所附图像的右下角组件(所有节点的度数均为 2),那么我们可以从最高度数节点中随机选取任何一个。比如说,我们可以在 ID_00180any nodes

之间添加一条边

可重现的数据

g <- structure(list(query = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 4L, 
                                        5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("ID_00104", 
                                                                                                            "ID_00136", "ID_00169", "ID_00178", "ID_00180"), class = "factor"), 
                    target = structure(c(16L, 19L, 20L, 1L, 9L, 9L, 6L, 11L, 
                                         13L, 15L, 4L, 8L, 10L, 14L, 2L, 3L, 5L, 7L, 12L, 17L, 18L
                    ), .Label = c("ID_00169", "ID_00288", "ID_00324", "ID_00394", 
                                  "ID_00663", "ID_00790", "ID_00846", "ID_00860", "ID_00910", "ID_00959", 
                                  "ID_01013", "ID_01047", "ID_01130", "ID_01222", "ID_01260", "ID_06663", 
                                  "ID_06781", "ID_06786", "ID_06791", "ID_09099"), class = "factor"), 
                    values = c(0.654172560113154, 0.919096895578551, 0.925821596244131, 
                                0.860406091370558, 0.746376811594203, 0.767195767195767, 
                                0.830379746835443, 0.661577608142494, 0.707520891364902, 
                                0.908193484698914, 0.657118786857624, 0.687664041994751, 
                                0.68586387434555, 0.874513618677043, 0.836646499567848, 0.618361836183618, 
                                0.684163701067616, 0.914728682170543, 0.876297577854671, 
                                0.732707087959009, 0.773116438356164)), row.names = c(NA, 
                                                                                      -21L), class = "data.frame")

更新

graph2 <- add.edges(
  graph1,
  combn(
    sapply(
      decompose(graph1),
      function(p) sample(names(V(p))[degree(p) == max(degree(p))], 1)
    ), 2
  ),
  weight = 0.01
)

plot(graph2, layout = layout_nicely(graph1))

给予


上一个答案

你可以试试

out <- combn(
  decompose(graph1),
  2,
  FUN = function(x) {
    add.edges(
      graph1,
      sapply(x, function(p) sample(names(V(p))[degree(p) == max(degree(p))], 1)),
      weight = 0.01
    )
  },
  simplify = FALSE
)

sapply(out,plot)
## Save the layout from graph1
set.seed(2021)
LO = layout_nicely(graph1)
plot(graph1, layout=LO)

MaxNode <- rep("", max(cl$membership))
for(i in 1:max(cl$membership)) {
    MaxNode[i] <- names(which.max(degree(graph1)[cl$membership == i])) 
}
graph2 <-add.edges(graph1, combn(MaxNode, 2), weight = 0.01)
plot(graph2, layout=LO)