在Prolog中获取图形的连接组件 [英] Getting connected components of graph in Prolog
本文介绍了在Prolog中获取图形的连接组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力进行逻辑编程.我有一个问题,希望大家能帮助我.不连续图通过事实以这种方式表示:
I'm struggling with logic programming. I have a this problem and I hope some of you can help me with it. Discontinous graph is represented by facts in this way:
h(0,1).
h(1,2).
h(3,4).
h(3,5).
因此,有两个单独的图形组件.我想要列表表示的输出上的所有单独组件.因此,如果图中有三个单独的组件,那么将有三个列表.对于上面给定的示例,预期输出为[[0,1,2],[3,4,5]]
.
So there is two separate graph components. I would like all the separate components on the output represented by a list. So if there is three separate components in the graph, there will be three lists. For the given example above, the expected output is [[0,1,2],[3,4,5]]
.
推荐答案
使用 iwhen/2
,我们可以定义
Using iwhen/2
we can define binrel_connected/2
like so:
:- 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,_) )).