graph-theory - 在数据记录中添加循环边缘 (bddbddb)

标签 graph-theory identity datalog transitive-closure

我有以下规则来查找图形中的所有路径。

path(X,Y) :- edge(X,Y).
path(X,Y) :- edge(X,Z), path(Z,Y).

但是,我还希望对于每个节点 n添加边 n->n(如果尚不存在)。

在没有任何节点关系的情况下如何做到这一点?

最佳答案

仅在 Prolog 中,您可以编写:

path(X,X).
path(X,Y) :- false, edge(X,Y).
path(X,Y) :- edge(X,Z), path(Z,Y).

or somewhat shorter and more terminating using closure0/3.

path(X,Y) :-
   closure0(edge, X,Y).

In (many definitions of) Datalog as a subset of Prolog, facts like path(X,X). are not permitted, since they admit an infinite set of solutions.

So you need to restrict that fact to a finite set of nodes. However, since you do not have an explicit definition of nodes, you need to make up one based on the edges you have:

node(X) :-
   edge(X,_).
node(Y) :_
   edge(_,Y).

现在导致:

path(X,X) :-
   node(X).
path(X,Y) :-
   edge(X,Z),
   path(Z,Y).

关于graph-theory - 在数据记录中添加循环边缘 (bddbddb),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36333169/

相关文章:

python - 与 PyDatalog 的逻辑析取

使用列表的 Prolog 到 Datalog 代码转换

python - python中无向图中的连通分量

prolog - 在prolog中遍历无向图

c++ - 使用非递归 dfs 检测有向图中的循环

python - 使用 python w/rest api 与 Azure Key Vault 交互

algorithm - 优化图中节点之间的连接

asp.net-identity - AspnetIdentity 在哪里存储 UserToken?

c# - 有没有办法在我的 ASP.Net MVC 5 应用程序之间传递 Windows 身份验证 session ?

z3 - Z3 中的固定点