找到所有可能的路径而无需重新访问

时间:2012-11-01 03:08:44

标签: prolog

我需要一个谓词路由,它可以在所有城市之间启动&结束。例如:

path(chicago,atlanta).
path(chicago,milwaukee).
path(milwaukee,detroit).
path(milwaukee,newyork).
path(chicago,detroit).
path(detroit, newyork).
path(newyork, boston).
path(atlanta,boston).
path(atlanta, milwaukee).

?- routing(chicago,newyork,X).
X=[chicago,milwaukee,newyork];
X=[chicago,detroit,newyork];
X=[chicago,milwaukee,detroit,newyork];
X=[chicago,atlanta,milwaukee,newyork];
X=[chicago,atlanta,milwaukee,detroit,newyork]

我试过这个,并继续回过头来。

routing(FromCity,ToCity,[FromCity|ToCity]) :-
  path(FromCity,ToCity).

routing(FromCity,ToCity,[FromCity|Connections]) :- 
  path(FromCity,FromConnection), 
  path(FromConnection,ToConnection),
  path(ToConnection,ToCity),
  routing(ToConnection,ToCity,Connections).

routing(FromCity,ToCity,[]).

但它只是继续给予

X=[chicago,milwaukee,newyork];
X=[chicago,chicago,newyork];
X=[chicago,chicago,chicago,newyork]
...
..

有人可以指出我正确的方向......

3 个答案:

答案 0 :(得分:4)

如果您确定(根据定义)您的图表是非循环的,您可以简化您的规则,利用Prolog深度优先搜索:

routing(FromCity, ToCity, [FromCity, ToCity]) :-
  path(FromCity, ToCity).

routing(FromCity, ToCity, [FromCity|Connections]) :- 
  path(FromCity, ToConnection),
  routing(ToConnection, ToCity, Connections).

这可以找到回溯的所有可用路径:

?- routing(chicago,newyork,X).
X = [chicago, atlanta, milwaukee, newyork] ;
X = [chicago, atlanta, milwaukee, detroit, newyork] ;
X = [chicago, milwaukee, newyork] ;
X = [chicago, milwaukee, detroit, newyork] ;
X = [chicago, detroit, newyork] ;
false.

注意列表构造的第一种和第二种模式之间的区别:[FromCity, ToCity] vs [FromCity|Connections]。这是因为Connections将是list,而ToCity将成为原子,当规则成功时。

如果图表包含循环,则此代码将循环播放。您可以参考another answer获取处理此问题的简单架构。

答案 1 :(得分:1)

如何进行如下操作?

首先,我们选择一个比path更好的谓词名称。 edge怎么样?

edge(chicago  , atlanta  ).
edge(chicago  , milwaukee).
edge(milwaukee, detroit  ).
edge(milwaukee, newyork  ).
edge(chicago  , detroit  ).
edge(detroit  , newyork  ).
edge(newyork  , boston   ).
edge(atlanta  , boston   ).
edge(atlanta  , milwaukee).

如上所述,edge/2显然不是symmetric,否则以下查询将无法成功!

?- edge(X, Y), \+ edge(Y, X).
  X = chicago  , Y = atlanta
; X = chicago  , Y = milwaukee
; X = milwaukee, Y = detroit
; X = milwaukee, Y = newyork
; X = chicago  , Y = detroit
; X = detroit  , Y = newyork
; X = newyork  , Y = boston
; X = atlanta  , Y = boston
; X = atlanta  , Y = milwaukee.

接下来,我们将connected_to/2定义为edge/2

的对称闭包
connected_to(X, Y) :- edge(X, Y).
connected_to(X, Y) :- edge(Y, X).

最后,我们将 path/4connected_to/2一起使用:

?- path(connected_to, Path, From, To).
; From = To                   , Path = [To]
; From = chicago, To = atlanta, Path = [chicago,atlanta]
; From = chicago, To = boston , Path = [chicago,atlanta,boston]
; From = chicago, To = newyork, Path = [chicago,atlanta,boston,newyork]
...

那么...... path/4connected_to/2)的最一般查询是否会普遍终止?

?- path(connected_to, Path, From, To), false.
false.                                 % terminates universally

最后,让我们计算不同地面路径的总数:

?- setof(P, From^To^(path(connected_to,P,From,To),ground(P)), _Ps),
   length(_Ps, N_Ps).
N_Ps = 244.

答案 2 :(得分:0)

这是我的解决方案,适用于定向或无向图,有或没有循环。

它还试图找到所有路径,而无需重新访问。

c(1,2).
% ... c(X,Y) means X and Y are connected

d(X,Y):- c(X,Y).
d(X,Y):- c(Y,X).
% Use d instead of c to allow undirected graphs

findPathHelper(_, Begin, [], End):- d(Begin, End).
findPathHelper(Front, Begin, [Next|NMiddle], End):-
    not(member(Begin,Front)),
    d(Begin, Next),
    append([Front,[Begin]], NFront),
    findPathHelper(NFront, Next, NMiddle, End).

findPath(Start, End, Path):-
    findPathHelper([], Start, Middle, End),
    append([[Start],Middle,[End]], Path).