0

I have a large network with 17,765 nodes and 7,4876 edges. I'm using igraph to run most of my analysis. I got stuck on finding the number of shortest paths for different pairs of nodes (around 1 million pairs). I don't need the paths, only their counts for each pair (how many exist). To do so, I'm iterating through the node pairs using a parallelized strategy together with the all_shortest_paths() function. It works for subsets of a few thousand node pairs; however, it is extremely slow, and I don't know how to optimize it. The code can be found below:

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)

I have opted for the "all" option in the all_shortest_paths() because I'm treating my graph as undirected.

Thanks in advance for your help :)

4
  • Counting shortest paths between two points is a rather unusual thing to want to do, which is why igraph only has limited support for this. Why do you want to do this, what's your application? Commented Dec 11, 2022 at 20:52
  • I want to compare the number of shortest paths that are "viable" (from my directed graph) against all the possible options (viable and inviable from the same graph treated as undirected graph). The main reason is to discover the fraction of paths (among all shortest paths in the network) that connect specific nodes. Commented Dec 13, 2022 at 5:03
  • I don't quite understand what you mean. As I understand your description, you want to count shortest paths between some pairs of vertices in both the directed and undirected version of a graph. This seems like a restatement of the task, but doesn't explain the motivation, so it's not really helpful in deciding what to include in igraph. Side note: Directed shortest paths won't usually be undirected shortest paths as well. The set of directed shortest paths is not a subset of the undirected ones. Commented Dec 13, 2022 at 7:33
  • The performance advice I can give is to minimize the number of calls to all_shortest_paths by including as many end vertices in the to parameter as possible. There is a non-negligible overhead to each call, so reducing the number of calls should give you a very noticeable performance boost. There is currently no direct way to only get the number of paths, but not the paths themselves. While the igraph_get_all_shortest_paths function in the igraph C library is able to return only the counts of paths, but not the paths themselves, internally it always computes paths. Commented Dec 13, 2022 at 7:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.