Find the path and its length between nodes in the graph

I am trying to solve this problem and I already read this answer, but my problem is related to the infinte loop, even if I used the visited node.

Let's look at my two attempts:

edge(1,2).
edge(1,4).
edge(1,3).
edge(2,3).
edge(2,5).
edge(3,4).
edge(3,5).
edge(4,5).


% ------ simple path finding in a directed graph

% -----  simple exploration

path0(A,B, Result) :-
    path0(A, B, [], Result).

path0(A, B, _, [e(A,B)]):- 
    edge(A,B).    
path0(A, B, Visited, [e(A,X)|Path]):-
    edge(A, X), dif(X, B),
    \+ member(X, Visited),
    path0(X, B, [A|Visited], Path ).    


%---- 1. exploration and length    

path(A, B, _, [e(A,B)], 1):- 
    edge(A,B).    
path(A, B, Visited, [e(A,X)|Path], Length):-
    edge(A, X),
    \+ member(X, Visited),
    length(Path, L),        % ERR: Path refers to a open list
    Length is L + 1,
    path(X, B, [A|Visited], Path, _).

% --- 2. not working

path2(A,B, Result, Length) :-
    path2(A, B, [], Result, Length).

path2(A, B, [], [e(A,B)], 1):- 
    edge(A,B).    
path2(A, B, Visited, [e(A,X)|Path], Length):-
    edge(A, X), dif(X, B),
    \+ member(X, Visited),
    path2(X, B, [A|Visited], Path, Len),
    Length is Len + 1.

Which gives me similar answers, i.e.:

 ?- path(1,3, Path, Length).
Path = [e(1, 3)],
Length = 1 ;
Path = [e(1, 2), e(2, 3)],
Length = 2 ;

And then the Swi-Prolog development environment freezes.

  • What should I define as a base case?
  • Why is the second run loop, if it is, even if I used the list of visited node and diff () to go back and forth? I was mistaken for the function name.

I would like to get rid of using length / 2. Thank.

Edit:

, , , - , , . {pathLengths} 3/4.

% ---- 3. working    
%   
min(A,B,A):- A =< B, !.       % for future use (shortest path)
min(_,B,B).

path3(From, To, Path, Len):-    
    path0(From, To, [], Path),
    length(Path, Len).
    %min(Len, MinLength, ?)

2:

% --- 2. 
% errors: 1. in base case I have to return Visited trough _, 
%             I can't pass a void list []
%         2. dif(X,B) is unuseful since base case it the first clause

path2(A,B, Result, Length) :-
    path2(A, B, [], Result, Length).

path2(A, B, _, [e(A,B)], 1):-      % If an edge is found
    edge(A,B).    
path2(A, B, Visited, [e(A,X)|Path], Length):-
    edge(A, X),  
    %tab(1),write(A),write('-'),write(X),
    \+ member(X, Visited),
    %tab(1),write([A|Visited]),write(' visited'),nl,
    path2(X, B, [A|Visited], Path, Len),
    Length is Len + 1.
+4
1

, path/4 path2/4 , , path/5. , path2/5:

path2(A,B, Result, Length) :-
    path(A, B, [], Result, Length).
 %  ^^^^ replace by path2

, , path/4. , false . . , , , . , failure-slice:

edge(1,2).
edge(1,4) :- false.
edge(1,3) :- false.
edge(2,3) :- false.
edge(2,5) :- false.
edge(3,4) :- false.
edge(3,5) :- false.
edge(4,5) :- false.

path(A,B, Result, Length) :-
    path(A, B, [], Result, Length), false.

path(A, B, _, [e(A,B)], 1):- false,
    edge(A,B).
path(A, B, Visited, [e(A,X)|Path], Length):-
    edge(A, X),
    \+ member(X, Visited),
    length(Path, L), false,
    Length is L + 1,
    path(X, B, [A|Visited], Path, _).

, length/2. , . ,

?- path(1, 3, Path, N).

Path , , length/2 - , , .

, , ? path .

path/4,5 ,

?- path(1, X, Path, N).

. Path = [1] ? /. , .

. . , :

yourpath(A,B, Path, N) :-
   path(edge, Path, A,B),
   length(Path, N).

. .

+3

All Articles