如何将一个包含大量igraph的列表合并为一个igraph - R

4

我有一个庞大的图表需要进行筛选。经过筛选(子图化)后,我得到一系列子图清单。我需要将这些子图合并为一个原始图表。我不知道如何合并这个包含将近一百万子图的庞大清单。

> 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,它可以创建一个列表。

2个回答

3

我猜你想要这些图的并集:

do.call(union, subgraph_list)

嗯...我希望有更快的选项。这个就像调用一百万次for循环,需要很长时间。<:) - Ankhnesmerira
它崩溃了我的系统(有128GB的RAM!),所以我想它不能被视为解决方案适用于我的情况。 - Ankhnesmerira
2
也许可以尝试使用 Reduce("+", subgraph_list) - Roland
谢谢。不幸的是,仍然需要太长时间。我认为我找到了一个解决方案,它不能直接作用于图形对象形式的所有图形。 - Ankhnesmerira

2

好的,我找到了一种适合我的解决方案(速度相当快,不会使我的电脑崩溃)。

> #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)

对于我的目的,我知道子图是隔离的(它们之间没有共享的边)。但如果它们相互连接,需要在转换为图之前仅执行unique(subgraph_df)以消除重复边。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接