Graph path define issue

做~自己de王妃 提交于 2019-12-11 07:49:57

问题


The solution

ppath(X,Y,M,Path,[Y|Path]) :- edge(X,Y,M),\+ memberchk(Y,Path).
path(X,Y,P,SoFar,Path) :- edge(X,W,M), \+ memberchk(W,SoFar),
    path(W,Y,N,[W|SoFar],Path), P is M+N.
 pravilo(X,Y,Z) :-
    aggregate(min(W), P^path(X,Y,W,[],P),Z).

Here is the code i have. The question is that starting point is a, and ending point is z.

There is an error after execution, the result is displayed like [z, c, h, b]. But the correct answer should [a,b,c,z].

Please help to fix my problem.


回答1:


library(aggregate) allows for a witness on min/max scalar operations. We can use that feature to report back the path as well as the travel length:

path(X,Y,M,Path,FullPath) :-
    edge(X,Y,M), \+ memberchk(Y,Path),
    reverse([Y|Path], FullPath).
path(X,Y,P,SoFar,Path) :-
    edge(X,W,M), \+ memberchk(W,SoFar),
    path(W,Y,N,[W|SoFar],Path), P is M+N.

pravilo(X,Y,Z,Path) :-
    aggregate(min(W,P), P^path(X,Y,W,[X],P), min(Z,Path)).

Note there is a typo in edge/3: edge(b,e,16 should be edge(b,e,16)..

Once corrected the DB, I get

pravilo(a,z,M,P).
M = 16,
P = [a, b, h, c, z].


来源:https://stackoverflow.com/questions/19045248/graph-path-define-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!