如何基于组件的节点的最大度在图的组件之间添加边 [英] How to add an edge between components of a graph based on the maximum degree of a node of the component
本文介绍了如何基于组件的节点的最大度在图的组件之间添加边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用以下代码查看详细信息
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个节点)posts
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))
现在,我想在nodes
个数最高的nodes
之间添加一个edge
,例如:ID_00180
具有度数14
(附加图像的左侧分量)和ID_00104
具有度数3
(附加图像的顶部分量)。在组合这两个组件时,我想在ID_00180
和ID_00104
之间添加一条边(而不是随机抽取)。
如果任何组件有多个相同的最高阶数,例如:附加图像的右下角组件(所有节点的阶数都为2),那么我们可以从最高阶数的节点中随机选取任何一个。比如说,我们可以在ID_00180
和any 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)
这篇关于如何基于组件的节点的最大度在图的组件之间添加边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文