Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shortest path in prolog

Started by LordG Nov 30, 2002 at 4:53 PM 0 replies 4.1k views
Original Post
LordG
LordG
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.
_Teran_
_Teran_
quote:
Original post by LordG
line(a,X,Length1), \+(( line(a,Y,Length2), Length2 < Length1 )).



Note that predicate \+ is negation. This whole query will succeed only if no vertex Y can be found that would be closer than X.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.