I wrote a recursive function in R for finding all paths s-t paths of a directed graph (no cycles). 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. 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. 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?
Thanks!