Issue and possible bug with union function of igraph

I tried using the union function on 2 graphs which where inducted subgraph of the same graph G (so there was no conflict among them).
My problem is the original graph (and the subgraphs) are weighted, on the other hand the union of those subgraphs is not.

This is the code i created the subgraphs with (weights can assume a value in {1, 2, 3, 4, 5}:

weights <- E(g)$weight
graphs <- list()
for (r in seq(5)) {
  graphs[[r]] <- delete.edges(g, E(g)[weights!=r])
}
 
reduced_graphs <- list()
x <- numeric(5)
y <- numeric(5)
for (r in seq(5)) {
  reduced_graphs[[r]] <- delete.vertices(graphs[[r]], V(graphs[[r]])[degree(graphs[[r]])<=-1])
  
  y[r] <- length(get_users(reduced_graphs[[r]]))
  
  reduced_graphs[[r]] <- induced_subgraph(reduced_graphs[[r]], c(sample(get_users(reduced_graphs[[r]]), y[r]^sqrt(4.3/10)), get_movies(reduced_graphs[[r]])))
    
  x[r] <- length(get_movies(reduced_graphs[[r]]))
  
  reduced_graphs[[r]] <- induced_subgraph(reduced_graphs[[r]], c(sample(get_movies(reduced_graphs[[r]]), x[r]^(4.3/10)), get_users(reduced_graphs[[r]])))
  
  reduced_graphs[[r]] <- delete.vertices(reduced_graphs[[r]],  V(reduced_graphs[[r]])[degree(reduced_graphs[[r]])==0])
}

And this is the code that gives me an issue:

reduced_graph <- igraph::union(reduced_graphs[[1]], reduced_graphs[[2]])
is_weighted(reduced_graph)
[1] FALSE
is_weighted(reduced_graphs[[1]])
[1] TRUE
is_weighted(reduced_graphs[[2]])
[1] TRUE

It also turn out that the vertex of the resulting graphs lost their other attributes as well (like type and color):

V(reduced_graph)$type
NULL
V(reduced_graph)$color
NULL

Does anyone know how to fix the issue? Thankyou!

OS: Windows 10 pro
Rstudio
igraph version: 1.2.6


Cross-posted on StackOverflow

I moved the code from quotation blocks to code block. Please use only code blocks in the future, otherwise the code won’t survive intact.

Can you give a short self-contained example that illustrates the problem? This example is not self-contained. See http://sscce.org/ for guidance.

What seems to happen here is that if an attribute name is present in both graphs, e.g. both have the weight edge attribute, then attribute names will be changed in the result graph: you get both weight_1 and weight_2. Then it is up to you to combine these into a single weight attribute and handle any edges that were present in both graphs.

I cannot tell you what the best way to do this is, as I am not experienced with R (I focus on igraph’s Mathematica interface). However, the following works:

E(ug)$weight <- rowSums(cbind(E(ug)$weight_1, E(ug)$weight_2), na.rm=T)

This assumes that the union graph is called ug, the original edge attributes were called weight, and that you want to add up weights when both graphs had the same edge.

Note that this behaviour is not a bug. It is documented, see igraph R manual pages

union keeps the attributes of all graphs. All graph, vertex and edge attributes are copied to the result. If an attribute is present in multiple graphs and would result a name clash, then this attribute is renamed by adding suffixes: _1, _2, etc.

I won’t comment on whether it is a good solution for handling duplicate attributes, or whether weight should have special treatment. I’ll leave that to others.

1 Like

Thankyou, I really appreciate your answer.
However the intersection between the set of edges of the graphs should be empty since each one of the 5 graphs[[r]] is a subgraph with only all the edges with weight == r. And each reduced_graphs[[r]] is a subgraph of graphs[[r]].
This is further proved by the following code:

> temp <- do.call(
+             rbind,
+             lapply(reduced_graphs, get.data.frame)
+         )
> temp$weight = NULL
> any(duplicated(temp))
[1] FALSE