From a0d2e6f2853d3c9af6da9ff8be6748b1ad11e064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Thu, 22 Mar 2012 21:41:48 +0000 Subject: [PATCH] dgraph_leaves: all leaves in a directed graph (linear time). --- docs/yap.tex | 7 +++++++ library/dgraphs.yap | 11 +++++++++++ 2 files changed, 18 insertions(+) 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).