4
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ With g = Graph[{a -> b, a -> c, b -> d, b -> e}]; you can do Last /@ EdgeList[g, b \[DirectedEdge] _]. $\endgroup$ – Öskå Jan 12 '15 at 14:49
  • $\begingroup$ What did you try with VertexOutComponent? "but non of them seems to give the option to filter by edge direction" $\leftarrow$ actually it does, that's why it has "Out" in the name. $\endgroup$ – Szabolcs Jan 12 '15 at 15:03
  • $\begingroup$ @Szabolcs I find this whole implementation really confusing. Why doesn't an AdjacencyList for a directed graph include only vertices that are adjacent to the vertex, not also those it is adjacent from. Wouldn't that be more standard? (E.g., xlinux.nist.gov/dads/HTML/adjacencyListRep.html) Somewhat similarly for the VertexOutComponent: why does the result for a vertex include that vertex when there is no self loop? Is there a standard reference that justifies this vocabulary? $\endgroup$ – Alan Oct 2 '15 at 16:07
6
$\begingroup$

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}|> *)
$\endgroup$
  • $\begingroup$ Ah, thank you very much. What confused me is that the vertex itself is included - as in my understanding its not exactly an out component. What I use now is Drop[VertexOutComponent[g, b, 1], 1]. This truly returns all vertices from out directed edges of b. $\endgroup$ – Thomas Fankhauser Jan 13 '15 at 15:31
  • $\begingroup$ Aren't those duplicates? mathematica.stackexchange.com/questions/97044/… $\endgroup$ – Kuba Nov 17 '15 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.