I have written in Prolog:
edge(x, y).
edge(y, t).
edge(t, z).
edge(y, z).
edge(x, z).
edge(z, x).
path(Start, End, Path) :-
path3(Start, End, [Start], Path).
path3(End, End, RPath, Path) :-
reverse(RPath, Path).
path3(A,B,Path,[B|Path]) :-
edge(A,B),
!.
path3(A, B, Done, Path) :-
edge(A, Next),
\+ memberchk(Next, Done),
path3(Next, B, [Next|Done], Path).
Its taking care of cyclic graphs as well, I am getting an irregular output when I try to traverse same node from same node.
eg: path(x,x,P).
expected output should be:
P = [x, z, t, y, x]
P = [x, z, y, x]
P = [x, z, x]
However, I am getting output:
p = [x] ------------> wrong case
P = [x, z, t, y, x]
P = [x, z, y, x]
P = [x, z, x]
How can I get rid of this unwanted case. Thanks
should work