prolog无向图线问题

时间:2016-11-28 22:15:29

标签: prolog

大家好我有问题

我有一张非定向图enter image description here enter image description here

我已经以节点(name,[path])的形式列出了每个节点。 其中具有多个路径的节点是node(name,[pathName,path])。 //但是路径可能与节点相交。

>node(giant,[sizePath]).
>node(large,[sizePath]).
>node(big,[sizePath]).
>node(a0,[numPath,alphabetPath]).
>node(1,[numPath]).
>node(2,[numPath]).
>node(3,[numPath]).
>node(b,[alphabetPath]).
>node(c,[alphabetPath]).
>node(d,[alphabetPath]).

然后我写了一条说明

的规则
samePath(Node1,Node2,PathName):-node(Node1,PathName),node(Node,1PathName).
  

如果我使用值samePath(2,3,PathName),我会得到输出

true; 
numLine.
     

但是,如果我使用值samePth(A0,1,PathName),我应该

true;
numLine.
     

但是我得到了

true;
fail.

因为它不会仅列出两个列表中匹配的元素

1 个答案:

答案 0 :(得分:0)

您正在比较列表而不是列表元素 - 要使其正常工作,您应该修改samePath以阅读

samePath(Node1,Node2,PathName) :-
    node(Node1,Paths1),
    node(Node2,Paths2),
    member(PathName,Paths1),
    member(PathName,Paths2).

这将统一PathName与两个列表中的任何路径。

相关问题