图形路径定义问题 [英] Graph path define issue

查看:48
本文介绍了图形路径定义问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决办法

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).

这是我的代码.问题是起点是a,终点是z.

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

执行后出现错误,结果显示为[z, c, h, b].但正确答案应该是 [a,b,c,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].

请帮助解决我的问题.

推荐答案

library(聚合) 允许对最小/最大标量操作进行 witness.我们可以使用该功能来报告路径和行程长度:

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)).

注意 edge/3 中有一个拼写错误:edge(b,e,16 应该是 edge(b,e,16)..

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].

这篇关于图形路径定义问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆