Following up on All simple paths of specified length in R
Is there a way to pass a (character) list of nodes into node1 and node2? I am interested in flow from one hub vertex to another, with path length <=5, as long as none of the intervening vertices is also a hub. The hubs are in a list and I would like to pass that list into the arguments node1 and node2. I have tried both of the following to execute the function:
[1]
do.call(SimplePathsLengthN, c(graph,as.character(hublist),as.character(hublist),5))
This returns:
Error in (function (graph, node1, node2, pathlength = 2) : unused arguments
[2]
for(i in 1:18){
for(j in 1:18){
SimplePathsLengthN(graph, as.character(hublist[[1]][i]), as.character(hublist[[1]][j]), 5)
} # where 18 is the number of hubs
}
This returns: Error in as.igraph.vs(graph, v) : Invalid vertex names
Is this because the vertex names are character in hublist?
I also tried using all_simple_paths but the network is dense enough that it runs for hours, so it isn't really an option.
pathlist1 <- all_simple_paths(
graph,
from = V(graph)$name %in% hublist,
to = V(graph)$name %in% hublist,
mode = "out"
)
I am sorry to create a new question; my reputation score is such that I cannot comment on the first.
UPDATE I have amended the code from the linked solution to include a list into which write the loop output:
hublist <- unlist(V(graph)$name[V(graph)$ishub == 1])
pathlist =list()
# block below is from https://stackoverflow.com/questions/64765791/all-simple-paths-of-specified-length-in-r
SimplePathsLengthN = function(graph, node1, node2, pathlength=2) {
SP = list()
if(node1 != node2) {
if(pathlength == 1) {
if(node2 %in% neighbors(graph, node1)) {
SP[[1]] = c(node1, node2) }
return(SP) }
Nbrs2 = neighbors(graph, node2)
for(nbr2 in Nbrs2) {
ASPn2 = SimplePathsLengthN(graph, node1, nbr2, pathlength-1)
for(i in seq_along(ASPn2)) {
if(!(node2 %in% ASPn2[[i]])) {
#pathlist = append(SP, list(c(ASPn2[[i]], node2)))
SP = append(SP, list(c(ASPn2[[i]], node2))) }
pathlist <- rbind(pathlist,SP)
}
}
}
return(SP)
}
m=1
for(j in 1:18){
for(k in 1:18){
pathlist[[m]]<- SimplePathsLengthN(graph, hublist[[j]], hublist[[k]], 2)
m=m+1
} # where 18 is the number of hub vertices
}
pathlist
This seems to work, but returns a warning
In rbind(pathlist, SP) : number of columns of result is not a multiple of vector length
Any help would be appreciated.