Context, first. I have the following bi-directional graph.
Represented in Prolog like this:
relation(a,c).
relation(b,d).
relation(c,d).
relation(d,e).
relation(e,f).
connection(X,Y) :- relation(X,Y).
connection(X,Y) :- relation(Y,X).
So I have the relation between the nodes, and then the connections between the nodes related, as I said before, in both directions.
What I am looking to do is a prolog definition path(X,Y)
capable to tell if there's a path in the graph between two nodes (giving true if, at least, one path exists between both nodes, and false if there's no existing paths between both nodes).
So, the goal output in this model should look like this:
?- path(a,d).
true.
?- path(b,a).
true.
?- path(f,b).
true
? path(a,g). % The node g doesn't exist
false.
I know this involves using a visited list, and I had seen examples of this, but giving all the posible paths between two nodes. However, this isn't what I'm looking for. What I'm looking is a definition which detects if there is a path between two nodes, not to give all the posible paths between two nodes.
Edit: So, thanks to @mbratch, I can now adapt the suggested problem to a solution:
relation(a,c).
relation(b,d).
relation(c,d).
relation(d,e).
relation(e,f).
connection(X,Y) :- relation(X,Y).
connection(X,Y) :- relation(Y,X).
path_exists(X,Y) :- path(X,Y,_), !.
path(A,B,Path) :-
travel(A,B,[A],Q),
reverse(Q,Path).
travel(A,B,P,[B|P]) :-
connection(A,B).
travel(A,B,Visited,Path) :-
connection(A,C),
C \== B,
\+member(C,Visited),
travel(C,B,[C|Visited],Path).