dgraph_leaves: all leaves in a directed graph (linear time).

This commit is contained in:
Vítor Santos Costa 2012-03-22 21:41:48 +00:00
parent 4484dec044
commit a0d2e6f285
2 changed files with 18 additions and 0 deletions

View File

@ -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

View File

@ -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).