如何让我的prolog代码获得最短的路径?

时间:2015-03-14 14:10:09

标签: prolog shortest-path

我写了几行代码来计算最短路径:

:- auto_table.

link(a,b,2).
link(c,b,1).
link(a,c,2).
link(c,d,2).
link(d,b,3).
link(a,e,2).
link(e,b,2).
link(a,f,1).

link(X,Y,C) :- link(Y,X,C).

:- table short_path(_,_,_,po('<'/2)).

% P is the next node in the path
short_path(X, Y, P, C) :-
    link(X, Y, C),
    P = Y.
short_path(X, Y, P, C) :-
    link(X, N, C2),
    short_path(N, Y, _, C3),
    C is C2 + C3,
    P = N.

输出如下:

P = f
C = 6;

P = e
C = 7;

P = b
C = 5;

P = c
C = 4;

我怎样才能以最小C作为输出获得最短路径?

0 个答案:

没有答案