I want to make a program which should give out all possible routes between two stations. The problem I have is that it doesn't give me all routes. My code so far is:
connection(s1,s2).
connection(s2,s3).
connection(s3,s4).
connection(s4,s5).
connection(s5,s1).
connection(s1,s4).
connection(s2,s5).
direction1(X,Y) :- connection(X,Y).
direction2(X,Y) :- connection(Y,X).
route1(X,Y,R):- route1(X,Y,[X],R).
route1(X,Y,_,[X,Y]) :- direction1(X,Y).
route1(X, Y, Visited, Route) :- direction1(X, Z), Z \= Y, \+ member(Z, Visited), route1(Z, Y, [Z|Visited], Route1), Route = [X|Route1].
route2(X,Y,R):- route2(X,Y,[X],R).
route2(X,Y,_,[X,Y]) :- direction2(X,Y).
route2(X, Y, Visited, Route) :- direction2(X, Z), Z \= Y, \+ member(Z, Visited), route2(Z, Y, [Z|Visited], Route2), Route = [X|Route2].
route(X,Y,R) :- route1(X,Y,R); route2(X,Y,R).
For example when I ask for "
?- route(s1,s4,R)
" it only gives me R = [s1, s4]
, R = [s1, s2, s3, s4]
and R = [s1, s5, s4]
.
But there are also the routes (s1,s2,s5,s4) and (s1,s5,s2,s3,s4) and I don't know why I don't get them. How to fix this?
Thanks in advance!