tidygraph icon indicating copy to clipboard operation
tidygraph copied to clipboard

root and leaf in 2-1 tree

Open dmi3kno opened this issue 6 years ago • 0 comments

When tree consists of one root and one leaf, node_is_root() and node_is_leaf() get lost:

library(tidygraph)
create_tree(2, 1) %>% mutate(node_is_root(), node_is_leaf())
#> # A tbl_graph: 2 nodes and 1 edges
#> #
#> # A rooted tree
#> #
#> # Node Data: 2 x 2 (active)
#>   `node_is_root()` `node_is_leaf()`
#>   <lgl>            <lgl>           
#> 1 T                T               
#> 2 F                F               
#> #
#> # Edge Data: 1 x 2
#>    from    to
#>   <int> <int>
#> 1     1     2

Results are opposite(but also wrong) if the tree grows inward:

library(tidygraph)

create_tree(2, 1, mode = "in") %>% mutate(node_is_root(), node_is_leaf())
#> # A tbl_graph: 2 nodes and 1 edges
#> #
#> # A rooted tree
#> #
#> # Node Data: 2 x 2 (active)
#>   `node_is_root()` `node_is_leaf()`
#>   <lgl>            <lgl>           
#> 1 F                F               
#> 2 T                T               
#> #
#> # Edge Data: 1 x 2
#>    from    to
#>   <int> <int>
#> 1     2     1

These simple trees may be part of the forest or may end up as such after pruning, so it is a realistic example.

Issue is in how node_is_root() and node_is_leaf() decide the "mode" of the graph - by simply comparing degree() calculated inwards and outwards.

dmi3kno avatar Mar 12 '18 22:03 dmi3kno