Pregunta

I have the same question as this how to merge two weighted graph and sum weigths.

But here ist my R code for better understanding:

g1 <- graph.full(10)
V(g1)$name <- letters[1:vcount(g1)]
E(g1)$weight <- 1

g3 <- graph.full(5)
V(g3)$name <- c("a", "b", "x", "y", "z")
E(g3)$weight <- 1

graph.union.by.name(g1, g3)

The weights in merged graph should be a 2 on same edges in g1 and g3 (a - b)

And the dput of graphs is:

> dput(g1)
structure(list(10, FALSE, c(1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 
4, 5, 6, 7, 8, 9, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 8, 9, 5, 6, 
7, 8, 9, 6, 7, 8, 9, 7, 8, 9, 8, 9, 9), c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 
3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8), c(0, 1, 9, 
2, 10, 17, 3, 11, 18, 24, 4, 12, 19, 25, 30, 5, 13, 20, 26, 31, 
35, 6, 14, 21, 27, 32, 36, 39, 7, 15, 22, 28, 33, 37, 40, 42, 
8, 16, 23, 29, 34, 38, 41, 43, 44), c(0, 1, 2, 3, 4, 5, 6, 7, 
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
40, 41, 42, 43, 44), c(0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45), 
    c(0, 9, 17, 24, 30, 35, 39, 42, 44, 45, 45), list(c(1, 0, 
    1), structure(list(name = "Full graph", loops = FALSE), .Names = c("name", 
    "loops")), structure(list(name = c("a", "b", "c", "d", "e", 
    "f", "g", "h", "i", "j")), .Names = "name"), structure(list(
        weight = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = "weight"))), class = "igraph")
> dput(g2)
structure(list(10, FALSE, c(1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 
4, 5, 6, 7, 8, 9, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 8, 9, 5, 6, 
7, 8, 9, 6, 7, 8, 9, 7, 8, 9, 8, 9, 9), c(0, 0, 0, 0, 0, 0, 0, 
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 
3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 8), c(0, 1, 9, 
2, 10, 17, 3, 11, 18, 24, 4, 12, 19, 25, 30, 5, 13, 20, 26, 31, 
35, 6, 14, 21, 27, 32, 36, 39, 7, 15, 22, 28, 33, 37, 40, 42, 
8, 16, 23, 29, 34, 38, 41, 43, 44), c(0, 1, 2, 3, 4, 5, 6, 7, 
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
40, 41, 42, 43, 44), c(0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45), 
    c(0, 9, 17, 24, 30, 35, 39, 42, 44, 45, 45), list(c(1, 0, 
    1), structure(list(name = "Full graph", loops = FALSE), .Names = c("name", 
    "loops")), structure(list(name = c("a", "b", "c", "d", "e", 
    "f", "g", "h", "i", "j")), .Names = "name"), structure(list(
        weight = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = "weight"))), class = "igraph")

Is it possible with igraph or do i need some workaround?

¿Fue útil?

Solución

This will be supported in the next version, until then here is a workaround:

mymerge <- function(g1, g2) {
  e1 <- get.data.frame(g1, what="edges")
  e2 <- get.data.frame(g2, what="edges")
  e <- merge(e1, e2, by=c("from", "to"), all=TRUE)
  newe <- data.frame(e[,c("from", "to"), drop=FALSE],
          weight=rowSums(e[, c("weight.x", "weight.y")], na.rm=TRUE))
  graph.data.frame(newe, directed=is.directed(g1))
}

mymerge(g1, g3)
# IGRAPH UNW- 13 54 -- 
# + attr: name (v/c), weight (e/n)
mymerge(g1, g3)["a", "b"]
# [1] 2
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top