198:314 Principles of Programming Languages
Fall 2002
Prolog Assignment:
Map Coloring, an Example of Generate and Test


10:00pm December 3, 2002
Deleted part of assignment mistakenly left in by edit earlier today. Deleted section marked in red.

9:36am December 3, 2002
Updated file with corrected adjacencies for EU in euadjacent.pl. Note change in the assignment shown in blue below.

Overview

There is a famous proven conjecture in graph theory that any map divided into countries can be colored with 4 colors such that no two adjacent countries have the same color. We'll call this the "no same colors share a border" rule. We are going to write a Prolog program that produces all such colorings for the countries of the European Union, using red, yellow, blue, and green.

Generate and Test

A standard paradigm for Prolog programs is called generate and test. This means that we write a Prolog program (i.e., a set of rules) that searches a space of possible solutions, generates a candidate solution and then tests that this candidate has the right properties to be an acceptable solution of our problem. The backtracking execution of Prolog allows us to do this in a very simple, but not always efficient, fashion. For example, say we want to calculate all pairs of distinct numbers that we can form from the numbers {1 2 2 3}. First, we might generate all pairs as candidate solutions: (1,2) (1,3) (2,2) (2,3) and then test if each of these is a pair of distinct numbers. Only (1,2) (1,3) and (2,3) pass our test; these are the three acceptable solutions. This is an inefficient way of doing the job, as it would have been better to have seen at pair generation time that (2,2) is not a valid pair and thus to have avoided generating it. For problems with a large candidate solution space relative to the acceptable solution space, this inefficiency can make the Prolog program take much more time than is necessary to solve the problem. In Prolog, usually it is very easy to code the naive generation solution procedure; with some effort we can get the better solution procedure as well.

A substantial example of using (naive) generate and test can be found in N queens program. This program places N queens on an N by N chessboard, so that no queen can attack any other queen. Our solution solves this problem for 4 queens on an 4 by 4 chess board. Try running this program with the query: queens(X). and observe the solutions generated.

Assignment Definition

This assignment has two parts. In the first part, you will run the code we give you in naive_color.pl and euadjacent.pl which uses the naive generate and test approach described above to color the countries of the European Union (i.e., EU) whose adjacencies are given as facts in euadjacent.pl. For example,

 adj(fr,sp). 

means that France and Spain share a border. Note that adjacency is a symmetric relation, which means if France is adjacent to Spain then Spain is adjacent to France. You will perform some experiments with this code and answer some questions about it.

A coloring is a list of country/color pairs. For example, the Prolog list [(fr,red),(sp,blue)] stands for the coloring where France is red and Spain is blue. The Prolog clauses in our files naive_coloring.pl and euadjacent.pl use the naive approach of generating a coloring for an entire list of countries and then checking that this coloring is valid for all adjacencies. The rule is that no two adjacent countries can have the same color; that is, the list [(fr,red),(sp,red)] is an illegal coloring.

In the second part, you will write a smart coloring predicate, which prevents colorings from being generated which violate the "no same colors share a border" rule, and thus incorporates the test as part of the candidate solution generation. You can use any predicates from the N queens program (mentioned above) that you think may be useful, (e.g., not).

Specifics