I have the following super simple digraph tree (code is Elixir):
digraph = :digraph.new()
coords = [{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}]
[v0, v1, v2] = (for c <- coords, do: :digraph.add_vertex(digraph, c))
:digraph.add_edge(digraph, v0, v1)
:digraph.add_edge(digraph, v1, v2)
We can see it is a tree:
iex(82)> :digraph_utils.is_tree(digraph)
true
But how do I find the root of vertex of the tree in an efficient way? Currently I'm doing this:
iex(83)> Enum.filter(:digraph.vertices(digraph), fn x -> :digraph.in_degree(digraph, x) == 0 end)
[{0.0, 0.0}]
Is this the most efficient way of finding the root vertex, that is, by finding the vertex with no in-edges?