Shortest path in prolog
Here are some example facts, where the first two letters represents the end points of a line and the number represents the length of that line.
line(a,b,10).
line(a,d,7).
line(a,e,13).
line(a,c,4).
line(b,d,23).
line(b,e,5).
Now I want to write a query to find an end point closest to "a" (basically the shortest line with "a" as one of the end points). I can''t seem to think of a way of doing this, but eventually searching through the net, I found a solution, which I modifed to this:
line(a,X,Length1), \+(( line(a,Y,Length2), Length2 < Length1 )).
This query works perfectly, and it will display only one answer:
X = c.
However, I do not understand why this works... If we take Length1 = 7 and Length2 = 13, then shouldn''t this also work and return X = d. But the query only seems to display one answer and that is X = c.
Why? Anyone who is an expert in prolog please explain this to me, cause this is driving me nuts.
Thanks.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement