【问题标题】:Finding all shortest paths for a million pair of nodes in a large network寻找大型网络中一百万对节点的所有最短路径
【发布时间】:2022-12-08 05:25:28
【问题描述】:

我有一个包含 17,765 个节点和 7,4876 个边的大型网络。我正在使用 igraph 来运行我的大部分分析。我一直在寻找不同节点对(大约 100 万对)的最短路径数量。我不需要路径,只需要它们对每对的计数(存在多少)。为此,我使用并行化策略和 all_shortest_paths() 函数迭代节点对。它适用于几千个节点对的子集;但是,它非常慢,我不知道如何优化它。代码可以在下面找到:

library(igraph)
library(doParallel)
library(foreach)

count_paths <- function(g,start,end) {
  #create the cluster
  my.cluster <- parallel::makeCluster(
    n.cores, 
    type = "PSOCK")
  
  doParallel::registerDoParallel(my.cluster)
  
  foreach(i=1:length(start),.combine = "c") %dopar% {
    length(igraph::all_shortest_paths(g,
                                      from = start[i],
                                      to=end[i],
                                      mode = "all")[["res"]])
  }
}

counts<-count_paths(graph_directed,names(v_start),names(v_end))
stopCluster(my.cluster)

我在 all_shortest_paths() 中选择了“全部”选项,因为我将我的图视为无向图。

在此先感谢您的帮助 :)

【问题讨论】:

    标签: r networking graph bigdata shortest-path


    【解决方案1】:
    library(igraph)
    library(Rcpp)
    library(parallel)
    
    # Create a cluster of workers using the parallel package
    my.cluster <- makeCluster(n.cores, type = "PSOCK")
    
    # Calculate the shortest path lengths between all pairs of nodes using the distances() function
    shortest.paths <- distances(g, mode = "all")
    
    # Use the parallel package to parallelize the calculation of the shortest paths counts
    counts <- parApply(my.cluster, shortest.paths, function(x) sum(!is.infinite(x)))
    
    # Stop the cluster of workers when you are done
    stopCluster(my.cluster)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多