igraph / rigraph

igraph R package
https://r.igraph.org
557 stars 202 forks source link

`adjacent_vertices()` is off by 1 when option `return.vs.es` is `FALSE` #1605

Closed stibu81 closed 2 days ago

stibu81 commented 3 days ago

What happens, and what did you expect instead?

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

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/.

R version 4.4.2 (2024-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=de_CH.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=de_CH.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=de_CH.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=de_CH.UTF-8 LC_IDENTIFICATION=C       

time zone: Europe/Zurich
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] igraph_2.1.1.9904

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       cli_3.6.3         knitr_1.49        rlang_1.1.4       xfun_0.49        
 [6] processx_3.8.4    glue_1.8.0        clipr_0.8.0       htmltools_0.5.8.1 ps_1.8.1         
[11] fansi_1.0.6       rmarkdown_2.29    evaluate_1.0.1    tibble_3.2.1      fastmap_1.2.0    
[16] yaml_2.3.10       lifecycle_1.0.4   compiler_4.4.2    fs_1.6.5          pkgconfig_2.0.3  
[21] rstudioapi_0.17.1 digest_0.6.37     R6_2.5.1          reprex_2.1.1      utf8_1.2.4       
[26] pillar_1.9.0      callr_3.7.6       magrittr_2.0.3    tools_4.4.2       withr_3.0.2