简体   繁体   English

组合两个图并在R igraph中添加边权重

[英]Combine two graphs and add edge weights in R igraph

I'm trying to combine two graphs with the same nodes, but such that the new graph edge weight is the sum of the two original graphs (but of course want the solution to extend to N graphs): 我试图将两个图表与相同的节点组合在一起,但是新的图形边缘权重是两个原始图形的总和(但当然希望解决方案扩展到N个图形):

g1 <- graph.empty(directed=FALSE) + vertices(letters[1:2])
g1 <- g1 + edge("a", "b")
E(g1)$weight <- 1

g2 <- graph.empty(directed=FALSE) + vertices(letters[1:2])
g2 <- g2 + edge("a", "b")

E(g2)$weight <- 2

g3 <- g1 %u% g2

E(g3)$weight_1 #this is 1
E(g3)$weight_2 #this is 2

But i want E(g3)$weight to be 3. 但我希望E(g3)$重量为3。

Is there a more elegant way of doing this than summing across the edge weights _1, _2, ... afterwards? 有没有一种更优雅的方式来做这个,而不是在边缘权重_1,_2,...之后进行求和? Something along the lines of simplify/contract? 简化/合同的东西?

Just add weight_1 and weight_2 . 只需添加weight_1weight_2 igraph does not currently have a way to combine vertex/edge attributes from multiple graphs, except by hand. igraph目前没有办法组合多个图形的顶点/边缘属性,除非是手工。 This is usually not a big issue, because it is just an extra line of code (per attribute). 这通常不是一个大问题,因为它只是一个额外的代码行(每个属性)。 Well, three lines if you want to remove the _1 , _2 attributes. 好吧,如果要删除_1_2属性,则为三行。 So all you need to do is: 所以你需要做的就是:

E(g3)$weight <- E(g3)$weight_1 + E(g3)$weight_2

and potentially 并且可能

g3 <- remove.edge.attribute(g3, "weight_1")
g3 <- remove.edge.attribute(g3, "weight_2")

I created an issue for this in the igraph issue tracker, but don't expect to work on it any time soon: https://github.com/igraph/igraph/issues/800 我在igraph问题跟踪器中为此创建了一个问题,但不要期望很快就能在它上面工作: https//github.com/igraph/igraph/issues/800

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

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