diff --git a/docs/yap.tex b/docs/yap.tex index 5709a446c..0d7ef8ea6 100644 --- a/docs/yap.tex +++ b/docs/yap.tex @@ -12635,6 +12635,13 @@ The path @var{Path} is a path starting at vertex @var{Vertex} in graph The path @var{Path} is a path starting at vertex @var{Vertex} in graph @var{Graph}. +@item dgraph_leaves(+@var{Graph}, ?@var{Vertices}) +@findex dgraph_leaves/2 +@snindex dgraph_leaves/2 +@cnindex dgraph_leaves/2 +The vertices @var{Vertices} have no outgoing edge in graph +@var{Graph}. + @end table @node UnDGraphs, Lambda , DGraphs, Library diff --git a/library/dgraphs.yap b/library/dgraphs.yap index 7dfb1d320..e081bc3bd 100644 --- a/library/dgraphs.yap +++ b/library/dgraphs.yap @@ -32,6 +32,7 @@ dgraph_min_paths/3, dgraph_isomorphic/4, dgraph_path/3, + dgraph_leaves/2, dgraph_reachable/3 ]). @@ -414,3 +415,13 @@ reachable([V|Vertices], Done0, DoneF, G, [V|EdgesF], Edges0) :- rb_insert(Done0, V, [], Done1), reachable(Kids, Done1, DoneI, G, EdgesF, EdgesI), reachable(Vertices, DoneI, DoneF, G, EdgesI, Edges0). + +dgraph_leaves(Graph, Vertices) :- + rb_visit(Graph, Pairs), + vertices_without_children(Pairs, Vertices). + +vertices_without_children([], []). +vertices_without_children((V-[]).Pairs, V.Vertices) :- + vertices_without_children(Pairs, Vertices). +vertices_without_children(_V-[_|_].Pairs, Vertices) :- + vertices_without_children(Pairs, Vertices).