# Issue and possible bug with union function of igraph

**URL:** <https://igraph.discourse.group/t/issue-and-possible-bug-with-union-function-of-igraph/838>\
**Category:** Usage\
**Tags:** R\
**Created:** [3 September 2021 10:52 UTC](https://igraph.discourse.group/t/issue-and-possible-bug-with-union-function-of-igraph/838 "2021-09-03T10:52:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/david/32/551_2.png) [@David](https://igraph.discourse.group/u/David)\
**Post date:** [3 September 2021 10:52 UTC](https://igraph.discourse.group/t/issue-and-possible-bug-with-union-function-of-igraph/838/1 "2021-09-03T10:52:58Z")

</div>

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}:

```auto
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:

```auto
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):

```auto
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](https://stackoverflow.com/q/69044932/695132)

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [3 September 2021 16:01 UTC](https://igraph.discourse.group/t/issue-and-possible-bug-with-union-function-of-igraph/838/2 "2021-09-03T16:01:41Z")

</div>

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/](http://sscce.org/) for guidance.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [3 September 2021 16:12 UTC](https://igraph.discourse.group/t/issue-and-possible-bug-with-union-function-of-igraph/838/3 "2021-09-03T16:12:32Z")

</div>

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](https://igraph.org/r/doc/union.igraph.html)

> `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.

---

<div class="post-metadata">

**Author:** ![David](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/david/32/551_2.png) [@David](https://igraph.discourse.group/u/David)\
**Post date:** [3 September 2021 16:54 UTC](https://igraph.discourse.group/t/issue-and-possible-bug-with-union-function-of-igraph/838/4 "2021-09-03T16:54:33Z")

</div>

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:

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

```
