0

I am trying to write a predicate that writes out all the ways from one node to another in a acyclic graph. If I example have these nodes/edges.

node(x,y).
node(x,z).
node(y,z).

And then I have tried something like:

predicate(X,Y):-
    node(X,Y),
    ...

but somehow I need to do this recursively which I need help with. Anyone got any ideas? Thanks

false
  • 10,264
  • 13
  • 101
  • 209
Fjodor
  • 529
  • 1
  • 7
  • 19
  • 2
    There are lots of posts regarding graphs in Prolog on Stackoverflow. You should try doing a search "[prolog] acyclic graphs". – lurker Oct 07 '14 at 12:45

1 Answers1

0

It should work well in this maybe. (But I'm not so familiar with Prolog.)

route(X, Z, [])    :-  node(X, Z).
route(X, Z, [Y|T]) :-  node(X, Y), route(Y, Z, T).

and then

?- findall(E, route(x, z, E), Routes).
% ==> Routes = [[], [y]].

+ EDIT

print_all_routes(X, Y) :- findall(E, (route(X, Y, E), write(E), nl), _).
findall
  • 2,176
  • 2
  • 17
  • 21
  • Seems to work but I am interested in writing out the paths inside the program. So all you would've to enter is start = X and end = Y. – Fjodor Oct 07 '14 at 12:52
  • It is nice to rewrite the query to such a predicate. I've edit my answer. – findall Oct 07 '14 at 13:27