I am writing the following code and is giving perfect results.
edge(s,a,300).
edge(s,d,20).
edge(a,d,400).
edge(a,b,1500).
edge(b,c,9).
edge(b,e,200).
edge(c,d,2000).
edge(c,g,12).
edge(d,e,3).
edge(e,f,400).
edge(f,g,800).
connected(X,Y,D) :- edge(X,Y,D) ; edge(Y,X,D).
path(A,B,D,Path) :-
travel(A,B,D,[A],Q),
reverse(Q,Path).
travel(A,B,D,P,[B|P]) :-
connected(A,B,D).
travel(A,B,D,Visited,Path) :-
connected(A,X,D1),
X \== B,
\+member(X,Visited),
D2 is D - D1,
travel(X,B,D2,[X|Visited],Path).
Here if I query like
| ?- path(s,e,23,P).
P = [s,d,e] ? ;
no
| ?-
I do get the correct response. But no I wish to get the results for a D<50, say. How to do?
You should let Prolog compute the distance D as well as the Path:
travel(A,B,D,Visited,Path) :-
connected(A,X,D1),
X \== B,
\+member(X,Visited),
travel(X,B,D2,[X|Visited],Path),
D is D2 + D1.
Then you can query it
?- path(Start, Stop, D, P), D < 50.
and will get (on backtracking) all paths with D < 50.
?- path(s,e,X,P),X<50.
X = 23,
P = [s, d, e] ;
false.