I am new to prolog and I am stuck on this map coloring problem.
adjacent(X,Y,Map) :- member([X,Y],Map) ; member([Y,X],Map).
find_regions([],R,R).
find_regions([[X,Y] |S], R,A) :-
(member(X,R) ->
(member(Y,R) -> find_regions(S,R,A) ; find_regions(S,[Y|R],A));
(member(Y,R) -> find_regions(S,[X|R],A) ; find_regions(S,[X,Y|R],A))).
color(Map,Colors,Coloring) :-
find_regions(Map, [], Regions),
color_all(Regions,Colors,Coloring),
\+ conflict(Map,Coloring).
color_all([R|Rs],Colors,[[R,C]|A]) :-
member(C,Colors),
color_all(Rs,Colors,A).
color_all([],_,[]).
conflict(Map,Coloring) :-
member([R1,C],Coloring),
member([R2,C],Coloring),
adjacent(R1,R2,Map).
The Query that I run to solve the map coloring problem is:
color([[1,2],[2,3][3,4],[3,5]],[red,green,blue,yellow],Coloring).
Where the first argument is to input the regions that are adjacent and the second is for colors.
Instead of returning the available coloring scheme it returns false.
I'm fairly new to prolog so I apologize if I use any incorrect terminology. However, I ran your query on swish.swi-prolog.org and I got the following when I traced it:
Call:color([[1, 2], []([3, 4],[2, 3]), [3, 5]],[red, green, blue, yellow],_9040)
Call:find_regions([[1, 2], []([3, 4],[2, 3]), [3, 5]],[],_904)
Call:lists:1:[]
Fail:lists:1:[]
Upon some further investigation, I noticed that your query is missing a comma. Try the query:
color([[1,2],[2,3],[3,4],[3,5]],[red,green,blue,yellow],Coloring).
I hope this helps!