The question is simple.
How can I struct my Graph in SWI prolog to implement the Dijkstra’s algorithm?
I have found this but it’s too slow for my job.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That implementation isn’t so bad:
SWI-Prolog offers attributed variables, then this answer could be relevant to you.
I hope I will post later today an implementation of dijkstra/2 using attribute variables.
edit well, I must say that first time programming with attribute variables is not too much easy.
I’m using the suggestion from the answer by @Mat I linked above, abusing of attribute variables to get constant time access to properties attached to data as required of algorithm. I’ve (blindly) implemented the wikipedia algorithm, here my effort:
To be true, I prefer the code you linked in the question. There is an obvious point to optimize, smallest_distance/4 now use a dumb linear scan, using an rbtree the runtime should be better. But attributed variables must be handled with care.
time/1 apparently show an improvement
but the graph is too small for any definitive assertion. Let we know if this snippet reduce the time required for your program.
File salesman.pl contains dist/3 facts, it’s taken verbatim from the link in the question.