How to handle a path in Prolog graph traversal
问题 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).