I am curious about pathfinding algorithms, so I took a look at Dijkstra's. I'm using this video as a guide (and it's what I'm basing my graph on). Here is the graph I am working from:
I now want to be able to find all connections of a given vertex. I think that I should use findall
for this, which I tried to use in the all_connections
goal below. However, with main
, my output is [b,b,b,b]
. Why is that happening? That makes no sense. If you understand what I am doing wrong please let me know.
connection(s, c, 3).
connection(c, l, 2).
connection(l, i, 4).
connection(l, j, 4).
connection(i, j, 6).
connection(i, k, 4).
connection(j, k, 4).
connection(k, e, 5).
connection(e, g, 2).
connection(g, h, 2).
connection(h, f, 3).
connection(f, d, 5).
connection(d, a, 4).
connection(b, d, 4).
connection(b, a, 3).
connection(b, s, 2).
connection(b, h, 1).
connection(a, s, 7).
are_connected(A, B, Score) :-
connection(A, B, Score);
connection(B, A, Score).
all_connections(A, Z) :-
findall(A, are_connected(A, _, _), Z).
main :-
all_connections(b, X),
write(X).