I'm new to Prolog. I'm trying to search simple pathes using prolog, but only base cases(length 1 or 0) were found. What's wrong? Code
I rewrited the recursion part referencing several examples which perform dfs or list manipulation.
I'm new to Prolog. I'm trying to search simple pathes using prolog, but only base cases(length 1 or 0) were found. What's wrong? Code
I rewrited the recursion part referencing several examples which perform dfs or list manipulation.
Use the following implementation as a starting point: Definition of a path/trail/walk
Just copy the whole "Implementation" section to your program.
Then, the rest is the definition of your graph. You can do it like this:
arc(a,b).
arc(c,d).
arc(b,d).
n(A,B) :-
( arc(A,B)
; arc(B,A)
).
This is it. You can now try:
?- path(n, Path, a, B).
Path = [a],
B = a ;
Path = [a, b],
B = b ;
Path = [a, b, d],
B = d ;
Path = [a, b, d, c],
B = c ;
false.
?- path(n, Path, A, d).
Path = [d],
A = d ;
Path = [a, b, d],
A = a ;
Path = [c, d],
A = c ;
Path = [b, d],
A = b ;
false.
?- path(n, Path, _, _).
Path = [_] ;
Path = [a, b] ;
Path = [a, b, d] ;
Path = [a, b, d, c] ;
Path = [c, d] ;
Path = [c, d, b] ;
Path = [c, d, b, a] ;
Path = [b, d] ;
Path = [b, d, c] ;
Path = [b, a] ;
Path = [d, c] ;
Path = [d, b] ;
Path = [d, b, a] ;
false.