【问题标题】:Getting connected components of graph in Prolog在 Prolog 中获取图的连接组件
【发布时间】:2016-01-16 20:32:07
【问题描述】:

我正在为逻辑编程而苦苦挣扎。我有一个这个问题,我希望你们中的一些人可以帮助我。不连续图用事实表示是这样的:

h(0,1).
h(1,2).
h(3,4).
h(3,5).

所以有两个独立的图形组件。我想要一个列表表示的输出上的所有单独组件。所以如果图中有三个独立的组件,就会有三个列表。对于上面给出的示例,预期的输出是[[0,1,2],[3,4,5]]

【问题讨论】:

  • 所以你基本上是在寻找一个返回[[0,1],[3,4,5]]的谓词?
  • Nono,对不起,我应该更具体一些。 h() 事实仅作为示例。可以有各种边和各种数量的图形组件。重点是,程序应该写下组件列表。所以在这种情况下它是:'[[0,1,2],[3,4,5]]'
  • 您是在寻找基本算法,还是应该高效?
  • 它只需要服务于它的目的。不需要效率。

标签: prolog graph-theory connected-components


【解决方案1】:

此模块计算强连通分量 - 我从 Markus Triska site 得到它。

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   Strongly connected components of a graph.
   Written by Markus Triska (triska@gmx.at), 2011, 2015
   Public domain code.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- module(scc, [nodes_arcs_sccs/3]).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   Usage:

   nodes_arcs_sccs(+Ns, +As, -SCCs)

   where:

   Ns is a list of nodes. Each node must be a ground term.
   As is a list of arc(From,To) terms where From and To are nodes.
   SCCs is a list of lists of nodes that are in the same strongly
        connected component.

   Running time is O(|V| + log(|V|)*|E|).

   Example:

   %?- nodes_arcs_sccs([a,b,c,d], [arc(a,b),arc(b,a),arc(b,c)], SCCs).
   %@ SCCs = [[a,b],[c],[d]].

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- use_module(library(assoc)).

nodes_arcs_sccs(Ns, As, Ss) :-
        must_be(list(ground), Ns),
        must_be(list(ground), As),
        catch((maplist(node_var_pair, Ns, Vs, Ps),
               list_to_assoc(Ps, Assoc),
               maplist(attach_arc(Assoc), As),
               scc(Vs, successors),
               maplist(v_with_lowlink, Vs, Ls0),
               keysort(Ls0, Ls1),
               group_pairs_by_key(Ls1, Ss0),
               pairs_values(Ss0, Ss),
               % reset all attributes
               throw(scc(Ss))),
              scc(Ss),
              true).

% Associate a fresh variable with each node, so that attributes can be
% attached to variables that correspond to nodes.

node_var_pair(N, V, N-V) :- put_attr(V, node, N).

v_with_lowlink(V, L-N) :-
        get_attr(V, lowlink, L),
        get_attr(V, node, N).

successors(V, Vs) :-
        (   get_attr(V, successors, Vs) -> true
        ;   Vs = []
        ).

attach_arc(Assoc, arc(X,Y)) :-
        get_assoc(X, Assoc, VX),
        get_assoc(Y, Assoc, VY),
        successors(VX, Vs),
        put_attr(VX, successors, [VY|Vs]).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   Tarjan's strongly connected components algorithm.

   DCGs are used to implicitly pass around the global index, stack
   and the predicate relating a vertex to its successors.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

scc(Vs, Succ) :- phrase(scc(Vs), [s(0,[],Succ)], _).

scc([])     --> [].
scc([V|Vs]) -->
        (   vindex_defined(V) -> scc(Vs)
        ;   scc_(V), scc(Vs)
        ).

scc_(V) -->
        vindex_is_index(V),
        vlowlink_is_index(V),
        index_plus_one,
        s_push(V),
        successors(V, Tos),
        each_edge(Tos, V),
        (   { get_attr(V, index, VI),
              get_attr(V, lowlink, VI) } -> pop_stack_to(V, VI)
        ;   []
        ).

vindex_defined(V) --> { get_attr(V, index, _) }.

vindex_is_index(V) -->
        state(s(Index,_,_)),
        { put_attr(V, index, Index) }.

vlowlink_is_index(V) -->
        state(s(Index,_,_)),
        { put_attr(V, lowlink, Index) }.

index_plus_one -->
        state(s(I,Stack,Succ), s(I1,Stack,Succ)),
        { I1 is I+1 }.

s_push(V)  -->
        state(s(I,Stack,Succ), s(I,[V|Stack],Succ)),
        { put_attr(V, in_stack, true) }.

vlowlink_min_lowlink(V, VP) -->
        { get_attr(V, lowlink, VL),
          get_attr(VP, lowlink, VPL),
          VL1 is min(VL, VPL),
          put_attr(V, lowlink, VL1) }.

successors(V, Tos) --> state(s(_,_,Succ)), { call(Succ, V, Tos) }.

pop_stack_to(V, N) -->
        state(s(I,[First|Stack],Succ), s(I,Stack,Succ)),
        { del_attr(First, in_stack) },
        (   { First == V } -> []
        ;   { put_attr(First, lowlink, N) },
            pop_stack_to(V, N)
        ).

each_edge([], _) --> [].
each_edge([VP|VPs], V) -->
        (   vindex_defined(VP) ->
            (   v_in_stack(VP) ->
                vlowlink_min_lowlink(V, VP)
            ;   []
            )
        ;   scc_(VP),
            vlowlink_min_lowlink(V, VP)
        ),
        each_edge(VPs, V).

v_in_stack(V) --> { get_attr(V, in_stack, true) }.

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   DCG rules to access the state, using semicontext notation.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

state(S), [S] --> [S].

state(S0, S), [S] --> [S0].

现在我们需要将它与您的格式交互。首先断言事实:

?- [user].
h(0,1).
h(1,2).
h(3,4).
h(3,5).
|: (^D here)

现在是查询 - 请注意,要使图形无向边必须在两个“方向”上检索:

?- setof(N, X^(h(N,X);h(X,N)), Ns), findall(arc(X,Y), (h(X,Y);h(Y,X)), As), nodes_arcs_sccs(Ns,As,SCCs).
Ns = [0, 1, 2, 3, 4, 5],
As = [arc(0, 1), arc(1, 2), arc(3, 4), arc(3, 5), arc(1, 0), arc(2, 1), arc(4, 3), arc(5, 3)],
SCCs = [[0, 1, 2], [3, 4, 5]].

可能值得定义一个服务谓词connected(X,Y) :- h(X,Y) ; h(Y,X). ...

编辑

当然,如果在 module(scc) 中找到的高度优化的实现被认为是多余的,我们可以通过独创性将代码减少到几行,计算一个固定点,特别是考虑现代允许的高级功能Prolog - 带有库(yall)的 SWI-Prolog,在这种情况下:

gr(Gc) :- h(X,Y), gr([X,Y], Gc).
gr(Gp, Gc) :-
    maplist([N,Ms]>>setof(M,(h(N,M);h(M,N)),Ms), Gp, Cs),
    append(Cs, UnSorted),
    sort(UnSorted, Sorted),
    ( Sorted \= Gp -> gr(Sorted, Gc) ; Gc = Sorted ).

被称为

?- setof(G,gr(G),L).
L = [[0, 1, 2], [3, 4, 5]].

【讨论】:

  • 哇。我正在做一些基本的 Prolog 练习,所以我预计应该有 2-3 个基本谓词之类的东西。但是非常感谢。我也会研究这段代码。
  • 对于实现“定点习语”的通用元谓词,您需要哪种 API?不久前,我实现了有点过于简单的fixedpoint/3(参见stackoverflow.com/a/30454790/4609915)....我现在看待事物的方式我更喜欢拥有/使用像fixedpoint(Not_Done_3, P_2, X0,X) :- call(P_2,X0,X1), if_(call(Not_Done_3,X0,X1), fixedpoint(Not_Done_3,P_2,X1,X), X1=X).这样的更通用的实现你怎么看?
  • 有没有不使用属性变量的解决方案?
  • @TransfiniteNumbers: 似乎 gr/2 应该做,它不使用属性。
  • 是的,我看到了。我更多的是参考 Markus Triska 的 SCC 算法。
【解决方案2】:

使用iwhen/2,我们可以像这样定义binrel_connected/2

:- use_module(library(ugraphs)).
:- use_module(library(lists)).

binrel_connected(R_2, CCs) :-
   findall(X-Y, call(R_2,X,Y), Es),
   iwhen(ground(Es), ( vertices_edges_to_ugraph([],Es,G0),
                       reduce(G0,G),
                       keys_and_values(G,CCs,_) )).

在 SICStus Prolog 4.5.0 上使用symm/2symmetric closure 进行示例查询:

| ?- binrel_connected(symm(h), CCs).
CCs = [[0,1,2],[3,4,5]] ? ;
no

【讨论】:

  • ensure_ground/3 不仅确保只有解决方案(基本答案),而且还消除了进一步的答案。事实上,对于ensure_ground(p,X,X),它确实成功了!与p(a,1,1). p(a,_,_).
  • 更糟糕的是,即使p(a,_,_). p(a,1,1). 也会成功。
  • @false。正确的!谢谢!
  • iwhen 这个名字代表什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-05
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多