`I want to print all the ways to reach Node e from Node a in a weighted unidrected acyclic graph. but my program misses a few long routes.
This is my program.
%Declare all the edges
edge(a,b,1).
edge(a,c,6).
edge(b,c,4).
edge(b,d,3).
edge(b,e,1).
edge(c,d,1).
edge(d,e,1).
% Two nodes are adjacent if there exists a edge between them
adj(X, Y, W) :- edge(X, Y, W).
adj(Y, X, W) :- edge(Y, X, W).
% return 0 weight and empty path because path is empty to reach the same node
% from the node itself and therefor no wirght as well.
findpath(X, X, 0, []).
% if X and Y are adjacent then append Y to X into the Path Variable
findpath(X,Y,Weight,Path) :- adj(X,Y,Weight),
append([X],[Y],Path).
% if X and Y are not adjacent, then consider nodes who are adjacent to Y with
% weight W2 , recursively call path predicate replacing Y with A and W1 with W2
% add their weight - W as W1 + W2 and append to Path.
findpath(X,Y,Weight,Path) :- not(adj(X,Y,Weight)),
edge(A,Y,Weight1),
findpath(X,A,Weight2,P1),
Weight is Weight1+Weight2,
append(P1,[Y],Path).
Output
`2 ?- findpath(a, e, W, P).
W = 2,
P = [a, b, e] ;
W = 2,
P = [a, b, e] ;
W = 5,
P = [a, b, d, e] ;
W = 5,
P = [a, b, d, e] ;
W = 8,
P = [a, c, d, e] ;
W = 8,
P = [a, c, d, e] ;
false.
My program missed a, b, c, e and a, b, c, d ,e. I dont understand why ? Also , it repeats the output as well.`