Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8321123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:54:33+00:00 2026-06-08T22:54:33+00:00

The question is simple. How can I struct my Graph in SWI prolog to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:54:36+00:00Added an answer on June 8, 2026 at 10:54 pm

    That implementation isn’t so bad:

    ?- time(dijkstra(penzance, Ss)).
    % 3,778 inferences, 0,003 CPU in 0,003 seconds (99% CPU, 1102647 Lips)
    Ss = [s(aberdeen, 682, [penzance, exeter, bristol, birmingham, manchester, carlisle, edinburgh|...]), s(aberystwyth, 352, [penzance, exeter, bristol, swansea, aberystwyth]), s(birmingham, 274, [penzance, exeter, bristol, birmingham]), s(brighton, 287, [penzance, exeter, portsmouth, brighton]), s(bristol, 188, [penzance, exeter, bristol]), s(cambridge, 339, [penzance, exeter|...]), s(cardiff, 322, [penzance|...]), s(carlisle, 474, [...|...]), s(..., ..., ...)|...].
    

    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:

    /*  File:    dijkstra_av.pl
        Author:  Carlo,,,
        Created: Aug  3 2012
        Purpose: learn graph programming with attribute variables
    */
    
    :- module(dijkstra_av, [dijkstra_av/3]).
    
    dijkstra_av(Graph, Start, Solution) :-
        setof(X, Y^D^(member(d(X,Y,D), Graph)
                 ;member(d(Y,X,D), Graph)), Xs),
        length(Xs, L),
        length(Vs, L),
        aggregate_all(sum(D), member(d(_, _, D), Graph), Infinity),
        catch((algo(Graph, Infinity, Xs, Vs, Start, Solution),
               throw(sol(Solution))
              ), sol(Solution), true).
    
    algo(Graph, Infinity, Xs, Vs, Start, Solution) :-
        pairs_keys_values(Ps, Xs, Vs),
        maplist(init_adjs(Ps), Graph),
        maplist(init_dist(Infinity), Ps),
        ord_memberchk(Start-Sv, Ps),
        put_attr(Sv, dist, 0),
        time(main_loop(Vs)),
        maplist(solution(Start), Vs, Solution).
    
    solution(Start, V, s(N, D, [Start|P])) :-
        get_attr(V, name, N),
        get_attr(V, dist, D),
        rpath(V, [], P).
    
    rpath(V, X, P) :-
        get_attr(V, name, N),
        (   get_attr(V, previous, Q)
        ->  rpath(Q, [N|X], P)
        ;   P = X
        ).
    
    init_dist(Infinity, N-V) :-
        put_attr(V, name, N),
        put_attr(V, dist, Infinity).
    
    init_adjs(Ps, d(X, Y, D)) :-
        ord_memberchk(X-Xv, Ps),
        ord_memberchk(Y-Yv, Ps),
        adj_add(Xv, Yv, D),
        adj_add(Yv, Xv, D).
    
    adj_add(X, Y, D) :-
        (   get_attr(X, adjs, L)
        ->  put_attr(X, adjs, [Y-D|L])
        ;   put_attr(X, adjs, [Y-D])
        ).
    
    main_loop([]).
    main_loop([Q|Qs]) :-
        smallest_distance(Qs, Q, U, Qn),
        put_attr(U, assigned, true),
        get_attr(U, adjs, As),
        update_neighbours(As, U),
        main_loop(Qn).
    
    smallest_distance([A|Qs], C, M, [T|Qn]) :-
        get_attr(A, dist, Av),
        get_attr(C, dist, Cv),
        (   Av < Cv
        ->  (N,T) = (A,C)
        ;   (N,T) = (C,A)
        ),
        !, smallest_distance(Qs, N, M, Qn).
    smallest_distance([], U, U, []).
    
    update_neighbours([V-Duv|Vs], U) :-
        (   get_attr(V, assigned, true)
        ->  true
        ;   get_attr(U, dist, Du),
            get_attr(V, dist, Dv),
            Alt is Du + Duv,
            (   Alt < Dv
            ->  put_attr(V, dist, Alt),
            put_attr(V, previous, U)
            ;   true
            )
        ),
        update_neighbours(Vs, U).
    update_neighbours([], _).
    
    :- begin_tests(dijkstra_av).
    
    test(1) :-
        nl,
        time(dijkstra_av([d(a,b,1),d(b,c,1),d(c,d,1),d(a,d,2)], a, L)),
        maplist(writeln, L).
    
    test(2) :-
        open('salesman.pl', read, F),
        readf(F, L),
        close(F),
        nl,
        dijkstra_av(L, penzance, R),
        maplist(writeln, R).
    
    readf(F, [d(X,Y,D)|R]) :-
        read(F, dist(X,Y,D)), !, readf(F, R).
    readf(_, []).
    
    :- end_tests(dijkstra_av).
    

    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

    % 2,278 inferences, 0,003 CPU in 0,003 seconds (97% CPU, 747050 Lips)
    s(aberdeen,682,[penzance,exeter,bristol,birmingham,manchester,carlisle,edinburgh,aberdeen])
    ....
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A simple question, but I haven't found a definitive answer on Stack Overflow. struct
I have a feeling this may be stupid question, but I can't come to
This is a simple question but I don't know how to construct a variable
The question look simple but i can't find any information on Firebreath's wiki page
This is quite a long introduction to a simple question, but otherwise there will
This is presumable a simple C++ question, but I'm relearning C++ and don't know
I am sorry if this is a really simple question but I am really
Suppose I have some simple struct like this: public struct WeightedInt { public int
I have rather simple, I imagine, question about CRTP , but I cannot seem
I apologize for the lengthy code. I have a simple question, but I thought

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.