0

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

David
  • 13
  • 5

1 Answers1

0

Maybe you can try the following workaround

graph_from_data_frame(
  unique(
    do.call(
      rbind,
      lapply(reduced_graphs, get.data.frame)
    )
  )
)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Thankyou, I really liked your idea and indeed the resulting graph keeps the weights, tho it doesn't solve the issue it had with the vertex attributes that get lost even with this solution. (Also correct me if I am wrong but I believe the unique is unnecessary since the subgraphs have no common edge) – David Sep 03 '21 at 15:54
  • @David Could you use `dput()` to share your data? Then I will check with it. – ThomasIsCoding Sep 03 '21 at 18:20