I'm very new to Prolog. I defined in graph.pl
the following graph:
And here's my Prolog code:
edge(a,e).
edge(e,d).
edge(d,c).
edge(c,b).
edge(b,a).
edge(d,a).
edge(e,c).
edge(f,b).
path(X,X).
path(X,Y):- edge(X,Z) ; path(Z,Y).
I understand it like this: there is a path between vertex X
and vertex Y
only if there is an edge between vertex X
and vertex Z
AND there is a path between vertex Z
and vertex Y
(some kind of recursion).
Is that right for the presented graph? When I ask Prolog about the path between vertex A
and vertex F
it gives me true
... which isn't even right! What might be wrong in this code?