<div class="notebook"> <div class="nb-cell markdown" name="md1"> # Map colouring (after Bratko, "Prolog Programming for AI" p190) _To find a way of colouring a map using only 4 colours in such a way that no pair of neighbouring countries are the same colour_. Define a predicate implementing the query below that binds `X` to a list of terms (_pairs_) of the form `Country-Color`, e.g., ``` X=[austria-red,belgium-green,denmark-blue,france-red, italy-yellow,netherlands-blue,portugal-blue, spain-yellow,switzerland-blue,w_germany-yellow] ``` We give away a little start. The predicate `country_colors` creates the term in which we have to fill out the variables representing the colors. We also give the start for the recursive definition you need to complete the coloring. We also give two predicates one of which you may want to use to avoid two neighbouring countries being assigned the same color: - [[(\+)/1]] - [[dif/2]] Here `\+ true` _fails_ and after `dif(X,Y)`, `X=red, Y=red` _fails_. </div> <div class="nb-cell program" name="p2"> color_countries(Pairs) :- country_colors(Pairs), colors(Pairs). colors([]). colours([Country-Color|Pairs]) :- <please complete> </div> <div class="nb-cell query" data-tabled="true" name="q1"> color_countries(Pairs). </div> <div class="nb-cell program" name="p3"> country_colors(Pairs) :- setof(Country-_, X^ngb(Country,X), Pairs). </div> <div class="nb-cell query" name="q2"> country_colors(Pairs). </div> <div class="nb-cell program" data-background="true" name="p1"> % SWISH extension to show a list of Country-Color pairs as an HTML table. :- use_rendering(table, [header('Country'-'Color')]). %% ngb(?Country, ?Neighbours). % % Determines the list of Neighbours of a Country. ngb(portugal, [spain]). ngb(spain, [portugal,france]). ngb(france, [spain,belgium,switzerland,w_germany,italy]). ngb(belgium, [france,w_germany,netherlands]). ngb(netherlands, [belgium,w_germany]). ngb(w_germany, [netherlands,belgium,france,switzerland,austria,denmark]). ngb(switzerland, [france,w_germany,austria,italy]). ngb(austria, [w_germany,switzerland,italy]). ngb(italy, [france,switzerland,austria]). ngb(denmark, [w_germany]). color(yellow). color(blue). color(red). color(green). </div> </div>