In functions that take a single vertex ID as a parameter (instead of a vertex sequence), negative IDs, which should be invalid, are often handled in a strange way. Take for example shortest_paths():
This is fine:
> shortest_paths(g,1,3)$vpath
[[1]]
+ 3/4 vertices, from 9cc5663:
[1] 1 2 3
This is fine:
> shortest_paths(g,5,3)$vpath
Error in shortest_paths(g, 5, 3) :
At core/paths/unweighted.c:294 : cannot get shortest paths, Invalid vertex id
What is happening here?
> shortest_paths(g,-1,3)$vpath
[[1]]
+ 2/4 vertices, from 9cc5663:
[1] 2 3
It turns out that internally, shortest_paths uses as.igraph.vs() on the from parameter, and effectively indexes into the vertex ID vector. And with R's usual quirkiness, -1 means "everything except -1":
> igraph:::as.igraph.vs(g,-1)
[1] 2 3 4
Then all but the first element gets ignored. Thus, unintuitively, -1 ends up meaning 2.
In functions that take a single vertex ID as a parameter (instead of a vertex sequence), negative IDs, which should be invalid, are often handled in a strange way. Take for example
shortest_paths():This is fine:
This is fine:
What is happening here?
It turns out that internally,
shortest_pathsusesas.igraph.vs()on thefromparameter, and effectively indexes into the vertex ID vector. And with R's usual quirkiness,-1means "everything except-1":Then all but the first element gets ignored. Thus, unintuitively,
-1ends up meaning2.