2

My assignment: Graph coloring - Write an answer set program to check if a graph had a 4-coloring and, if it does, the answer set should contain the coloring. If there isn't a coloring, it wont have an answer set. The program should consist of the code to do the 4-coloring and a sample graph representation.

I have this (from ASP Graph Coloring Wikipedia), but I don't understand how exactly it works - could someone please explain this code?

c(1..n).                                           
1 {color(X,I) : c(I)} 1 :- v(X).             
:- color(X,I), color(Y,I), e(X,Y), c(I).

v(1..100). % 1,...,100 are vertices
e(1,55). % there is an edge from 1 to 55
DuDa
  • 3,718
  • 4
  • 16
  • 36

1 Answers1

1

The code says:

c(1..n). 

You have the facts c(1), c(2), ... c(n) which represent the colors. n is previously given, in your case n=4.

1 {color(X,I) : c(I)} 1 :- v(X).      

For every vertex v(X) exists exactly one assignment color from X to a possible c (color).
a {...} b means that the ... occurs between a and b times.
So it means: every vertex has exactly one color. Not more, not less.

:- color(X,I), color(Y,I), e(X,Y), c(I).

Rules starting with :- are constraints: the missing head says the body implies false and therefore the body has to be false as well. It reads:
It can not be the case that there is an edge e from X to Y where X and Y have the same color I.
Or in other words: neighboring vertices have different colors.

To get an example graph you add the vertices and egdes you need.

DuDa
  • 3,718
  • 4
  • 16
  • 36