Four-color theorem in Prolog (using a dynamic predicate)
Posted
by outa
on Stack Overflow
See other posts from Stack Overflow
or by outa
Published on 2010-06-13T14:36:35Z
Indexed on
2010/06/13
14:42 UTC
Read the original article
Hit count: 235
Hi,
I'm working on coloring a map according to the four-color theorem (http://en.wikipedia.org/wiki/Four_color_theorem) with SWI-Prolog. So far my program looks like this:
colour(red).
colour(blue).
map_color(A,B,C) :- colour(A),
colour(B),
colour(C),
C \= B,
C \= A.
(the actual progam would be more complex, with 4 colors and more fields, but I thought I'd start out with a simple case)
Now, I want to avoid double solutions that have the same structure. E.g. for a map with three fields, the solution "red, red, blue" would have the same structure as "blue, blue, red", just with different color names, and I don't want both of them displayed.
So I thought I would have a dynamic predicate solution/3, and call assert(solution(A,B,C)) at the end of my map_color predicate. And then, for each solution, check if they already exist as a solution/3 fact. The problem is that I would have to assert something like solution(Color1,Color1,Color2), i.e. with variables in order to make a unification check. And I can't think of a way to achieve this.
So, the question is, what is the best way to assert a found solution and then make a unification test so that "red, red, blue" would unify with "blue, blue, red"?
© Stack Overflow or respective owner