0

I use igraph.

I want yo find all the possible paths between 2 nodes.

For the moment, there doesn't seem to exist any function to find all the paths between 2 nodes in igraph

I found this subject that gives code in python: All possible paths from one node to another in a directed tree (igraph)

I tried to port it to R but I have some little problems. It gives me the error:

Error of for (newpath in newpaths) { : 
  for() loop sequence incorrect

Here is the code:

find_all_paths <- function(graph, start, end, mypath=vector()) {
  mypath = append(mypath, start)

  if (start == end) {
    return(mypath)
  }

  paths = list()

  for (node in graph[[start]][[1]]) {
     if (!(node %in% mypath)){
      newpaths <- find_all_paths(graph, node, end, mypath)
      for (newpath in newpaths){
        paths <- append(paths, newpath)
      }
     }
  }
  return(paths)
}

test <- find_all_paths(graph, farth[1], farth[2])

Here is a dummy code taken from the igrah package, from which to get a sample graph and nodes:

actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
                             "Esmeralda"),
                      age=c(48,33,45,34,21),
                      gender=c("F","M","F","M","F"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
                                "David", "Esmeralda"),
                         to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
                         same.dept=c(FALSE,FALSE,TRUE,FALSE,FALSE,TRUE),
                         friendship=c(4,5,5,2,1,1), advice=c(4,5,5,4,2,3))
g <- graph.data.frame(relations, directed=FALSE, vertices=actors)

farth <- farthest.nodes(g)

test <- find_all_paths(graph, farth[1], farth[2])

thanks!

If anyone sees where the problem, that would be of great help...

Mathieu

Community
  • 1
  • 1
mathieu_r
  • 235
  • 1
  • 6
  • `I tried to port it to R [from Python]` <- Strongly disadvised if you want your code to be efficient. – Ari B. Friedman May 21 '13 at 11:15
  • 1
    Can you please make your example reproducible by providing code for creating small versions of `graph` and `farth`? – flodel May 21 '13 at 12:02
  • Take a look to http://igraph.sourceforge.net/doc/R/shortest.paths.html – Mihai8 May 21 '13 at 12:59
  • @user1929959 I dont want the shortest path but all the paths between two nodes – mathieu_r May 21 '13 at 15:01
  • @AriB.Friedman Initially, I generate my network from a spatial object with R. It's simpler to maintain if I process everything within R. Also, the answer already exists in pythion. The problem is with R.. – mathieu_r May 21 '13 at 15:16

1 Answers1

1

I also attempted to translate that same solution from Python to R and came up with the following which seems to do the job for me:

# Find paths from node index n to m using adjacency list a.
adjlist_find_paths <- function(a, n, m, path = list()) {
  path <- c(path, list(n))
  if (n == m) {
    return(list(path))
  } else {
    paths = list()
    for (child in a[[n]]) {
      if (!child %in% unlist(path)) {
        child_paths <- adjlist_find_paths(a, child, m, path)
        paths <- c(paths, child_paths)
      }
    }
    return(paths)
  }
}

# Find paths in graph from vertex source to vertex dest.
paths_from_to <- function(graph, source, dest) {
  a <- as_adj_list(graph, mode = "out")
  paths <- adjlist_find_paths(a, source, dest)
  lapply(paths, function(path) {V(graph)[unlist(path)]})
}
johannux
  • 186
  • 10
  • thanks Johannux, it worked like a charm :) I also found out there was an all_simple_paths functions in igraph that might complete the same thing. – mathieu_r Sep 29 '15 at 06:34