This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/CLPBN/clpbn/topsort.yap
2008-10-31 09:39:26 +00:00

28 lines
605 B
Prolog

:- module(topsort, [topsort/2]).
:- use_module(library(dgraphs),
[dgraph_new/1,
dgraph_add_edges/3,
dgraph_top_sort/2]).
/* simple implementation of a topological sorting algorithm */
/* graph is as Node-[Parents] */
topsort(Graph0, Sorted) :-
mkedge_list(Graph0, EdgeList, []),
dgraph_new(DGraph0),
dgraph_add_edges(DGraph0, EdgeList, DGraph1),
dgraph_top_sort(DGraph1, Sorted).
mkedge_list([]) --> [].
mkedge_list([V-Parents|More]) -->
add_edges(Parents, V),
mkedge_list(More).
add_edges([], _V) --> [].
add_edges([P|Parents], V) --> [P-V],
add_edges(Parents, V).