tl;dr: distances
is giving me path lengths, but fail to recover what nodes are in those paths when using simple_paths
.
I'd like to find all shortest, simple paths of a given length in my network. My network can be relatively large (1000 nodes, tens of thousands of edges), and since simple_paths
is relatively slow and distances
is quick, I thought I could first calculate distances
as a filtering step.
That is, my current strategy is to
- calculate the length of all simple paths between each pair of vertices, i.e.
dd = distances(my.net)
- find paths with desired length, i.e.
dd[dd == desired.length]
- recover the nodes in this, now relatively small, list of paths.
However, I'm failing on step 3. That is, I can't recover the paths given by distances
. For example, in the code below distances
finds a path of length 3 between nodes D and X. When I try to use simple_paths
to find out what that path actually is, I get nothing. What am I doing wrong?
require(dplyr)
require(tidyverse)
require(igraph)
set.seed(1)
# make network
fake.net = data.frame(A = sample(LETTERS,50,replace = T),
B = sample(LETTERS,50,replace = T),
stringsAsFactors = F) %>%
dplyr::filter(!A==B) %>%
as.matrix() %>% graph_from_edgelist()
# find one path of length 3
dd = distances(fake.net)
ia = which(dd==3)[1]
v.from = V(fake.net)[ia %% ncol(dd)]
v.to = V(fake.net)[ceiling(ia / ncol(dd))]
# what is that path?
shortest_paths(fake.net, from = v.from, to = v.to)
$vpath
$vpath[[1]]
+ 0/26 vertices, named, from ffb91bb:
$epath
NULL
$predecessors
NULL
$inbound_edges
NULL
library()
calls to load any needed packages. That way we can firgure out whether your workspace has stuff that would otherwise create failures. I getError: Problem with
filter()` input..1
. x level sets of factors are different` from your effort to createfake.net
. – IRTFMrequire(dplyr)
, does that fix it? – R Greg Staceydata.frame
call. – IRTFMstringsAsFactors=F
explicitly – R Greg Stacey