简体   繁体   English

如何在R中向量化(并加快)此递归图搜索功能?

[英]How can I vectorize (and speed up) this recursive graph search function in R?

I wrote a recursive function in R for finding all paths st paths of a directed graph (no cycles). 我在R中编写了一个递归函数,用于查找有向图的路径st的所有路径(无循环)。 I used this page as my model: All possible paths from one node to another in a directed tree (igraph) and it outputs the correct result, but it's slow. 我使用此页面作为模型: 在有向树(igraph)中从一个节点到另一个节点的所有可能路径,并且它输出正确的结果,但是速度很慢。 With small graphs, no big deal. 小图没什么大不了的。 With large graphs, it's an issue. 对于大图,这是一个问题。

I'm new to R but have read that it performs significantly better when avoiding loops and using vectorization. 我是R的新手,但已阅读它在避免循环和使用向量化时的性能明显更好。 I'm trying to wrap my head around it, and am hoping you might assist. 我正在努力解决问题,希望您能提供帮助。 My code: 我的代码:

findAllPaths <- function(graph,start,end) {
  return(fastFindPaths(graph, start, end))
}   


fastFindPaths <- function(graph, from, to, path) {
  if(missing(path)) path <- c()
  path <- cbind(path, from)
  if (from == to) return(path)
  paths <- c()
  adjList <- get.adjlist(graph, mode="out")[[from]]
  for (child in adjList) {
    if (!child %in% path) {
      childPaths <- fastFindPaths(graph, child, to, path)
      for (childPath in childPaths) paths <- c(paths, childPath)
    }
  }
  return(paths)
}

So, is this a candidate for vectorization? 那么,这是矢量化的候选人吗? How can I speed this up? 我怎样才能加快速度? Any other tips you'd give someone learning R? 您还会给其他学习R的技巧吗?

Thanks! 谢谢!

igraph的开发版本具有get.all.simple.paths()函数,您可以从此处获取: http : get.all.simple.paths()

I used Tamas's suggestion to not call get.adjlist() but instead use neighbors() and that provided a decent speed increase. 我使用Tamas的建议不调用get.adjlist() ,而是使用neighbors() ,这提供了不错的速度提高。 What really helped improve the performance, however, was parallelizing the search, by calling fastFindPaths() on each of the start node's children and aggregating the results. 但是,真正有助于提高性能的是,通过在每个起始节点的子节点上调用fastFindPaths()并汇总结果来并行化搜索。 I'm currently on a Windows machine, so used the clusterApply() function. 我目前在Windows计算机上,因此使用了clusterApply()函数。 Though I will likely generalize the code to check the Sys.info()[1] and use mclapply() if not Windows. 尽管我可能会泛化代码以检查Sys.info()[1] ,如果不是Windows,则使用mclapply()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM