I am trying to wrote a predicate isway(A, B) which should work for cyclic graphs. The expected outcome I am trying to get is, if there a path exists between 2 nodes or not. example: if I ask isway(a, f), expect it to answer either yes or no. below is my code but it never works and always gives me false as an output.
%given facts
edge(a,b).
edge(a,c).
edge(b,c).
edge(c,d).
edge(c,e).
edge(d,e).
edge(f,g).
edge(g,h).
edge2(d,a).
edge2(h,f).
edge2(X,Y) :- edge(X,Y).
%my implementation
isway(A,B) :- edge2(A,B,[]).
edge2(A,B,V) :- edge(A,X), not(member(X,V)), (B = X ; edge2(X,B,[A|V])).
%my output
?- isway(b,a). %I expect true for this one
false
?- isway(a,f). %False for this is ok but I am not confident about my logic
false.
You can use tabling for computing the transitive closure of the edge relation:
Then, the queries work as you expect: