When adjacent_vertices() is run with the option return.vs.es set to FALSE (which is not the default), all the vertex indices are 1 too small.
neighbors(), on the other hand, returns correct results for both values of return.vs.es.
The reason is that in neighbors() the vertex-indices are always increased by 1, while in adjacent_vertices, this only happens when return.vs.es is TRUE.
To reproduce
library(igraph)
#>
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#>
#> decompose, spectrum
#> The following object is masked from 'package:base':
#>
#> union
g <- make_tree(13, children = 3)
V(g)$name <- paste0("V", 1:13)
plot(g, vertex.size = 35)
# make sure the option is set to the default
igraph_options(return.vs.es = TRUE)
# in this case, all results are as expected
lapply(c(1, 4), neighbors, graph = g)
#> [[1]]
#> + 3/13 vertices, named, from 101b41a:
#> [1] V2 V3 V4
#>
#> [[2]]
#> + 3/13 vertices, named, from 101b41a:
#> [1] V11 V12 V13
av <- adjacent_vertices(g, c(1, 4))
av
#> $V1
#> + 3/13 vertices, named, from 101b41a:
#> [1] V2 V3 V4
#>
#> $V4
#> + 3/13 vertices, named, from 101b41a:
#> [1] V11 V12 V13
lapply(av, as.integer)
#> $V1
#> [1] 2 3 4
#>
#> $V4
#> [1] 11 12 13
# with return.vs.es = FALSE, neighbors() still works, but adjacent_vertices()
# is 1 too small.
igraph_options(return.vs.es = FALSE)
lapply(c(1, 4), neighbors, graph = g)
#> [[1]]
#> [1] 2 3 4
#>
#> [[2]]
#> [1] 11 12 13
av <- adjacent_vertices(g, c(1, 4))
av
#> $V1
#> [1] 1 2 3
#>
#> $V4
#> [1] 10 11 12
lapply(av, \(i) V(g)[[i]])
#> $V1
#> + 3/13 vertices, named, from 101b41a:
#> name
#> 1 V1
#> 2 V2
#> 3 V3
#>
#> $V4
#> + 3/13 vertices, named, from 101b41a:
#> name
#> 10 V10
#> 11 V11
#> 12 V12
What happens, and what did you expect instead?
When
adjacent_vertices()
is run with the optionreturn.vs.es
set toFALSE
(which is not the default), all the vertex indices are 1 too small.neighbors()
, on the other hand, returns correct results for both values ofreturn.vs.es
.The reason is that in
neighbors()
the vertex-indices are always increased by 1, while inadjacent_vertices
, this only happens whenreturn.vs.es
isTRUE
.To reproduce
Created on 2024-11-29 with reprex v2.1.1
System information
R is installed through apt from the PPA
https://cloud.r-project.org/bin/linux/ubuntu noble-cran40/
.