1

我正在继续一些格子和半格子的研究,突然有这个问题。

基本上,我们有一个 [a,b] 对的 RelationList,这意味着 (a,b) 是一条边。现在我们应该知道,这个RelationList 1-connectivity 是否形成了一个图。顺便说一句,我们有一个有序图,所以 (a,b) 的顺序很重要。

clear_link(X, Y, RelationList) :-
    (member([X,Y], RelationList)
    ;
    member([Y,X], RelationList)),
    X =\= Y.

linked(X, Y, RelationList) :-
    clear_link(X, Y, RelationList),
    !.
linked(X, Y, RelationList) :-
    clear_link(X, Z, RelationList),
    linked(Z, Y, RelationList).

simple_connect(RelationList, E) :-
    forall((member(X, E),
    member(Y, E), X < Y),
    linked(X, Y, RelationList)).

但是,对于 6 元素图,我有 stackoverflow。

?- simple_connect([[2,1],[2,3],[4,3],[4,5],[6,5]],[1,2,3,4,5,6]).
ERROR: Out of local stack

我定义错了吗?

4

1 回答 1

2

我纠正了一些。现在好了

clear_link(X, Y, RelationList) :-
    member([X,Y], RelationList),
    X =\= Y.

linked(X, Y, RelationList) :-
    clear_link(X, Y, RelationList),
    !.
linked(X, Y, RelationList) :-
    clear_link(X, Z, RelationList),
    linked(Z, Y, RelationList),
    !.

simple_connect(RelationList, E) :-
    forall((member(X, E),
    member(Y, E), X < Y),
    linked(X, Y, RelationList)).

connective_graph(RelationList, E) :-
    findall(Relation, (
        member(X, RelationList),
        sort(X, Relation)
    ),SortRelationList),
    simple_connect(SortRelationList, E).

?- connective_graph([[2,1],[2,3],[4,3],[4,5],[6,5]],[1,2,3,4,5,6]).
true.

?- connective_graph([[2,1],[4,3],[4,5],[6,5]],[1,2,3,4,5,6]).
false.

正确答案(复制到帖子)

connected(X, Y, RelationList) :-
    (member([X,Y], RelationList);
    member([Y,X], RelationList)).

path(X, Y, RelationList, Path) :-
    travel(X, Y, RelationList, [X], ReversePath),
    reverse(ReversePath, Path),!.

travel(X, Y, RelationList, Point, [Y | Point]) :-
    connected(X, Y, RelationList).
travel(X, Y, RelationList, Visited, Path) :-
    connected(X, Z, RelationList),
    Z =\= Y,
    \+member(Z, Visited),
    travel(Z, Y, RelationList, [Z|Visited], Path).

connective_graph(RelationList, E) :-
    forall((member(X, E),
        member(Y, E),
        X < Y)
    ,path(X,Y,RelationList,_)).
于 2011-02-19T18:26:16.623 回答