如何将大量 igraph 组合成一个 igraph - R

How to combine a large list of igraphs into one igraph - R

我有一个大图需要过滤。过滤(子图)后,我得到了一个子图列表。我需要再次将所有这些子图组合成一个图。不知道怎么组合大列表(近百万子图)

> require(igraph)
> graph <- make_ring(7)  #this is my original graph
> V(graph)$name <- c("A", "B", "C", "D", "E", "F", "G")  #name of vertices
> V(graph)$att1 <- c(1,2,NA,1,2,3,NA)  #some attribute
> selected_vertices <- V(graph)$name[which(V(graph)$att1 == 1)] #let's say i need to subgraph the graph to include only vertices with att1 == 1 (and their first order neighbours)
> subgraph_list <- make_ego_graph(graph, order=1, selected_vertices)  #this creates a list of igraphs but I need one graph containing all the graphs

这失败了:

> subgraph <- induced_subgraph(graph, unlist(subgraph_list ))
Error in as.igraph.vs(graph, vids) : 
  (list) object cannot be coerced to type 'double'

我已经尝试过其他方法来对图进行自我子图化,但由于它是一个相当大的图,因此需要很长时间,并且运行良好且相对较快的一个函数是 make_ego_graph,它创建了一个列表.

我假设您想要这些图的并集:

do.call(union, subgraph_list)

好的,我找到了适合我的解决方案(速度非常快,不会让我的电脑崩溃)。

> #first convert the list of igrpahs to list of data.frames
> subgraph_list_df <- lapply(subgraph_list, as_data_frame)
> # then combine all the data.frames in the list into one data.frame
> subgraph_df <- do.call(rbind, subgraph_list_df)
> #then make a graph out of the one combined data.frame
> subgraph <- graph_from_data_frame(subgraph_df , directed = FALSE)

出于我的目的,我知道子图是孤立的(它们之间没有共享边)。但是如果它们是连接的,应该只做一个唯一的(subgraph_df)在转换为图形之前去除重复的边。