合并两个图形并在R igraph中添加边缘权重 [英] Combine two graphs and add edge weights in R igraph
问题描述
我正在尝试将两个图与相同的节点合并,但是新图的边权重是两个原始图的总和(但当然希望解决方案扩展到N个图):
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):
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
但是我希望E(g3)$ weight为3.
But i want E(g3)$weight to be 3.
有没有比之后对边缘权重_1,_2,...求和更优雅的方法了?类似于简化/合同?
Is there a more elegant way of doing this than summing across the edge weights _1, _2, ... afterwards? Something along the lines of simplify/contract?
推荐答案
只需添加weight_1
和weight_2
. igraph目前尚无法通过手动方式组合多个图的顶点/边属性.这通常不是什么大问题,因为它只是一行额外的代码(每个属性).好吧,如果要删除_1
,_2
属性,则需要三行.因此,您所需要做的就是:
Just add weight_1
and weight_2
. igraph does not currently have a way to combine vertex/edge attributes from multiple graphs, except by hand. 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. So all you need to do is:
E(g3)$weight <- E(g3)$weight_1 + E(g3)$weight_2
并且可能
g3 <- remove.edge.attribute(g3, "weight_1")
g3 <- remove.edge.attribute(g3, "weight_2")
我在igraph问题跟踪器中为此创建了一个问题,但不要指望很快就可以解决: https://github.com/igraph/igraph/issues/800
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
这篇关于合并两个图形并在R igraph中添加边缘权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!