David
David

Reputation: 13

Issue 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

Upvotes: 0

Views: 136

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 102579

Maybe you can try the following workaround

graph_from_data_frame(
  unique(
    do.call(
      rbind,
      lapply(reduced_graphs, get.data.frame)
    )
  )
)

Upvotes: 0

Related Questions