Hamiltonian cycle instantiation and encoding:
% vertices: n=node
n(1..4).
% edges: e=edge
e(1,(2;3)). e(2,(3;4)). e(3,(1;4)). e(4,1).
% starting point
s(1).
# p=path, o=omit, op=on-path, r=reach, s=start% generate path
p(X,Y):- not o(X,Y), e(X,Y).
o(X,Y):- not p(X,Y), e(X,Y).
% at most one incoming/outgoing edge
:- p(X,Y), p(U,Y), X < U.
:- p(X,Y), p(X,V), Y < V.
% at least one incoming/outgoing edge
op(Y):- p(X,Y), p(Y,Z).
:- n(X), not op(X).
% connectedness
r(X):- s(X).
r(Y):- r(X), p(X,Y).
:- n(X), not r(X).
Solver: ! clingo file_name.lp
Return:
clingo version 5.6.2
Reading from encoding.lp
Solving...
UNSATISFIABLE
Models : 0
Calls : 1
Time : 0.002s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)
CPU Time : 0.000s
My question is: is this program really unsatisfiable or am I encoding it the wrong way?
No, it is not unsatisfiable. There exists indeed exactly one Hamiltonian cycle from
a
, which is clearlya->b->c->d(->a)
.You are just declaring the nodes using numbers, but the starting node is
a
(not a number).The part in which you encoded the logics of the problem looks ok to me.
Replacing
s(a)
withs(1)
we obtainSATISFIABLE
as result:P.S. Note that the two following lines:
can be compacted with: