小编bas*_*sta的帖子

查找图中节点之间的路径及其长度

我正在尝试解决这个问题,我已经阅读了这个答案,但我的问题是无限循环,即使我使用了访问节点列表。

让我们看看我的两次尝试:

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 …
Run Code Online (Sandbox Code Playgroud)

graph-theory prolog shortest-path non-termination failure-slice

5
推荐指数
1
解决办法
2766
查看次数