Combine the two graphs and add the edge weights to R igraph

I am trying to combine two graphs with the same nodes, but so that the new weight of the edge of the graph is the sum of the two original graphs (but, of course, I want the solution to go on 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

But I want the weight of E (g3) $ to be 3.

Is there a more elegant way to do this than summing over the weights of the edges _1, _2, ... after? Something like simplification / contract?

+4
source share
1 answer

weight_1 weight_2. igraph / , . , ( ). , , _1, _2. , , , :

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

+2

Source: https://habr.com/ru/post/1569856/


All Articles