I have a simple tree graph with directed edges:
In: AdjacencyList[Graph[{a -> b, a -> c, b -> d, b -> e}], b]
Out: {a, d, e}
I wonder how I can select only the outgoing edge vertices elegantly.
{d, e}
In other words: I want the children of a vertex only.
The obvious solution for a tree is to always drop the first vertex. Sadly this approach fails for the root vertex, so there has to be some If-branching - which I want to avoid.
So far I tried VertexOutComponent and IncidenceList but non of them seems to give the option to filter by edge direction.
Do you see an elegant approach there, or will I have to keep using root checks?
Answer
VertexOutComponent gives
VertexOutComponent[g, b, 1]
(* {b, d, e} *)
This is precisely what you are looking for, plus the vertex b itself.
To retrieve the full adjacency list of a directed graph, you can also use IGAdjacencyList from IGraph/M.
g = Graph[{a -> b, a -> c, b -> d, b -> e}];
IGAdjacencyList[g, "Out"]
(* <|a -> {b, c}, b -> {d, e}, c -> {}, d -> {}, e -> {}|> *)
IGAdjacencyList[g, "In"]
(* <|a -> {}, b -> {a}, c -> {a}, d -> {b}, e -> {b}|> *)
Comments
Post a Comment