Reputation: 891
is there a way to find all accessible vertices of an igraph in R, like the function acc in the graph package? I can only find a function for adjacent vertices in an igraph but not accessible vertices.
To give some context, I have a hierarchical graph, and I want to find all nodes below. I can do this with the acc function for a graph object. However, acc doesn't work with igraph
nodesBelow <- function(graph, nodes) {
sub <- character()
for(node in nodes){
sub <- c(sub, c(names(acc(graph, node)[[1]]),node))
}
sub <- unique(sub)
subGraph(sub, graph)
}
Upvotes: 4
Views: 1464
Reputation: 7232
There is subcomponent
- example:
g1 <- graph.tree(n = 8, children = 2, mode = "out" )
print.igraph(g1,full = TRUE)
# IGRAPH D--- 8 7 -- Tree
# + attr: name (g/c), children (g/n), mode (g/c)
# + edges:
# [1] 1->2 1->3 2->4 2->5 3->6 3->7 4->8
subcomponent(g1, 2, mode = "out")
#[1] 2 4 5 8
Upvotes: 6