algorithm - All Paths Between 2 vertexes in R -


i use igraph.

i want yo find possible paths between 2 nodes.

for moment, there doesn't seem exist function find paths between 2 nodes in igraph

i found subject gives code in python: all possible paths 1 node in directed tree (igraph)

i tried port r have little problems. gives me error:

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

here code:

find_all_paths <- function(graph, start, end, mypath=vector()) {   mypath = append(mypath, start)    if (start == end) {     return(mypath)   }    paths = list()    (node in graph[[start]][[1]]) {      if (!(node %in% mypath)){       newpaths <- find_all_paths(graph, node, end, mypath)       (newpath in newpaths){         paths <- append(paths, newpath)       }      }   }   return(paths) }  test <- find_all_paths(graph, farth[1], farth[2]) 

here dummy code taken igrah package, sample graph , 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 sees problem, of great help...

mathieu

i attempted translate same solution python r , came following seems job me:

# find paths node index n 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()     (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 vertex source vertex dest. paths_from_to <- function(graph, source, dest) {   <- as_adj_list(graph, mode = "out")   paths <- adjlist_find_paths(a, source, dest)   lapply(paths, function(path) {v(graph)[unlist(path)]}) } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -