0

I have defined the following network:

connected(d,f).
connected(d,a).
connected(d,b).
connected(d,c).
connected(c,a).
connected(c,b).
connected(c,e).
connected(b,e).

connected(X,Y) :- 
    edge(X,Y).
connected(X,Y) :- 
    edge(Y,X).

I need to find all paths between two specified nodes and write them out. I have found a previous post that gives a way to check if there is a path between two nodes but I cannot think of a way how to write out the paths.

path(A,B) :-   % two nodes are connected, if
  walk(A,B,[]) % - if we can walk from one to the other,
  .            % first seeding the visited list with the empty list

walk(A,B,V) :-       % we can walk from A to B...
  edge(A,X) ,        % - if A is connected to X, and
  not(member(X,V)) , % - we haven't yet visited X, and
  (                  % - either
    B = X            %   - X is the desired destination
  ;                  %   OR
    walk(X,B,[A|V])  %   - we can get to it from X
  )                  %
  .                  % Easy!
  • [`?- path(connected, Path, A,B).`](https://stackoverflow.com/q/30328433/772868) and rename all facts from `connected` to `edge`. – false Dec 21 '21 at 20:09
  • Hey. Thanks for the response. I don't really understand what you mean. Rename all 'connected' to 'edge'? Then why did you include 'connected' to 'path'? – Petras Nasvytis Dec 21 '21 at 20:41
  • Not all 'connected' but all **facts** named 'connected'. The two rules are fine. – false Dec 21 '21 at 20:56

0 Answers0