Anytime strongly connected components via Prolog

回眸只為那壹抹淺笑 提交于 2020-01-25 09:18:04

问题


Markus Triska has reported an algorithm to determine strongly connected components (SCC). Is there a solution which determines the SCC without making use of attributed variable and that can work anytime. So that some vertices can have infinitely many edges?

I am asking because I am wondering whether B-Prologs anytime tabling which they call eager can be replicated. B-Prolog determines clusters which is their name for SCC. But it has also a tabling mode where it returns tabled results eagerly.


回答1:


I guess this algorithm fits the bill, since with a little modification it would allow an infinite degree in the root. This is the SCC algorithm when all edges point to finitely many vertices:

% plan(+Atom, -Assoc)
plan(P, R) :-
  sccforpred(P, [], [], R, _).

% sccforpred(+Atom, +List, +Assoc, -Assoc, -List)
sccforpred(P, S, H, H, [P]) :-
  member(P, S), !.
sccforpred(P, _, H, H, []) :-
  member(Q-L, H), (Q = P; member(P, L)), !.
sccforpred(P, S, I, O2, R2) :-
  findall(Q, edge(P, Q), L),
  sccforlist(L, [P|S], I, O, R),
  (member(U, R), member(U, S) ->
      O2 = O, R2 = [P|R];
      O2 = [P-R|O], R2 = []).

% sccforlist(+List, +List, +Assoc, -Assoc, -List)
sccforlist([], _, H, H, []).
sccforlist([P|Q], S, I, O, R) :-
  sccforpred(P, S, I, H, R1),
  sccforlist(Q, S, H, O, R2),
  findall(U, (member(U, R1); member(U, R2)), K),
  sort(K, R).

Using this example graph:

I get this result:

Jekejeke Prolog 4, Runtime Library 1.4.1 (August 20, 2019)

?- plan(21,X).
X = [21-[], 22-[22, 23], 26-[24, 25, 26], 27-[]]


来源:https://stackoverflow.com/questions/58573038/anytime-strongly-connected-components-via-prolog

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