Map colouring

Description

A program for colouring maps.

Source: PrologTutorial (on-line tutorial

Download

Program source code: map_colouring.pl

Listing

/* prolog  tutorial 2.9 Map coloring redux */
 
 
 
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). 
 
 
 
map1([[1,2],[1,3],[1,4],[1,5],[2,3],[2,4],[3,4],[4,5]] ).
 
 
 
% ?- color([[1,2],[1,3],[1,4],[1,5],[2,3],[2,4],[3,4],[4,5]],[red,green,blue,yellow],Coloring).

Comments

pl/prolog/pllib/map_colouring.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0