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])
}
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:
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.
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.
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: