0

I am trying to generate all paths between two nodes, where each edge can only be visited once.

Here is my ASP code:

% Define the input: nodes and edges
node(a). node(b). node(c). node(d).
edge(a,b). edge(b,c). edge(c,d),edge(d,a).

% Define the output: two nodes are connected if there is a path between them
connected(X,Y) :- path(X,Y,_).

% Define the base case for the path: a node is connected to itself
path(X,X,0):- node(X).

% Define the recursive case for the path: there is a path between two nodes if there is an edge between them
path(X,Y,1) :- edge(X,Y).

% Define the recursive case for the path: there is a path between two nodes if there is a path between X and some other node Z, and there is an edge between Z and Y
path(X,Y,K) :- K > 1, edge(X,Z), path(Z,Y,K-1).

This works completely fine in case it is acyclic graph, however it doesn't terminate in case of cyclic one. I don't know, how to fix the path rule to make it visit edge only once.

I should get path(a,d,4).

logi-kal
  • 7,107
  • 6
  • 31
  • 43

0 Answers0