cmput325-w23-livecoding/asp/clp.pl

28 lines
645 B
Prolog

:- use_module(library(clpfd)).
vertex(1).
vertex(2).
vertex(3).
link(1, 2).
link(1, 3).
link(2, 3).
arc(X, Y) :- link(X, Y).
arc(X, Y) :- link(Y, X).
coloring(Colors) :-
findall(V, vertex(V), Vertices),
length(Vertices, N),
same_length(Colors, Vertices),
Colors ins 0..N, % At most N different colors
maplist(constrain1(Vertices, Colors), Vertices, Colors),
sum(Colors, #=, Sum),
labeling([min(Sum)], [Sum | Colors]).
constrain1(Vertices, Colors, Vertex, Color) :-
maplist(constrain2(Vertex, Color), Vertices, Colors).
constrain2(V1, C1, V2, C2) :-
arc(V1, V2), !,
C1 #\= C2.
constrain2(_, _, _, _).