简体   繁体   English

如何在 igraph R 中的图形组件之间添加边

[英]How to add a edges between component of a graph in igraph R

I have a graph containing 4 components .我有一个包含4 components的图表。 Now, I want to add an edge among all components based on the size of the membership .现在,我想根据 member 的size of the membershipall components中添加一条边。

For example, the following graph contains 4 components .例如,下图包含4 components

在此处输入图像描述

First, I will connect all components with only one edge and take the edge randomly .首先,我将all components with only one edge and take the edge randomly I can do it using this code我可以使用此代码来做到这一点

graph1 <- graph_from_data_frame(g, directed = FALSE)
E(graph1)$weight <- g$new_ssp
cl <- components(graph1)

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

Secondly, now, I want to add an edge between component-1 and component-2 .其次,现在,我想在component-1 and component-2之间添加一条边。 I want to add an edge between 2 components but rest of the component will be present in the new graph from the previous graph .我想在2 components之间添加一条边,但组件rest of the component will be present in the new graph from the previous graph

Like, after adding an edge between component-1 and component-2 , the new graph will contain 3 component 1st (component-1 and component-2 as a 1 component because we added 1 edge), 2nd (component-3 from the main graph), and 3rd (component-4 from the main graph) .就像,在component-1 and component-2之间添加一条边之后,新图将包含3 component 1st (component-1 and component-2 as a 1 component because we added 1 edge), 2nd (component-3 from the main graph), and 3rd (component-4 from the main graph) I can do it using this code我可以使用此代码来做到这一点

dg <- decompose.graph(graph1)
graph3 <- (dg[[1]] %u% dg[[2]])

component_subgraph_1 <- components(graph3)

graph2 <- with(
  stack(membership(component_subgraph_1)),
  add.edges(
    graph1,
    c(combn(sapply(split(ind, values), sample, size = 1), 2)),
    weight = 0.01))

Figure:数字: 在此处输入图像描述

Same for all combinations.所有组合都相同。 Such as, component-1 and component-3 , and component-1 and component-4 , and component-2 and component-3 , and component-2 and component-4 , and component-3 and component-4 .例如, component-1 and component-3component-1 and component-4component-2 and component-3component-2 and component-4component-3 and component-4

But, this is not feasible to write the code and change manually dg[[1]] , dg[[2]] , and so on.但是,编写代码并手动更改dg[[1]]dg[[2]]等是不可行的。 Moreover, my actual dataset contains a lot of components.此外,我的实际数据集包含很多组件。 So, in reality, this is impossible.所以,在现实中,这是不可能的。 Any idea, how can I do this automatically?任何想法,我怎样才能自动做到这一点?

Actually, I have a scoring function (like the shortest path).实际上,我有一个评分 function (如最短路径)。 So, I want to check the score after adding all components , or after adding only 2 components , after adding only 3 components , and so on !所以,我想在adding all components后检查分数,或者after adding only 2 components after adding only 3 componentsso on Something like greedy algorithms .greedy algorithms之类的东西。

Reproducible Data:可重现的数据:

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")

Thanks in advance.提前致谢。

You are actually close to what you want already.你实际上已经接近你想要的了。 Perhaps the code below could help you也许下面的代码可以帮助你

out <- with(
  stack(membership(cl)),
  lapply(
    combn(split(ind, values), 2, simplify = FALSE),
    function(x) {
      add.edges(
        graph1,
        c(combn(sapply(x, sample, size = 1), 2)),
        weight = 0.01
      )
    }
  )
)

and then you can run然后你可以运行

sapply(out, plot)

to visualize all the combinations.可视化所有组合。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM