I need to write a program in prolog for finding paths, for example, for the graph:
edge(a, b).
edge(a, c).
edge(c, b).
and the test case are:
/* test case 1 */
?- path(a, b, P).
P = [a, b] ;
P = [a, c, b] ;
false.
/* test case 2 */
?- path(c,b,[c,b]).
true.
and my code is
path(X,Y,[X,Y]):-
edge(X,Y).
path(X,Z,[X|P]):-
edge(X,Y),
path(Y,Z,P).
However, for the test case 2, my code will show
?- path(c,b,[c,b]).
true;
false.
I know that I should add a cut in my code to remove the false in case 2, but it will remove the false in case 1 too. How can I solve this problem?