I have a non-cyclical graph that can be considered a tree. Here's a simplified example:
library(tidygraph)
create_tree(20,2, directed = TRUE, mode="in") %>% plot
The real-life example can be a little more complicated, as I may have multiple paths from leaves to the root (all of them directed non-cyclical).
I want to simplify the graph by removing intermediate nodes as follows:
K=0
In most extreme case (lets call it "k=0" simplification) I would enumerate all leaves, assure that they are connected to the root through depth-first search and then remove all intermediate connections, effectively linking every leaf to the root.
K=-1
The next level simplification (say "k=-1") I want to start with nodes that have at least one leaf child and repeat the same procedure. After simplification, all intermediate nodes will be removed:
data.frame(from=c(5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20),
to = c(1,1,1,1,1, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10)) %>%
as_tbl_graph() %>% plot
K=-2
The next step of simplification would not make sense for this graph, because no edges would be modified and no nodes would be removed.
How do I code it up using igraph
/tidygraph
in R?