/*
K4P2 Graceful Graph in Picat.
Problem from Tailor/Minion summer_school/examples/K4P2GracefulGraph.eprime
Also see
http://mathworld.wolfram.com/GracefulGraph.html
Model created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import cp.
main => go.
go =>
M = 16,
N = 8,
graph(Graph),
graceful_graph(Graph, M,N,Nodes,Edges),
writeln(nodes=Nodes),
writeln(edges=Edges).
% Just count the number of solutions
go2 =>
M = 16,
N = 8,
graph(Graph),
L = findall(_, $graceful_graph(Graph, M,N,_Nodes,_Edges)),
writef("Number of solutions: %d\n", L.length).
graceful_graph(Graph, M,N,Nodes,Edges) =>
Nodes = new_list(N),
Nodes :: 0..M,
Edges = new_list(M),
Edges :: 1..M,
all_different(Edges),
all_different(Nodes),
foreach({[From,To],Edge} in zip(Graph,Edges))
abs(Nodes[From] - Nodes[To]) #= Edge
end,
Vars = Nodes ++ Edges,
solve(Vars).
graph(Graph) =>
Graph =
[[1, 2],
[1, 3],
[1, 4],
[2, 3],
[2, 4],
[3, 4],
[5, 6],
[5, 7],
[5, 8],
[6, 7],
[6, 8],
[7, 8],
[1, 5],
[2, 6],
[3, 7],
[4, 8]].