【问题标题】:How to connect components of a graph in igraph如何在 igraph 中连接图形的组件
【发布时间】:2021-03-16 17:24:18
【问题描述】:

我有一个包含weighted undirected graph 的边缘列表的数据集。我的数据集包含multiple components

现在,我正在尝试连接图形的组件。我的图表包含4 components。其实我想联系他们any edge of 1 component with any edge of another component and would like to give any weight of this edge

graph1 <- graph_from_data_frame(g, directed = FALSE)
E(graph1)$weight <- is.numeric(g$new_ssp)
plot(graph1, vertex.label = V(graph1)$name)
is_weighted(graph1)
cl <- components(graph1)

另外,我可以找到membership of the graph

connected_components <- lapply(seq_along(cl$csize)[cl$csize > 1], function(x) 
  V(graph1)$name[cl$membership %in% x])

现在,您能告诉我如何连接所有组件吗?有可能吗?

可重现的数据:

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"), 
               new_ssp = 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")

【问题讨论】:

  • 您应该使用E(graph1)$weight &lt;- g$new_ssp 为边添加权重

标签: r graph igraph connected-components


【解决方案1】:

试试下面的代码

graph2 <- with(
  stack(membership(cl)),
  add.edges(
    graph1,
    c(combn(sapply(split(ind, values), sample, size = 1), 2)),
    weight = runif(choose(cl$no, 2))
  )
)

所有组件之间的完整连接如下所示


你可以查看graph2的边权重

> E(graph2)
+ 27/27 edges from 31a7614 (vertex names):
 [1] ID_00104--ID_06663 ID_00104--ID_06791 ID_00104--ID_09099 ID_00136--ID_00169
 [5] ID_00136--ID_00910 ID_00169--ID_00910 ID_00178--ID_00790 ID_00180--ID_01013
 [9] ID_00180--ID_01130 ID_00180--ID_01260 ID_00180--ID_00394 ID_00180--ID_00860
[13] ID_00180--ID_00959 ID_00180--ID_01222 ID_00180--ID_00288 ID_00180--ID_00324
[17] ID_00180--ID_00663 ID_00180--ID_00846 ID_00180--ID_01047 ID_00180--ID_06781
[21] ID_00180--ID_06786 ID_00136--ID_06663 ID_00178--ID_06663 ID_06663--ID_01013
[25] ID_00136--ID_00178 ID_00136--ID_01013 ID_00178--ID_01013

> E(graph2)$weight
 [1] 0.6541726 0.9190969 0.9258216 0.8604061 0.7463768 0.7671958 0.8303797
 [8] 0.6615776 0.7075209 0.9081935 0.6571188 0.6876640 0.6858639 0.8745136
[15] 0.8366465 0.6183618 0.6841637 0.9147287 0.8762976 0.7327071 0.7731164
[22] 0.3098355 0.5842953 0.7397926 0.7337629 0.7643245 0.8331742

如果要查看边缘列表,可以使用as_data_frame,例如,

> as_data_frame(graph2)
       from       to   new_ssp    weight
1  ID_00104 ID_06663 0.6541726 0.6541726
2  ID_00104 ID_06791 0.9190969 0.9190969
3  ID_00104 ID_09099 0.9258216 0.9258216
4  ID_00136 ID_00169 0.8604061 0.8604061
5  ID_00136 ID_00910 0.7463768 0.7463768
6  ID_00169 ID_00910 0.7671958 0.7671958
7  ID_00178 ID_00790 0.8303797 0.8303797
8  ID_00180 ID_01013 0.6615776 0.6615776
9  ID_00180 ID_01130 0.7075209 0.7075209
10 ID_00180 ID_01260 0.9081935 0.9081935
11 ID_00180 ID_00394 0.6571188 0.6571188
12 ID_00180 ID_00860 0.6876640 0.6876640
13 ID_00180 ID_00959 0.6858639 0.6858639
14 ID_00180 ID_01222 0.8745136 0.8745136
15 ID_00180 ID_00288 0.8366465 0.8366465
16 ID_00180 ID_00324 0.6183618 0.6183618
17 ID_00180 ID_00663 0.6841637 0.6841637
18 ID_00180 ID_00846 0.9147287 0.9147287
19 ID_00180 ID_01047 0.8762976 0.8762976
20 ID_00180 ID_06781 0.7327071 0.7327071
21 ID_00180 ID_06786 0.7731164 0.7731164
22 ID_06791 ID_00910        NA 0.5680686
23 ID_00178 ID_06791        NA 0.6278790
24 ID_06791 ID_00288        NA 0.8059711
25 ID_00178 ID_00910        NA 0.2578959
26 ID_00910 ID_00288        NA 0.6189977
27 ID_00178 ID_00288        NA 0.6594792

【讨论】:

  • 非常感谢。你能告诉我,size=1 是什么吗?因为,我之前的图表是weighted,它有edge weight,现在也加权了(我检查了is_weighted(graph2) 函数)。但是,你能告诉我,我怎样才能检查新边缘的重量?
  • @Akib62 size=1 是来自sample 的参数,当使用sapply 而不是cl 时,它会从每个组件中随机选择1。我没有为新添加的边缘分配任何权重。但这很容易做到。您可以尝试replace(E(graph2)$weight, is.na(E(graph2)$weight),1) 为所有新边分配权重1。例如。
  • @Akib62 我更新了我的解决方案,为新添加的边分配随机权重。
  • 非常感谢。也就是说,我不需要编写这部分代码connected_components &lt;- lapply(seq_along(cl$csize)[cl$csize &gt; 1], function(x) V(graph1)$name[cl$membership %in% x])?当您从cl 获得会员资格时。但是,当我尝试生成新图的边缘列表时,我只得到边缘列表而不是权重。你能告诉我如何获得带有重量的 edge_list 吗?我正在尝试graph2_edge &lt;- as_edgelist(graph2, names = TRUE)
  • @Akib62 在我的回答中查看add.edges 的论点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-05
  • 2021-08-19
  • 2015-06-26
  • 2022-01-22
  • 2012-10-24
  • 1970-01-01
相关资源
最近更新 更多