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