5 constraint satisfaction problems - semantic scholar file5 constraint satisfaction problems in...

24
5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention of a range of powerful new search methods and a deeper understanding of problem structure and complexity. Chapters 3 and 4 explored the idea that problems can be solved by searching in a space of states. These states can be evaluated by domain-specific heuristics and tested to see whether they are goal states. From the point of view of the search algorithm, however, each state is a black box with no discernible internal structure. It is represented by an arbi- BLACK BOX trary data structure that can be accessed only by the problem-specific routines—the successor function, heuristic function, and goal test. This chapter examines constraint satisfaction problems, whose states and goal test conform to a standard, structured, and very simple representation (Section 5.1). Search al- REPRESENTATION gorithms can be defined that take advantage of the structure of states and use general-purpose rather than problem-specific heuristics to enable the solution of large problems (Sections 5.2– 5.3). Perhaps most importantly, the standard representation of the goal test reveals the struc- ture of the problem itself (Section 5.4). This leads to methods for problem decomposition and to an understanding of the intimate connection between the structure of a problem and the difficulty of solving it. 5.1 C ONSTRAINT S ATISFACTION P ROBLEMS Formally speaking, a constraint satisfaction problem (or CSP) is defined by a set of vari- CONSTRAINT SATISFACTION PROBLEM ables, X 1 ,X 2 ,...,X n , and a set of constraints, C 1 ,C 2 ,...,C m . Each variable X i has a VARIABLES CONSTRAINTS nonempty domain D i of possible values. Each constraint C i involves some subset of the DOMAIN VALUES variables and specifies the allowable combinations of values for that subset. A state of the problem is defined by an assignment of values to some or all of the variables, {X i = v i ,X j = ASSIGNMENT v j ,...}. An assignment that does not violate any constraints is called a consistent or legal CONSISTENT assignment. A complete assignment is one in which every variable is mentioned, and a so- lution to a CSP is a complete assignment that satisfies all the constraints. Some CSPs also require a solution that maximizes an objective function. OBJECTIVE FUNCTION 137

Upload: vophuc

Post on 26-Aug-2019

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

5 CONSTRAINTSATISFACTION PROBLEMS

In which we see how treating states as more than just little black boxes leads to theinvention of a range of powerful new search methods and a deeper understandingof problem structure and complexity.

Chapters 3 and 4 explored the idea that problems can be solved by searching in aspace of states. These states can be evaluated by domain-specific heuristics and tested tosee whether they are goal states. From the point of view of the search algorithm, however,each state is a black box with no discernible internal structure. It is represented by an arbi-BLACK BOX

trary data structure that can be accessed only by the problem-specific routines—the successorfunction, heuristic function, and goal test.

This chapter examines constraint satisfaction problems, whose states and goal testconform to a standard, structured, and very simple representation (Section 5.1). Search al-REPRESENTATION

gorithms can be defined that take advantage of the structure of states and use general-purposerather than problem-specific heuristics to enable the solution of large problems (Sections 5.2–5.3). Perhaps most importantly, the standard representation of the goal test reveals the struc-ture of the problem itself (Section 5.4). This leads to methods for problem decompositionand to an understanding of the intimate connection between the structure of a problem andthe difficulty of solving it.

5.1 CONSTRAINT SATISFACTION PROBLEMS

Formally speaking, a constraint satisfaction problem (or CSP) is defined by a set of vari-CONSTRAINTSATISFACTIONPROBLEM

ables, X1,X2, . . . ,Xn, and a set of constraints, C1, C2, . . . , Cm. Each variable Xi has aVARIABLES

CONSTRAINTS nonempty domain Di of possible values. Each constraint Ci involves some subset of theDOMAIN

VALUES

variables and specifies the allowable combinations of values for that subset. A state of theproblem is defined by an assignment of values to some or all of the variables, {Xi = vi,Xj =

ASSIGNMENT vj , . . .}. An assignment that does not violate any constraints is called a consistent or legalCONSISTENT assignment. A complete assignment is one in which every variable is mentioned, and a so-

lution to a CSP is a complete assignment that satisfies all the constraints. Some CSPs alsorequire a solution that maximizes an objective function.OBJECTIVE

FUNCTION

137

Page 2: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

138 Chapter 5. Constraint Satisfaction Problems

So what does all this mean? Suppose that, having tired of Romania, we are lookingat a map of Australia showing each of its states and territories, as in Figure 5.1(a), and thatwe are given the task of coloring each region either red, green, or blue in such a way that noneighboring regions have the same color. To formulate this as a CSP, we define the variablesto be the regions: WA, NT , Q, NSW , V , SA, and T . The domain of each variable is the set{red , green, blue}. The constraints require neighboring regions to have distinct colors; forexample, the allowable combinations for WA and NT are the pairs

{(red , green), (red , blue), (green, red), (green, blue), (blue, red), (blue, green)} .

(The constraint can also be represented more succinctly as the inequality WA 6= NT , pro-vided the constraint satisfaction algorithm has some way to evaluate such expressions.) Thereare many possible solutions, such as

{WA= red ,NT = green, Q= red ,NSW = green, V = red ,SA= blue, T = red }.

It is helpful to visualize a CSP as a constraint graph, as shown in Figure 5.1(b). The nodesCONSTRAINT GRAPH

of the graph correspond to variables of the problem and the arcs correspond to constraints.Treating a problem as a CSP confers several important benefits. Because the representa-

tion of states in a CSP conforms to a standard pattern—that is, a set of variables with assignedvalues—the successor function and goal test can written in a generic way that applies to allCSPs. Furthermore, we can develop effective, generic heuristics that require no additional,domain-specific expertise. Finally, the structure of the constraint graph can be used to sim-plify the solution process, in some cases giving an exponential reduction in complexity. TheCSP representation is the first, and simplest, in a series of representation schemes that will bedeveloped throughout the book.

WesternAustralia

NorthernTerritory

SouthAustralia

Queensland

New South Wales

Victoria

Tasmania

WA

NT

SA

Q

NSW

V

T

(a) (b)

Figure 5.1 (a) The principal states and territories of Australia. Coloring this map can beviewed as a constraint satisfaction problem. The goal is to assign colors to each region sothat no neighboring regions have the same color. (b) The map-coloring problem representedas a constraint graph.

Page 3: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.1. Constraint Satisfaction Problems 139

It is fairly easy to see that a CSP can be given an incremental formulation as a standardsearch problem as follows:

♦ Initial state: the empty assignment {}, in which all variables are unassigned.

♦ Successor function: a value can be assigned to any unassigned variable, provided thatit does not conflict with previously assigned variables.

♦ Goal test: the current assignment is complete.

♦ Path cost: a constant cost (e.g., 1) for every step.

Every solution must be a complete assignment and therefore appears at depth n if there aren variables. Furthermore, the search tree extends only to depth n. For these reasons, depth-first search algorithms are popular for CSPs. (See Section 5.2.) It is also the case that thepath by which a solution is reached is irrelevant. Hence, we can also use a complete-stateformulation, in which every state is a complete assignment that might or might not satisfythe constraints. Local search methods work well for this formulation. (See Section 5.3.)

The simplest kind of CSP involves variables that are discrete and have finite domains.FINITE DOMAINS

Map-coloring problems are of this kind. The 8-queens problem described in Chapter 3 canalso be viewed as a finite-domain CSP, where the variables Q1, . . . , Q8 are the positions ofeach queen in columns 1, . . . , 8 and each variable has the domain {1, 2, 3, 4, 5, 6, 7, 8}. If themaximum domain size of any variable in a CSP is d, then the number of possible completeassignments is O(dn)—that is, exponential in the number of variables. Finite-domain CSPsinclude Boolean CSPs, whose variables can be either true or false . Boolean CSPs includeBOOLEAN CSPS

as special cases some NP-complete problems, such as 3SAT. (See Chapter 7.) In the worstcase, therefore, we cannot expect to solve finite-domain CSPs in less than exponential time.In most practical applications, however, general-purpose CSP algorithms can solve problemsorders of magnitude larger than those solvable via the general-purpose search algorithms thatwe saw in Chapter 3.

Discrete variables can also have infinite domains—for example, the set of integers orINFINITE DOMAINS

the set of strings. For example, when scheduling construction jobs onto a calendar, each job’sstart date is a variable and the possible values are integer numbers of days from the currentdate. With infinite domains, it is no longer possible to describe constraints by enumeratingall allowed combinations of values. Instead, a constraint language must be used. For ex-CONSTRAINT

LANGUAGE

ample, if Job1, which takes five days, must precede Job3, then we would need a constraintlanguage of algebraic inequalities such as StartJob1 + 5 ≤ StartJob3. It is also no longerpossible to solve such constraints by enumerating all possible assignments, because there areinfinitely many of them. Special solution algorithms (which we will not discuss here) existfor linear constraints on integer variables—that is, constraints, such as the one just given,LINEAR

CONSTRAINTS

in which each variable appears only in linear form. It can be shown that no algorithm existsfor solving general nonlinear constraints on integer variables. In some cases, we can reduceNONLINEAR

CONSTRAINTS

integer constraint problems to finite-domain problems simply by bounding the values of allthe variables. For example, in a scheduling problem, we can set an upper bound equal to thetotal length of all the jobs to be scheduled.

Constraint satisfaction problems with continuous domains are very common in the realCONTINUOUSDOMAINS

world and are widely studied in the field of operations research. For example, the scheduling

Page 4: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

140 Chapter 5. Constraint Satisfaction Problems

of experiments on the Hubble Space Telescope requires very precise timing of observations;the start and finish of each observation and maneuver are continuous-valued variables thatmust obey a variety of astronomical, precedence, and power constraints. The best-knowncategory of continuous-domain CSPs is that of linear programming problems, where con-LINEAR

PROGRAMMING

straints must be linear inequalities forming a convex region. Linear programming problemscan be solved in time polynomial in the number of variables. Problems with different types ofconstraints and objective functions have also been studied—quadratic programming, second-order conic programming, and so on.

In addition to examining the types of variables that can appear in CSPs, it is useful tolook at the types of constraints. The simplest type is the unary constraint, which restricts theUNARY CONSTRAINT

value of a single variable. For example, it could be the case that South Australians activelydislike the color green. Every unary constraint can be eliminated simply by preprocessingthe domain of the corresponding variable to remove any value that violates the constraint. Abinary constraint relates two variables. For example, SA 6= NSW is a binary constraint. ABINARY CONSTRAINT

binary CSP is one with only binary constraints; it can be represented as a constraint graph, asin Figure 5.1(b).

Higher-order constraints involve three or more variables. A familiar example is pro-vided by cryptarithmetic puzzles. (See Figure 5.2(a).) It is usual to insist that each letter inCRYPTARITHMETIC

a cryptarithmetic puzzle represent a different digit. For the case in Figure 5.2(a)), this wouldbe represented as the six-variable constraint Alldiff (F, T, U,W,R,O). Alternatively, it canbe represented by a collection of binary constraints such as F 6= T . The addition constraintson the four columns of the puzzle also involve several variables and can be written as

O + O = R + 10 ·X1

X1 + W + W = U + 10 ·X2

X2 + T + T = O + 10 ·X3

X3 = F

where X1, X2, and X3 are auxiliary variables representing the digit (0 or 1) carried over intoAUXILIARYVARIABLES

the next column. Higher-order constraints can be represented in a constraint hypergraph,CONSTRAINTHYPERGRAPH

such as the one shown in Figure 5.2(b). The sharp-eyed reader will have noticed that theAlldiff constraint can be broken down into binary constraints—F 6= T , F 6= U , and so on.In fact, as Exercise 5.11 asks you to prove, every higher-order, finite-domain constraint canbe reduced to a set of binary constraints if enough auxiliary variables are introduced. Becauseof this, we will deal only with binary constraints in this chapter.

The constraints we have described so far have all been absolute constraints, violationof which rules out a potential solution. Many real-world CSPs include preference constraintsPREFERENCE

indicating which solutions are preferred. For example, in a university timetabling problem,Prof. X might prefer teaching in the morning whereas Prof. Y prefers teaching in the after-noon. A timetable that has Prof. X teaching at 2 p.m. would still be a solution (unless Prof. Xhappens to be the department chair), but would not be an optimal one. Preference constraintscan often be encoded as costs on individual variable assignments—for example, assigningan afternoon slot for Prof. X costs 2 points against the overall objective function, whereas amorning slot costs 1. With this formulation, CSPs with preferences can be solved using opti-

Page 5: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.2. Backtracking Search for CSPs 141

(a)

OWTF U R

(b)

+

F

T

T

O

W

W

U

O

O

R

X3 X1X2

Figure 5.2 (a) A cryptarithmetic problem. Each letter stands for a distinct digit; the aim isto find a substitution of digits for letters such that the resulting sum is arithmetically correct,with the added restriction that no leading zeroes are allowed. (b) The constraint hypergraphfor the cryptarithmetic problem, showing the Alldiff constraint as well as the column additionconstraints. Each constraint is a square box connected to the variables it constrains.

mization search methods, either path-based or local. We do not discuss such CSPs further inthis chapter, but we provide some pointers in the bibliographical notes section.

5.2 BACKTRACKING SEARCH FOR CSPS

The preceding section gave a formulation of CSPs as search problems. Using this formula-tion, any of the search algorithms from Chapters 3 and 4 can solve CSPs. Suppose we applybreadth-first search to the generic CSP problem formulation given in the preceding section.We quickly notice something terrible: the branching factor at the top level is nd, because anyof d values can be assigned to any of n variables. At the next level, the branching factor is(n− 1)d, and so on for n levels. We generate a tree with n! · dn leaves, even though there areonly dn possible complete assignments!

Our seemingly reasonable but naı̈ve problem formulation has ignored a crucial propertycommon to all CSPs: commutativity. A problem is commutative if the order of applicationCOMMUTATIVITY

of any given set of actions has no effect on the outcome. This is the case for CSPs be-cause, when assigning values to variables, we reach the same partial assignment, regardlessof order. Therefore, all CSP search algorithms generate successors by considering possibleassignments for only a single variable at each node in the search tree. For example, at theroot node of a search tree for coloring the map of Australia, we might have a choice betweenSA= red , SA= green, and SA= blue, but we would never choose between SA= red andWA= blue. With this restriction, the number of leaves is dn, as we would hope.

The term backtracking search is used for a depth-first search that chooses values forBACKTRACKINGSEARCH

one variable at a time and backtracks when a variable has no legal values left to assign. Thealgorithm is shown in Figure 5.3. Notice that it uses, in effect, the one-at-a-time method of

Page 6: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

142 Chapter 5. Constraint Satisfaction Problems

function BACKTRACKING-SEARCH(csp) returns a solution, or failurereturn RECURSIVE-BACKTRACKING({ }, csp)

function RECURSIVE-BACKTRACKING(assignment , csp) returns a solution, or failureif assignment is complete then return assignment

var← SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment , csp)for each value in ORDER-DOMAIN-VALUES(var ,assignment , csp) do

if value is consistent with assignment according to CONSTRAINTS[csp] thenadd {var = value} to assignment

result←RECURSIVE-BACKTRACKING(assignment , csp)if result 6= failure then return result

remove {var = value} from assignment

return failure

Figure 5.3 A simple backtracking algorithm for constraint satisfaction problems. Thealgorithm is modeled on the recursive depth-first search of Chapter 3. The functionsSELECT-UNASSIGNED-VARIABLE and ORDER-DOMAIN-VALUES can be used to imple-ment the general-purpose heuristics discussed in the text.

WA=red WA=blueWA=green

WA=redNT=blue

WA=redNT=green

WA=redNT=greenQ=red

WA=redNT=greenQ=blue

Figure 5.4 Part of the search tree generated by simple backtracking for the map-coloringproblem in Figure 5.1.

incremental successor generation described on page 76. Also, it extends the current assign-ment to generate a successor, rather than copying it. Because the representation of CSPs isstandardized, there is no need to supply BACKTRACKING-SEARCH with a domain-specificinitial state, successor function, or goal test. Part of the search tree for the Australia problemis shown in Figure 5.4, where we have assigned variables in the order WA,NT , Q, . . ..

Plain backtracking is an uninformed algorithm in the terminology of Chapter 3, so wedo not expect it to be very effective for large problems. The results for some sample problemsare shown in the first column of Figure 5.5 and confirm our expectations.

In Chapter 4 we remedied the poor performance of uninformed search algorithms bysupplying them with domain-specific heuristic functions derived from our knowledge of theproblem. It turns out that we can solve CSPs efficiently without such domain-specific knowl-

Page 7: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.2. Backtracking Search for CSPs 143

Problem Backtracking BT+MRV Forward Checking FC+MRV Min-Conflicts

USA (> 1,000K) (> 1,000K) 2K 60 64n-Queens (> 40,000K) 13,500K (> 40,000K) 817K 4KZebra 3,859K 1K 35K 0.5K 2KRandom 1 415K 3K 26K 2KRandom 2 942K 27K 77K 15K

Figure 5.5 Comparison of various CSP algorithms on various problems. The algorithmsfrom left to right, are simple backtracking, backtracking with the MRV heuristic, forwardchecking, forward checking with MRV, and minimum conflicts local search. Listed in eachcell is the median number of consistency checks (over five runs) required to solve the prob-lem; note that all entries except the two in the upper right are in thousands (K). Numbers inparentheses mean that no answer was found in the allotted number of checks. The first prob-lem is finding a 4-coloring for the 50 states of the United States of America. The remainingproblems are taken from Bacchus and van Run (1995), Table 1. The second problem countsthe total number of checks required to solve all n-Queens problems for n from 2 to 50. Thethird is the “Zebra Puzzle,” as described in Exercise 5.13. The last two are artificial randomproblems. (Min-conflicts was not run on these.) The results suggest that forward checkingwith the MRV heuristic is better on all these problems than the other backtracking algorithms,but not always better than min-conflicts local search.

edge. Instead, we find general-purpose methods that address the following questions:

1. Which variable should be assigned next, and in what order should its values be tried?

2. What are the implications of the current variable assignments for the other unassignedvariables?

3. When a path fails—that is, a state is reached in which a variable has no legal values—can the search avoid repeating this failure in subsequent paths?

The subsections that follow answer each of these questions in turn.

Variable and value ordering

The backtracking algorithm contains the line

var← SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment , csp).

By default, SELECT-UNASSIGNED-VARIABLE simply selects the next unassigned variablein the order given by the list VARIABLES[csp]. This static variable ordering seldom results inthe most efficient search. For example, after the assignments for WA= red and NT = green,there is only one possible value for SA, so it makes sense to assign SA= blue next rather thanassigning Q. In fact, after SA is assigned, the choices for Q, NSW , and V are all forced. Thisintuitive idea—choosing the variable with the fewest “legal” values—is called the minimumremaining values (MRV) heuristic. It also has been called the “most constrained variable” orMINIMUM REMAINING

VALUES

“fail-first” heuristic, the latter because it picks a variable that is most likely to cause a failuresoon, thereby pruning the search tree. If there is a variable X with zero legal values remain-ing, the MRV heuristic will select X and failure will be detected immediately—avoidingpointless searches through other variables which always will fail when X is finally selected.

Page 8: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

144 Chapter 5. Constraint Satisfaction Problems

The second column of Figure 5.5, labeled BT+MRV, shows the performance of this heuristic.The performance is 3 to 3,000 times better than simple backtracking, depending on the prob-lem. Note that our performance measure ignores the extra cost of computing the heuristicvalues; the next subsection describes a method that makes this cost manageable.

The MRV heuristic doesn’t help at all in choosing the first region to color in Australia,because initially every region has three legal colors. In this case, the degree heuristic comesDEGREE HEURISTIC

in handy. It attempts to reduce the branching factor on future choices by selecting the vari-able that is involved in the largest number of constraints on other unassigned variables. InFigure 5.1, SA is the variable with highest degree, 5; the other variables have degree 2 or 3,except for T , which has 0. In fact, once SA is chosen, applying the degree heuristic solves theproblem without any false steps—you can choose any consistent color at each choice pointand still arrive at a solution with no backtracking. The minimum remaining values heuristicis usually a more powerful guide, but the degree heuristic can be useful as a tie-breaker.

Once a variable has been selected, the algorithm must decide on the order in which toexamine its values. For this, the least-constraining-value heuristic can be effective in some

LEAST-CONSTRAINING-VALUE

cases. It prefers the value that rules out the fewest choices for the neighboring variables inthe constraint graph. For example, suppose that in Figure 5.1 we have generated the partialassignment with WA= red and NT = green, and that our next choice is for Q. Blue wouldbe a bad choice, because it eliminates the last legal value left for Q’s neighbor, SA. Theleast-constraining-value heuristic therefore prefers red to blue. In general, the heuristic istrying to leave the maximum flexibility for subsequent variable assignments. Of course, if weare trying to find all the solutions to a problem, not just the first one, then the ordering doesnot matter because we have to consider every value anyway. The same holds if there are nosolutions to the problem.

Propagating information through constraints

So far our search algorithm considers the constraints on a variable only at the time that thevariable is chosen by SELECT-UNASSIGNED-VARIABLE. But by looking at some of theconstraints earlier in the search, or even before the search has started, we can drasticallyreduce the search space.

Forward checking

One way to make better use of constraints during search is called forward checking. When-FORWARDCHECKING

ever a variable X is assigned, the forward checking process looks at each unassigned variableY that is connected to X by a constraint and deletes from Y ’s domain any value that is in-consistent with the value chosen for X . Figure 5.6 shows the progress of a map-coloringsearch with forward checking. There are two important points to notice about this exam-ple. First, notice that after assigning WA= red and Q= green, the domains of NT and SA

are reduced to a single value; we have eliminated branching on these variables altogether bypropagating information from WA and Q. The MRV heuristic, which is an obvious part-ner for forward checking, would automatically select SA and NT next. (Indeed, we canview forward checking as an efficient way to incrementally compute the information that the

Page 9: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.2. Backtracking Search for CSPs 145

Initial domainsAfter WA=redAfter Q=greenAfter V=blue

R G B

R

R B

R G B

R G B

B

R G B

R G B

R G B

R

R

R

R G B

B

B

G B

R G B

G

G

R G B

R G B

B

G B

R G B

R G B

R G B

R G B

WA TSAVNSWQNT

Figure 5.6 The progress of a map-coloring search with forward checking. WA= red

is assigned first; then forward checking deletes red from the domains of the neighboringvariables NT and SA. After Q = green , green is deleted from the domains of NT , SA, andNSW . After V = blue , blue is deleted from the domains of NSW and SA, leaving SA withno legal values.

MRV heuristic needs to do its job.) A second point to notice is that, after V = blue, thedomain of SA is empty. Hence, forward checking has detected that the partial assignment{WA= red , Q= green, V = blue} is inconsistent with the constraints of the problem, andthe algorithm will therefore backtrack immediately.

Constraint propagation

Although forward checking detects many inconsistencies, it does not detect all of them. Forexample, consider the third row of Figure 5.6. It shows that when WA is red and Q is green,both NT and SA are forced to be blue. But they are adjacent and so cannot have the samevalue. Forward checking does not detect this as an inconsistency, because it does not look farenough ahead. Constraint propagation is the general term for propagating the implicationsCONSTRAINT

PROPAGATION

of a constraint on one variable onto other variables; in this case we need to propagate fromWA and Q onto NT and SA, (as was done by forward checking) and then onto the constraintbetween NT and SA to detect the inconsistency. And we want to do this fast: it is no goodreducing the amount of search if we spend more time propagating constraints than we wouldhave spent doing a simple search.

The idea of arc consistency provides a fast method of constraint propagation that isARC CONSISTENCY

substantially stronger than forward checking. Here, “arc” refers to a directed arc in the con-straint graph, such as the arc from SA to NSW . Given the current domains of SA and NSW ,the arc is consistent if, for every value x of SA, there is some value y of NSW that is consis-tent with x. In the third row of Figure 5.6, the current domains of SA and NSW are {blue}and {red , blue} respectively. For SA= blue, there is a consistent assignment for NSW ,namely, NSW = red ; therefore, the arc from SA to NSW is consistent. On the other hand,the reverse arc from NSW to SA is not consistent: for the assignment NSW = blue, there isno consistent assignment for SA. The arc can be made consistent by deleting the value blue

from the domain of NSW .We can also apply arc consistency to the arc from SA to NT at the same stage in the

search process. The third row of the table in Figure 5.6 shows that both variables have thedomain {blue}. The result is that blue must be deleted from the domain of SA, leaving thedomain empty. Thus, applying arc consistency has resulted in early detection of an inconsis-

Page 10: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

146 Chapter 5. Constraint Satisfaction Problems

function AC-3(csp) returns the CSP, possibly with reduced domainsinputs: csp, a binary CSP with variables {X1, X2, . . . , Xn}local variables: queue , a queue of arcs, initially all the arcs in csp

while queue is not empty do(Xi, Xj)←REMOVE-FIRST(queue)if REMOVE-INCONSISTENT-VALUES(Xi, Xj) then

for each Xk in NEIGHBORS[Xi] doadd (Xk, Xi) to queue

function REMOVE-INCONSISTENT-VALUES(Xi, Xj) returns true iff we remove a valueremoved← false

for each x in DOMAIN[Xi] doif no value y in DOMAIN[Xj] allows (x ,y) to satisfy the constraint between Xi and Xj

then delete x from DOMAIN[Xi]; removed← true

return removed

Figure 5.7 The arc consistency algorithm AC-3. After applying AC-3, either every arcis arc-consistent, or some variable has an empty domain, indicating that the CSP cannot bemade arc-consistent (and thus the CSP cannot be solved). The name “AC-3” was used by thealgorithm’s inventor (Mackworth, 1977) because it’s the third version developed in the paper.

tency that is not detected by pure forward checking.Arc consistency checking can be applied either as a preprocessing step before the be-

ginning of the search process, or as a propagation step (like forward checking) after everyassignment during search. (The latter algorithm is sometimes called MAC, for MaintainingArc Consistency.) In either case, the process must be applied repeatedly until no more incon-sistencies remain. This is because, whenever a value is deleted from some variable’s domainto remove an arc inconsistency, a new arc inconsistency could arise in arcs pointing to thatvariable. The full algorithm for arc consistency, AC-3, uses a queue to keep track of the arcsthat need to be checked for inconsistency. (See Figure 5.7.) Each arc (Xi,Xj) in turn isremoved from the agenda and checked; if any values need to be deleted from the domain ofXi, then every arc (Xk,Xi) pointing to Xi must be reinserted on the queue for checking. Thecomplexity of arc consistency checking can be analyzed as follows: a binary CSP has at mostO(n2) arcs; each arc (Xk,Xi) can be inserted on the agenda only d times, because Xi hasat most d values to delete; checking consistency of an arc can be done in O(d2) time; so thetotal worst-case time is O(n2d3). Although this is substantially more expensive than forwardchecking, the extra cost is usually worthwhile.1

Because CSPs include 3SAT as a special case, we do not expect to find a polynomial-time algorithm that can decide whether a given CSP is consistent. Hence, we deduce that arcconsistency does not reveal every possible inconsistency. For example, in Figure 5.1, the par-tial assignment {WA= red ,NSW = red} is inconsistent, but AC-3 will not find the incon-

1 The AC-4 algorithm, due to Mohr and Henderson (1986), runs in O(n2d2). See Exercise 5.10.

Page 11: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.2. Backtracking Search for CSPs 147

sistency. Stronger forms of propagation can be defined using the notion called k-consistency.K-CONSISTENCY

A CSP is k-consistent if, for any set of k − 1 variables and for any consistent assignment tothose variables, a consistent value can always be assigned to any kth variable. For example,1-consistency means that each individual variable by itself is consistent; this is also callednode consistency. 2-consistency is the same as arc consistency. 3-consistency means thatNODE CONSISTENCY

any pair of adjacent variables can always be extended to a third neighboring variable; this isalso called path consistency.PATH CONSISTENCY

A graph is strongly k-consistent if it is k-consistent and is also (k − 1)-consistent,STRONGLYK-CONSISTENT

(k−2)-consistent, . . . all the way down to 1-consistent. Now suppose we have a CSP problemwith n nodes and make it strongly n-consistent (i.e., strongly k-consistent for k = n). We canthen solve the problem with no backtracking. First, we choose a consistent value for X1. Weare then guaranteed to be able to choose a value for X2 because the graph is 2-consistent, forX3 because it is 3-consistent, and so on. For each variable Xi, we need only search throughthe d values in the domain to find a value consistent with X1, . . . ,Xi−1. We are guaranteed tofind a solution in time O(nd). Of course, there is no free lunch: any algorithm for establishingn-consistency must take time exponential in n in the worst case.

There is a broad middle ground between n-consistency and arc consistency: runningstronger consistency checks will take more time, but will have a greater effect in reducingthe branching factor and detecting inconsistent partial assignments. It is possible to calculatethe smallest value k such that running k-consistency ensures that the problem can be solvedwithout backtracking (see Section 5.4), but this is often impractical. In practice, determiningthe appropriate level of consistency checking is mostly an empirical science.

Handling special constraints

Certain types of constraints occur frequently in real problems and can be handled usingspecial-purpose algorithms that are more efficient than the general-purpose methods de-scribed so far. For example, the Alldiff constraint says that all the variables involved musthave distinct values (as in the cryptarithmetic problem). One simple form of inconsistencydetection for Alldiff constraints works as follows: if there are m variables involved in theconstraint, and if they have n possible distinct values altogether, and m > n, then the con-straint cannot be satisfied.

This leads to the following simple algorithm: First, remove any variable in the con-straint that has a singleton domain, and delete that variable’s value from the domains of theremaining variables. Repeat as long as there are singleton variables. If at any point an emptydomain is produced or there are more variables than domain values left, then an inconsistencyhas been detected.

We can use this method to detect the inconsistency in the partial assignment {WA= red ,NSW = red} for Figure 5.1. Notice that the variables SA, NT , and Q are effectively con-nected by an Alldiff constraint because each pair must be a different color. After applyingAC-3 with the partial assignment, the domain of each variable is reduced to {green, blue}.That is, we have three variables and only two colors, so the Alldiff constraint is violated.Thus, a simple consistency procedure for a higher-order constraint is sometimes more effec-

Page 12: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

148 Chapter 5. Constraint Satisfaction Problems

tive than applying arc consistency to an equivalent set of binary constraints.Perhaps the most important higher-order constraint is the resource constraint, some-RESOURCE

CONSTRAINT

times called the atmost constraint. For example, let PA1, . . . ,PA4 denote the numbers ofpersonnel assigned to each of four tasks. The constraint that no more than 10 personnel areassigned in total is written as atmost(10,PA1,PA2,PA3,PA4). An inconsistency can bedetected simply by checking the sum of the minimum values of the current domains; forexample, if each variable has the domain {3, 4, 5, 6}, the atmost constraint cannot be satis-fied. We can also enforce consistency by deleting the maximum value of any domain if it isnot consistent with the minimum values of the other domains. Thus, if each variable in ourexample has the domain {2, 3, 4, 5, 6}, the values 5 and 6 can be deleted from each domain.

For large resource-limited problems with integer values—such as logistical problemsinvolving moving thousands of people in hundreds of vehicles—it is usually not possible torepresent the domain of each variable as a large set of integers and gradually reduce thatset by consistency checking methods. Instead, domains are represented by upper and lowerbounds and are managed by bounds propagation. For example, let’s suppose there are twoflights, 271 and 272, for which the planes have capacities 165 and 385, respectively. Theinitial domains for the numbers of passengers on each flight are then

Flight271 ∈ [0, 165] and Flight272 ∈ [0, 385] .

Now suppose we have the additional constraint that the two flights together must carry 420people: Flight271 + Flight272 ∈ [420, 420]. Propagating bounds constraints, we reduce thedomains to

Flight271 ∈ [35, 165] and Flight272 ∈ [255, 385] .

We say that a CSP is bounds-consistent if for every variable X , and for both the lower boundand upper bound values of X , there exists some value of Y that satisfies the constraint be-tween X and Y , for every variable Y . This kind of bounds propagation is widely used inBOUNDS

PROPAGATION

practical constraint problems.

Intelligent backtracking: looking backward

The BACKTRACKING-SEARCH algorithm in Figure 5.3 has a very simple policy for what todo when a branch of the search fails: back up to the preceding variable and try a differentvalue for it. This is called chronological backtracking, because the most recent decisionCHRONOLOGICAL

BACKTRACKING

point is revisited. In this subsection, we will see that there are much better ways.Consider what happens when we apply simple backtracking in Figure 5.1 with a fixed

variable ordering Q, NSW , V , T , SA, WA, NT . Suppose we have generated the partialassignment {Q= red ,NSW = green, V = blue, T = red}. When we try the next variable,SA, we see that every value violates a constraint. We back up to T and try a new colorfor Tasmania! Obviously this is silly—recoloring Tasmania cannot resolve the problem withSouth Australia.

A more intelligent approach to backtracking is to go all the way back to one of theset of variables that caused the failure. This set is called the conflict set; here, the conflictCONFLICT SET

set for SA is {Q,NSW , V }. In general, the conflict set for variable X is the set of previ-ously assigned variables that are connected to X by constraints. The backjumping methodBACKJUMPING

Page 13: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.2. Backtracking Search for CSPs 149

backtracks to the most recent variable in the conflict set; in this case, backjumping wouldjump over Tasmania and try a new value for V . This is easily implemented by modifyingBACKTRACKING-SEARCH so that it accumulates the conflict set while checking for a legalvalue to assign. If no legal value is found, it should return the most recent element of theconflict set along with the failure indicator.

The sharp-eyed reader will have noticed that forward checking can supply the conflictset with no extra work: whenever forward checking based on an assignment to X deletes avalue from Y ’s domain, it should add X to Y ’s conflict set. Also, every time the last value isdeleted from Y ’s domain, the variables in the conflict set of Y are added to the conflict set ofX . Then, when we get to Y , we know immediately where to backtrack if needed.

The eagle-eyed reader will have noticed something odd: backjumping occurs whenevery value in a domain is in conflict with the current assignment; but forward checkingdetects this event and prevents the search from ever reaching such a node! In fact, it can beshown that every branch pruned by backjumping is also pruned by forward checking. Hence,simple backjumping is redundant in a forward-checking search or, indeed, in a search thatuses stronger consistency checking, such as MAC.

Despite the observations of the preceding paragraph, the idea behind backjumping re-mains a good one: to backtrack based on the reasons for failure. Backjumping notices failurewhen a variable’s domain becomes empty, but in many cases a branch is doomed long beforethis occurs. Consider again the partial assignment {WA= red ,NSW = red} (which, fromour earlier discussion, is inconsistent). Suppose we try T = red next and then assign NT , Q,V , SA. We know that no assignment can work for these last four variables, so eventually werun out of values to try at NT . Now, the question is, where to backtrack? Backjumping cannotwork, because NT does have values consistent with the preceding assigned variables—NT

doesn’t have a complete conflict set of preceding variables that caused it to fail. We know,however, that the four variables NT , Q, V , and SA, taken together, failed because of a set ofpreceding variables, which must be those variables which directly conflict with the four. Thisleads to a deeper notion of the conflict set for a variable such as NT : it is that set of preced-ing variables that caused NT , together with any subsequent variables, to have no consistentsolution. In this case, the set is WA and NSW , so the algorithm should backtrack to NSW

and skip over Tasmania. A backjumping algorithm that uses conflict sets defined in this wayis called conflict-directed backjumping.CONFLICT-DIRECTED

BACKJUMPING

We must now explain how these new conflict sets are computed. The method is infact very simple. The “terminal” failure of a branch of the search always occurs because avariable’s domain becomes empty; that variable has a standard conflict set. In our example,SA fails, and its conflict set is (say) {WA,NT , Q}. We backjump to Q, and Q absorbsthe conflict set from SA (minus Q itself, of course) into its own direct conflict set, which is{NT ,NSW }; the new conflict set is {WA,NT ,NSW }. That is, there is no solution fromQ onwards, given the preceding assignment to {WA,NT ,NSW }. Therefore, we backtrackto NT , the most recent of these. NT absorbs {WA,NT ,NSW } − {NT} into its owndirect conflict set {WA}, giving {WA,NSW } (as stated in the previous paragraph). Nowthe algorithm backjumps to NSW , as we would hope. To summarize: let Xj be the currentvariable, and let conf (Xj) be its conflict set. If every possible value for Xj fails, backjump

Page 14: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

150 Chapter 5. Constraint Satisfaction Problems

to the most recent variable Xi in conf (Xj), and set

conf (Xi)← conf (Xi) ∪ conf (Xj)− {Xi} .

Conflict-directed backjumping takes us back to the right point in the search tree, but doesn’tprevent us from making the same mistakes in another branch of the tree. Constraint learningCONSTRAINT

LEARNING

actually modifies the CSP by adding a new constraint that is induced from these conflicts.

5.3 LOCAL SEARCH FOR CONSTRAINT SATISFACTION PROBLEMS

Local-search algorithms (see Section 4.3) turn out to be very effective in solving many CSPs.They use a complete-state formulation: the initial state assigns a value to every variable,and the successor function usually works by changing the value of one variable at a time.For example, in the 8-queens problem, the initial state might be a random configuration of8 queens in 8 columns, and the successor function picks one queen and considers moving itelsewhere in its column. Another possibility would be start with the 8 queens, one per columnin a permutation of the 8 rows, and to generate a successor by having two queens swap rows.2

We have actually already seen an example of local search for CSP solving: the application ofhill climbing to the n-queens problem (page 112). The application of WALKSAT (page 223)to solve satisfiability problems, which are a special case of CSPs, is another.

In choosing a new value for a variable, the most obvious heuristic is to select the valuethat results in the minimum number of conflicts with other variables—the min-conflictsMIN-CONFLICTS

heuristic. The algorithm is shown in Figure 5.8 and its application to an 8-queens problem isdiagrammed in Figure 5.9 and quantified in Figure 5.5.

Min-conflicts is surprisingly effective for many CSPs, particularly when given a reason-able initial state. Its performance is shown in the last column of Figure 5.5. Amazingly, onthe n-queens problem, if you don’t count the initial placement of queens, the runtime of min-conflicts is roughly independent of problem size. It solves even the million-queens problemin an average of 50 steps (after the initial assignment). This remarkable observation was thestimulus leading to a great deal of research in the 1990s on local search and the distinction be-tween easy and hard problems, which we take up in Chapter 7. Roughly speaking, n-queensis easy for local search because solutions are densely distributed throughout the state space.Min-conflicts also works well for hard problems. For example, it has been used to scheduleobservations for the Hubble Space Telescope, reducing the time taken to schedule a week ofobservations from three weeks (!) to around 10 minutes.

Another advantage of local search is that it can be used in an online setting when theproblem changes. This is particularly important in scheduling problems. A week’s airlineschedule may involve thousands of flights and tens of thousands of personnel assignments,but bad weather at one airport can render the schedule infeasible. We would like to repair theschedule with a minimum number of changes. This can be easily done with a local searchalgorithm starting from the current schedule. A backtracking search with the new set of

2 Local search can easily be extended to CSPs with objective functions. In that case, all the techniques for hillclimbing and simulated annealing can be applied to optimize the objective function.

Page 15: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.4. The Structure of Problems 151

function MIN-CONFLICTS(csp,max steps) returns a solution or failureinputs: csp, a constraint satisfaction problem

max steps , the number of steps allowed before giving up

current← an initial complete assignment for csp

for i = 1 to max steps doif current is a solution for csp then return current

var← a randomly chosen, conflicted variable from VARIABLES[csp]value← the value v for var that minimizes CONFLICTS(var , v , current , csp)set var = value in current

return failure

Figure 5.8 The MIN-CONFLICTS algorithm for solving CSPs by local search. The initialstate may be chosen randomly or by a greedy assignment process that chooses a minimal-conflict value for each variable in turn. The CONFLICTS function counts the number ofconstraints violated by a particular value, given the rest of the current assignment.

2

2

1

2

3

1

2

3

3

2

3

2

3

0

Figure 5.9 A two-step solution for an 8-queens problem using min-conflicts. At eachstage, a queen is chosen for reassignment in its column. The number of conflicts (in thiscase, the number of attacking queens) is shown in each square. The algorithm moves thequeen to the min-conflict square, breaking ties randomly.

constraints usually requires much more time and might find a solution with many changesfrom the current schedule.

5.4 THE STRUCTURE OF PROBLEMS

In this section, we examine ways in which the structure of the problem, as represented by theconstraint graph, can be used to find solutions quickly. Most of the approaches here are verygeneral and are applicable to other problems besides CSPs, for example probabilistic reason-ing. After all, the only way we can possibly hope to deal with the real world is to decomposeit into many subproblems. Looking again at Figure 5.1(b) with a view to identifying problem

Page 16: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

152 Chapter 5. Constraint Satisfaction Problems

structure, one fact stands out: Tasmania is not connected to the mainland.3 Intuitively, it is ob-vious that coloring Tasmania and coloring the mainland are independent subproblems—anyINDEPENDENT

SUBPROBLEMS

solution for the mainland combined with any solution for Tasmania yields a solution for thewhole map. Independence can be ascertained simply by looking for connected componentsCONNECTED

COMPONENTS

of the constraint graph. Each component corresponds to a subproblem CSP i. If assignmentSi is a solution of CSP i, then

⋃i Si is a solution of

⋃i CSP i. Why is this important? Con-

sider the following: suppose each CSP i has c variables from the total of n variables, wherec is a constant. Then there are n/c subproblems, each of which takes at most dc work tosolve. Hence, the total work is O(dcn/c), which is linear in n; without the decomposition,the total work is O(dn), which is exponential in n. Let’s make this more concrete: dividing aBoolean CSP with n= 80 into four subproblems with c= 20 reduces the worst-case solutiontime from the lifetime of the universe down to less than a second.

Completely independent subproblems are delicious, then, but rare. In most cases, thesubproblems of a CSP are connected. The simplest case is when the constraint graph forms atree: any two variables are connected by at most one path. Figure 5.10(a) shows a schematicexample.4 We will show that any tree-structured CSP can be solved in time linear in thenumber of variables. The algorithm has the following steps:

1. Choose any variable as the root of the tree, and order the variables from the root to theleaves in such a way that every node’s parent in the tree precedes it in the ordering. (SeeFigure 5.10(b).) Label the variables X1, . . . ,Xn in order. Now, every variable exceptthe root has exactly one parent variable.

2. For j from n down to 2, apply arc consistency to the arc (Xi,Xj), where Xi is theparent of Xj , removing values from DOMAIN[Xi] as necessary.

3. For j from 1 to n, assign any value for Xj consistent with the value assigned for Xi,where Xi is the parent of Xj .

There are two key points to note. First, after step 2 the CSP is directionally arc-consistent,so the assignment of values in step 3 requires no backtracking. (See the discussion of k-consistency on page 147.) Second, by applying the arc-consistency checks in reverse order instep 2, the algorithm ensures that any deleted values cannot endanger the consistency of arcsthat have been processed already. The complete algorithm runs in time O(nd2).

Now that we have an efficient algorithm for trees, we can consider whether more generalconstraint graphs can be reduced to trees somehow. There are two primary ways to do this,one based on removing nodes and one based on collapsing nodes together.

The first approach involves assigning values to some variables so that the remainingvariables form a tree. Consider the constraint graph for Australia, shown again in Fig-ure 5.11(a). If we could delete South Australia, the graph would become a tree, as in (b).Fortunately, we can do this (in the graph, not the continent) by fixing a value for SA anddeleting from the domains of the other variables any values that are inconsistent with thevalue chosen for SA.3 A careful cartographer or patriotic Tasmanian might object that Tasmania should not be colored the same asits nearest mainland neighbor, to avoid the impression that it might be part of that state.4 Sadly, very few regions of the world, with the possible exception of Sulawesi, have tree-structured maps.

Page 17: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.4. The Structure of Problems 153

A

C

B D

E

F(a)

A CB D E F

(b)

Figure 5.10 (a) The constraint graph of a tree-structured CSP. (b) A linear ordering of thevariables consistent with the tree with A as the root.

WA

NT

SA

Q

NSW

V

T

WA

NTQ

NSW

V

T

(a) (b)

Figure 5.11 (a) The original constraint graph from Figure 5.1. (b) The constraint graphafter the removal of SA.

Now, any solution for the CSP after SA and its constraints are removed will be con-sistent with the value chosen for SA. (This works for binary CSPs; the situation is morecomplicated with higher-order constraints.) Therefore, we can solve the remaining tree withthe algorithm given above and thus solve the whole problem. Of course, in the general case(as opposed to map coloring) the value chosen for SA could be the wrong one, so we wouldneed to try each of them. The general algorithm is as follows:

1. Choose a subset S from VARIABLES[csp] such that the constraint graph becomes a treeafter removal of S. S is called a cycle cutset.CYCLE CUTSET

2. For each possible assignment to the variables in S that satisfies all constraints on S,

(a) remove from the domains of the remaining variables any values that are inconsis-tent with the assignment for S, and

(b) If the remaining CSP has a solution, return it together with the assignment for S.

If the cycle cutset has size c, then the total runtime is O(dc · (n − c)d2). If the graph is“nearly a tree” then c will be small and the savings over straight backtracking will be huge.

Page 18: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

154 Chapter 5. Constraint Satisfaction Problems

In the worst case, however, c can be as large as (n − 2). Finding the smallest cycle cutset isNP-hard, but several efficient approximation algorithms are known for this task. The overallalgorithmic approach is called cutset conditioning; we will see it again in Chapter 14, whereCUTSET

CONDITIONING

it is used for reasoning about probabilities.The second approach is based on constructing a tree decomposition of the constraintTREE

DECOMPOSITION

graph into a set of connected subproblems. Each subproblem is solved independently, and theresulting solutions are then combined. Like most divide-and-conquer algorithms, this workswell if no subproblem is too large. Figure 5.12 shows a tree decomposition of the map-coloring problem into five subproblems. A tree decomposition must satisfy the followingthree requirements:

• Every variable in the original problem appears in at least one of the subproblems.

• If two variables are connected by a constraint in the original problem, they must appeartogether (along with the constraint) in at least one of the subproblems.

• If a variable appears in two subproblems in the tree, it must appear in every subproblemalong the path connecting those subproblems.

The first two conditions ensure that all the variables and constraints are represented in thedecomposition. The third condition seems rather technical, but simply reflects the constraintthat any given variable must have the same value in every subproblem in which it appears;the links joining subproblems in the tree enforce this constraint. For example, SA appears inall four of the connected subproblems in Figure 5.12. You can verify from Figure 5.11 thatthis decomposition makes sense.

T

WA

NT

SA

NT

SA

Q

SA

Q

NSW

SA NSW

V

Figure 5.12 A tree decomposition of the constraint graph in Figure 5.11(a).

We solve each subproblem independently; if any one has no solution, we know the en-tire problem has no solution. If we can solve all the subproblems, then we attempt to construct

Page 19: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.5. Summary 155

a global solution as follows. First, we view each subproblem as a “mega-variable” whose do-main is the set of all solutions for the subproblem. For example, the leftmost subproblems inFigure 5.12 is a map-coloring problem with three variables and hence has six solutions—oneis {WA = red ,SA = blue,NT = green}. Then, we solve the constraints connecting thesubproblems using the efficient algorithm for trees given earlier. The constraints betweensubproblems simply insist that the subproblem solutions agree on their shared variables. Forexample, given the solution {WA = red ,SA = blue,NT = green} for the first subproblem,the only consistent solution for the next subproblem is {SA = blue,NT = green, Q = red}.

A given constraint graph admits many tree decompositions; in choosing a decompo-sition, the aim is to make the subproblems as small as possible. The tree width of a treeTREE WIDTH

decomposition of a graph is one less than the size of the largest subproblem; the tree widthof the graph itself is defined to be the minimum tree width among all its tree decompositions.If a graph has tree width w, and we are given the corresponding tree decomposition, then theproblem can be solved in O(ndw+1) time. Hence, CSPs with constraint graphs of boundedtree width are solvable in polynomial time. Unfortunately, finding the decomposition withminimal tree width is NP-hard, but there are heuristic methods that work well in practice.

5.5 SUMMARY

• Constraint satisfaction problems (or CSPs) consist of variables with constraints onthem. Many important real-world problems can be described as CSPs. The structure ofa CSP can be represented by its constraint graph.

• Backtracking search, a form of depth-first search, is commonly used for solving CSPs.

• The minimum remaining values and degree heuristics are domain-independent meth-ods for deciding which variable to choose next in a backtracking search. The least-constraining-value heuristic helps in ordering the variable values.

• By propagating the consequences of the partial assignments that it constructs, the back-tracking algorithm can reduce greatly the branching factor of the problem. Forwardchecking is the simplest method for doing this. Arc consistency enforcement is a morepowerful technique, but can be more expensive to run.

• Backtracking occurs when no legal assignment can be found for a variable. Conflict-directed backjumping backtracks directly to the source of the problem.

• Local search using the min-conflicts heuristic has been applied to constraint satisfactionproblems with great success.

• The complexity of solving a CSP is strongly related to the structure of its constraintgraph. Tree-structured problems can be solved in linear time. Cutset conditioning canreduce a general CSP to a tree-structured one and is very efficient if a small cutset canbe found. Tree decomposition techniques transform the CSP into a tree of subproblemsand are efficient if the tree width of the constraint graph is small.

Page 20: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

156 Chapter 5. Constraint Satisfaction Problems

BIBLIOGRAPHICAL AND HISTORICAL NOTES

The earliest work related to constraint satisfaction dealt largely with numerical constraints.Equational constraints with integer domains were studied by the Indian mathematician Brah-magupta in the seventh century; they are often called Diophantine equations, after the GreekDIOPHANTINE

EQUATIONS

mathematician Diophantus (c. 200–284), who actually considered the domain of positive ra-tionals. Systematic methods for solving linear equations by variable elimination were studiedby Gauss (1829); the solution of linear inequality constraints goes back to Fourier (1827).

Finite-domain constraint satisfaction problems also have a long history. For example,graph coloring (of which map coloring is a special case) is an old problem in mathematics.GRAPH COLORING

According to Biggs et al. (1986), the four-color conjecture (that every planar graph can becolored with four or fewer colors) was first made by Francis Guthrie, a student of de Morgan,in 1852. It resisted solution—despite several published claims to the contrary—until a proofwas devised, with the aid of a computer, by Appel and Haken (1977).

Specific classes of constraint satisfaction problems occur throughout the history ofcomputer science. One of the most influential early examples was the SKETCHPAD sys-tem (Sutherland, 1963), which solved geometric constraints in diagrams and was the fore-runner of modern drawing programs and CAD tools. The identification of CSPs as a generalclass is due to Ugo Montanari (1974). The reduction of higher-order CSPs to purely binaryCSPs with auxiliary variables (see Exercise 5.11) is due originally to the 19th-century logi-cian Charles Sanders Peirce. It was introduced into the CSP literature by Dechter (1990b) andwas elaborated by Bacchus and van Beek (1998). CSPs with preferences among solutions arestudied widely in the optimization literature; see Bistarelli et al. (1997) for a generalizationof the CSP framework to allow for preferences. The bucket-elimination algorithm (Dechter,1999) can also be applied to optimization problems.

Backtracking search for constraint satisfaction is due to Bitner and Reingold (1975),although they trace the basic algorithm back to the 19th century. Bitner and Reingold alsointroduced the MRV heuristic, which they called the most-constrained-variable heuristic.Brelaz (1979) used the degree heuristic as a tie-breaker after applying the MRV heuristic.The resulting algorithm, despite its simplicity, is still the best method for k-coloring arbitrarygraphs. Haralick and Elliot (1980) proposed the least-constraining-value heuristic.

Constraint propagation methods were popularized by Waltz’s (1975) success on poly-hedral line-labeling problems for computer vision. Waltz showed that, in many problems,propagation completely eliminates the need for backtracking. Montanari (1974) introducedthe notion of constraint networks and propagation by path consistency. Alan Mackworth(1977) proposed the AC-3 algorithm for enforcing arc consistency as well as the general ideaof combining backtracking with some degree of consistency enforcement. AC-4, a moreefficient arc consistency algorithm, was developed by Mohr and Henderson (1986). Soon af-ter Mackworth’s paper appeared, researchers began experimenting with the tradeoff betweenthe cost of consistency enforcement and the benefits in terms of search reduction. Haralickand Elliot (1980) favored the minimal forward checking algorithm described by McGregor(1979), whereas Gaschnig (1979) suggested full arc consistency checking after each variable

Page 21: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.5. Summary 157

assignment—an algorithm later called MAC by Sabin and Freuder (1994). The latter paperprovides somewhat convincing evidence that, on harder CSPs, full arc consistency checkingpays off. Freuder (1978, 1982) investigated the notion of k-consistency and its relationshipto the complexity of solving CSPs. Apt (1999) describes a generic algorithmic frameworkwithin which consistency propagation algorithms can be analyzed.

Special methods for handling higher-order constraints have been developed primarilywithin the context of constraint logic programming. Marriott and Stuckey (1998) pro-vide excellent coverage of research in this area. The Alldiff constraint was studied by Regin(1994). Bounds constraints were incorporated into constraint logic programming by Van Hen-tenryck et al. (1998).

The basic backjumping method is due to John Gaschnig (1977, 1979). Kondrak andvan Beek (1997) showed that this algorithm is essentially subsumed by forward checking.Conflict-directed backjumping was devised by Prosser (1993). The most general and pow-erful form of intelligent backtracking was actually developed very early on by Stallman andSussman (1977). Their technique of dependency-directed backtracking led to the develop-

DEPENDENCY-DIRECTEDBACKTRACKING

ment of truth maintenance systems (Doyle, 1979), which we will discuss in Section 10.8.The connection between the two areas is analyzed by de Kleer (1989).

The work of Stallman and Sussman also introduced the idea of constraint record-ing, in which partial results obtained by search can be saved and reused later in the search.CONSTRAINT

RECORDING

The idea was introduced formally into backtracking search by Dechter (1990a). Backmark-ing (Gaschnig, 1979) is a particularly simple method in which consistent and inconsistentBACKMARKING

pairwise assignments are saved and used to avoid rechecking constraints. Backmarking canbe combined with conflict-directed backjumping; Kondrak and van Beek (1997) present ahybrid algorithm that provably subsumes either method taken separately. The method ofdynamic backtracking (Ginsberg, 1993) retains successful partial assignments from laterDYNAMIC

BACKTRACKING

subsets of variables when backtracking over an earlier choice that does not invalidate thelater success.

Local search in constraint satisfaction problems was popularized by the work of Kirk-patrick et al. (1983) on simulated annealing (see Chapter 4), which is widely used forscheduling problems. The min-conflicts heuristic was first proposed by Gu (1989) and was de-veloped independently by Minton et al. (1992). Sosic and Gu (1994) showed how it could beapplied to solve the 3,000,000 queens problem in less than a minute. The astounding successof local search using min-conflicts on the n-queens problem led to a reappraisal of the natureand prevalence of “easy” and “hard” problems. Peter Cheeseman et al. (1991) explored thedifficulty of randomly generated CSPs and discovered that almost all such problems eitherare trivially easy or have no solutions. Only if the parameters of the problem generator areset in a certain narrow range, within which roughly half of the problems are solvable, do wefind “hard” problem instances. We discuss this phenomenon further in Chapter 7.

Work relating the structure and complexity of CSPs originates with Freuder (1985), whoshowed that search on arc-consistent trees works without any backtracking. A similar result,with extensions to acyclic hypergraphs, was developed in the database community (Beeriet al., 1983). Since those papers were published, there has been a great deal of progress indeveloping more general results relating the complexity of solving a CSP to the structure of

Page 22: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

158 Chapter 5. Constraint Satisfaction Problems

its constraint graph. The notion of tree width was introduced by the graph theorists Robertsonand Seymour (1986). Dechter and Pearl (1987, 1989), building on the work of Freuder, ap-plied the same notion (which they called induced width) to constraint satisfaction problemsand developed the tree decomposition approach sketched in Section 5.4. Drawing on thiswork and on results from database theory, Gottlob et al. (1999a, 1999b) developed a notion,hypertree width, that is based on the characterization of the CSP as a hypergraph. In addi-tion to showing that any CSP with hypertree width w can be solved in time O(nw+1 log n),they also showed that hypertree width subsumes all previously defined measures of “width”in the sense that there are cases where the hypertree width is bounded and the other measuresare unbounded.

There are several good surveys of CSP techniques, including those by Kumar (1992),Dechter and Frost (1999), and Bartak (2001); and the encyclopedia articles by Dechter (1992)and Mackworth (1992). Pearson and Jeavons (1997) survey tractable classes of CSPs, cover-ing both structural decomposition methods and methods that rely on properties of the domainsor constraints themselves. Kondrak and van Beek (1997) give an analytical survey of back-tracking search algorithms, and Bacchus and van Run (1995) give a more empirical survey.The texts by Tsang (1993) and by Marriott and Stuckey (1998) go into much more depththan has been possible in this chapter. Several interesting applications are described in thecollection edited by Freuder and Mackworth (1994). Papers on constraint satisfaction ap-pear regularly in Artificial Intelligence and in the specialist journal, Constraints. The primaryconference venue is the International Conference on Principles and Practice of ConstraintProgramming, often called CP.

EXERCISES

5.1 Define in your own words the terms constraint satisfaction problem, constraint, back-tracking search, arc consistency, backjumping and min-conflicts.

5.2 How many solutions are there for the map-coloring problem in Figure 5.1?

5.3 Explain why it is a good heuristic to choose the variable that is most constrained, butthe value that is least constraining in a CSP search.

5.4 Consider the problem of constructing (not solving) crossword puzzles:5 fitting wordsinto a rectangular grid. The grid, which is given as part of the problem, specifies whichsquares are blank and which are shaded. Assume that a list of words (i.e., a dictionary) isprovided and that the task is to fill in the blank squares using any subset of the list. Formulatethis problem precisely in two ways:

a. As a general search problem. Choose an appropriate search algorithm, and specify aheuristic function, if you think one is needed. Is it better to fill in blanks one letter at atime or one word at a time?

5 Ginsberg et al. (1990) discuss several methods for constructing crossword puzzles. Littman et al. (1999) tacklethe harder problem of solving them.

Page 23: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

Section 5.5. Summary 159

b. As a constraint satisfaction problem. Should the variables be words or letters?

Which formulation do you think will be better? Why?

5.5 Give precise formulations for each of the following as constraint satisfaction problems:

a. Rectilinear floor-planning: find nonoverlapping places in a large rectangle for a num-FLOOR-PLANNING

ber of smaller rectangles.

b. Class scheduling: There is a fixed number of professors and classrooms, a list of classesCLASS SCHEDULING

to be offered, and a list of possible time slots for classes. Each professor has a set ofclasses that he or she can teach.

5.6 Solve the cryptarithmetic problem in Figure 5.2 by hand, using backtracking, forwardchecking, and the MRV and least-constraining-value heuristics.

5.7 Figure 5.5 tests out various algorithms on the n-queens problem. Try these same al-gorithms on map-coloring problems generated randomly as follows: scatter n points on theunit square; selecting a point X at random, connect X by a straight line to the nearest pointY such that X is not already connected to Y and the line crosses no other line; repeat theprevious step until no more connections are possible. Construct the performance table for thelargest n you can manage, using both d= 3 and d=4 colors. Comment on your results.

5.8 Use the AC-3 algorithm to show that arc consistency is able to detect the inconsistencyof the partial assignment {WA= red , V = blue} for the problem shown in Figure 5.1.

5.9 What is the worst-case complexity of running AC-3 on a tree-structured CSP?

5.10 AC-3 puts back on the queue every arc (Xk,Xi) whenever any value is deleted fromthe domain of Xi, even if each value of Xk is consistent with several remaining values of Xi.Suppose that, for every arc (Xk,Xi), we keep track of the number of remaining values of Xi

that are consistent with each value of Xk. Explain how to update these numbers efficientlyand hence show that arc consistency can be enforced in total time O(n2d2).

5.11 Show how a single ternary constraint such as “A + B = C” can be turned into threebinary constraints by using an auxiliary variable. You may assume finite domains. (Hint:consider a new variable that takes on values which are pairs of other values, and considerconstraints such as “X is the first element of the pair Y .”) Next, show how constraints withmore than three variables can be treated similarly. Finally, show how unary constraints can beeliminated by altering the domains of variables. This completes the demonstration that anyCSP can be transformed into a CSP with only binary constraints.

5.12 Suppose that a graph is known to have a cycle cutset of no more than k nodes. Describea simple algorithm for finding a minimal cycle cutset whose runtime is not much more thanO(nk) for a CSP with n variables. Search the literature for methods for finding approximatelyminimal cycle cutsets in time that is polynomial in the size of the cutset. Does the existenceof such algorithms make the cycle cutset method practical?

5.13 Consider the following logic puzzle: In five houses, each with a different color, live5 persons of different nationalities, each of whom prefer a different brand of cigarette, a

Page 24: 5 CONSTRAINT SATISFACTION PROBLEMS - Semantic Scholar file5 CONSTRAINT SATISFACTION PROBLEMS In which we see how treating states as more than just little black boxes leads to the invention

160 Chapter 5. Constraint Satisfaction Problems

different drink, and a different pet. Given the following facts, the question to answer is“Where does the zebra live, and in which house do they drink water?”

The Englishman lives in the red house.The Spaniard owns the dog.The Norwegian lives in the first house on the left.Kools are smoked in the yellow house.The man who smokes Chesterfields lives in the house next to the man with the fox.The Norwegian lives next to the blue house.The Winston smoker owns snails.The Lucky Strike smoker drinks orange juice.The Ukrainian drinks tea.The Japanese smokes Parliaments.Kools are smoked in the house next to the house where the horse is kept.Coffee is drunk in the green house.The Green house is immediately to the right (your right) of the ivory house.Milk is drunk in the middle house.

Discuss different representations of this problem as a CSP. Why would one prefer one repre-sentation over another?