thomasp85 / tidygraph

A tidy API for graph manipulation
https://tidygraph.data-imaginist.com
Other
546 stars 61 forks source link

dfs_rank seems to give wrong results with v1.3.1 #190

Open teng-gao opened 7 months ago

teng-gao commented 7 months ago

Hi, I have a graph G_m that looks like this:

IGRAPH baf9167 D--- 4 3 -- 
+ attr: last_mut (v/c), .orig_data (v/x), .tidygraph_node_index (v/x),
| label (v/c), id (v/n), node (v/c), GT (v/c), clone (v/n),
| .tidygraph_edge_index (e/x), .orig_data (e/x), label (e/c),
| from_label (e/c), to_label (e/c), from_node (e/c), to_node (e/c)
+ edges from baf9167:
[1] 1->2 2->3 2->4

When I do:

G_m %>% as_tbl_graph %>% mutate(rank = dfs_rank(root = 3))

Output:

# A tbl_graph: 4 nodes and 3 edges
#
# A rooted tree
#
# Node Data: 4 × 9 (active)
  last_mut  .orig_data .tidygraph_node_index label    id node  GT    clone  rank
  <chr>     <list>     <list>                <chr> <int> <chr> <chr> <int> <int>
1 ""        <tibble>   <int [609]>           ""        1 NA    ""        1    NA
2 "10a,6e,… <tibble>   <int [2]>             "10a…     2 Node… "10a…     2    NA
3 "10c,6a,… <tibble>   <int [610]>           "10c…     3 Node3 "10a…     3    NA
4 "15a,4d,… <tibble>   <int [972]>           "15a…     4 Node4 "10a…     4    NA
#
# Edge Data: 3 × 9
   from    to .tidygraph_edge_index .orig_data       label   from_label to_label
  <int> <int> <list>                <list>           <chr>   <chr>      <chr>   
1     1     2 <int [2]>             <tibble [2 × 5]> ->10a,… ""         10a,6e,…
2     2     3 <int [2]>             <tibble [2 × 5]> 10a,6e… "10a,6e,1… 10c,6a,…
3     2     4 <int [2]>             <tibble [2 × 5]> 10a,6e… "10a,6e,1… 15a,4d,…
# ℹ 2 more variables: from_node <chr>, to_node <chr>

I think the correct output should be rank = 1 for row 3 (the root node itself should be accessible via dfs). If I use base igraph dfs

igraph::dfs(G_m, root = 3, unreachable = F)

I get:

$order
+ 1/4 vertex, from baf9167:
[1] 3

This wasn't an issue in tidygraph_1.2.3.

> sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3 
LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3;  LAPACK version 3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: America/New_York
tzcode source: system (glibc)

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

other attached packages:
[1] igraph_2.0.2    tidygraph_1.3.1 dplyr_1.1.3    

loaded via a namespace (and not attached):
 [1] tidyr_1.3.0      utf8_1.2.4       R6_2.5.1         tidyselect_1.2.0
 [5] magrittr_2.0.3   glue_1.6.2       tibble_3.2.1     pkgconfig_2.0.3 
 [9] generics_0.1.3   lifecycle_1.0.3  cli_3.6.1        fansi_1.0.5     
[13] vctrs_0.6.4      compiler_4.3.1   tools_4.3.1      purrr_1.0.2     
[17] pillar_1.9.0     rlang_1.1.1