|
You can do this without the intermediate step of generating all permutations simply like so: order([]).
order([_]).
order([X,Y|L]) :-
follows(Y, X), order([Y|L]).
?- length(L, 9), order(L).
L = [prancer, cupid, rudolph, dasher, blitzen, vixen, comet, donder, dancer] .
This is likely more efficient as we're cutting short the generation of most permutations.Or you can use CPL(FD) as suggested by @Avshalom below, though more heavyweight this is likely more efficient still. The most efficient though is simply to use a topological sort algorithm, which will run in linear time, unlike any of these solutions (some of which are exponential). SWI Prolog has this built-in: ?- findall(X-Y, is_behind(Y, X), Edges), vertices_edges_to_ugraph([], Edges, UG), top_sort(UG, L).
L = [prancer, cupid, rudolph, dasher, blitzen, vixen, comet, donder, dancer].
|