Constraint Satisfaction
Problems
1
Intro Example: 8-Queens
Generate-and-test: 88 combinations
2
Intro Example: 8-Queens
3
Constraint Satisfaction Problem
Set of variables {X1, X2, …, Xn}
Each variable Xi has a domain Di of possible
values
Usually Di is discrete and finite
Set of constraints {C1, C2, …, Cp}
Each constraint Ck involves a subset of variables
and specifies the allowable combinations of values
of these variables
Assign a value to every variable such that all
constraints are satisfied
4
Example: 8-Queens Problem
8 variables Xi, i = 1 to 8
Domain for each variable {1,2,…,8}
Constraints are of the forms:
Xi = k Xj k for all j = 1 to 8, ji
Xi = ki, Xj = kj |i-j| | ki - kj|
for all j = 1 to 8, ji
5
Example: Map Coloring
NT
WA
Q
SA
NSW
V
T
• 7 variables {WA,NT,SA,Q,NSW,V,T}
• Each variable has the same domain {red, green, blue}
• No two adjacent variables have the same value:
WANT, WASA, NTSA, NTQ, SAQ, SANSW, SAV,QNSW, NSWV
6
Constraint Graph
Binary constraints
NT
WA
Q
NSW
SA
V
T
Two variables are adjacent or neighbors if they
are connected by an edge or an arc
7
Map Coloring
{}
WA=red
WA=red
NT=green
WA=red
NT=green
Q=red
WA=green
WA=blue
WA=red
NT=blue
WA=red
NT=green
Q=blue
NT
WA
Q
SA
NSW
V
T
8
Backtracking Algorithm
CSP-BACKTRACKING(PartialAssignment a)
If a is complete then return a
X select an unassigned variable
D select an ordering for the domain of X
For each value v in D do
If v is consistent with a then
Add (X= v) to a
result CSP-BACKTRACKING(a)
If result failure then return result
Return failure
CSP-BACKTRACKING({})
9
Questions
1. Which variable X should be assigned a
value next?
2. In which order should its domain D be
sorted?
3. In which order should constraints be
verified?
10
Choice of Variable
Map coloring
NT
WA
Q
SA
NSW
V
T
11
Choice of Variable
8-queen
12
Choice of Variable
Most-constrained-variable heuristic:
Select a variable with the fewest
remaining values
= Fail First Principle
13
Choice of Variable
NT
WA
Q
SA
NSW
V
T
Most-constraining-variable heuristic:
Select the variable that is involved in the
largest number of constraints on other
unassigned variables
= Fail First Principle again
14
Choice of Value
NT
WA
Q
SA
NSW
V
{}
T
15
Choice of Value
NT
WA
Q
SA
NSW
V
{blue}
T
Least-constraining-value heuristic:
Prefer the value that leaves the largest subset
of legal values for other unassigned variables
16
Choice of Constraint to Test
Most-constraining-Constraint:
Prefer testing constraints that are more
difficult to satisfy
= Fail First Principle
17
Constraint Propagation …
… is the process of determining how
the possible values of one variable
affect the possible values of other
variables
18
Forward Checking
After a variable X is assigned a value v, look
at each unassigned variable Y that is
connected to X by a constraint and deletes
from Y’s domain any value that is inconsistent
with v
19
Map Coloring
NT
WA
Q
T
NSW
SA
V
WA
NT
Q
NSW
V
SA
T
RGB
RGB
RGB
RGB
RGB
RGB
RGB
20
Map Coloring
NT
WA
Q
T
NSW
SA
V
WA
NT
Q
NSW
V
SA
T
RGB
RGB
RGB
RGB
RGB
RGB
RGB
R
GB
RGB
RGB
RGB
GB
RGB
21
Map Coloring
NT
WA
Q
T
NSW
SA
V
WA
NT
Q
NSW
V
SA
T
RGB
RGB
RGB
RGB
RGB
RGB
RGB
R
GB
RGB
RGB
RGB
GB
RGB
R
B
G
RB
RGB
B
RGB
22
Map Coloring
NT
WA
Q
T
NSW
SA
V
WA
NT
RGB
RGB
R
Impossible assignments that forward
Q checking
NSWdo not
V detect
SA
T
RGB
RGB
RGB
RGB
RGB
GB
RGB
RGB
RGB
GB
RGB
R
B
G
RB
RGB
B
RGB
R
B
G
R
B
RGB
23
Example: 4-Queens Problem
1
2
3
4
X1
{1,2,3,4}
X2
{1,2,3,4}
X3
{1,2,3,4}
X4
{1,2,3,4}
1
2
3
4
24
Example: 4-Queens Problem
1
2
3
4
X1
{1,2,3,4}
X2
{1,2,3,4}
X3
{1,2,3,4}
X4
{1,2,3,4}
1
2
3
4
25
Example: 4-Queens Problem
1
2
3
4
X1
{1,2,3,4}
X2
{ , ,3,4}
X3
{ ,2, ,4}
X4
{ ,2,3, }
1
2
3
4
26
Example: 4-Queens Problem
1
2
3
4
X1
{1,2,3,4}
X2
{ , ,3,4}
X3
{ ,2, ,4}
X4
{ ,2,3, }
1
2
3
4
27
Example: 4-Queens Problem
1
2
3
4
X1
{1,2,3,4}
X2
{ , ,3,4}
X3
{ , , , }
X4
{ ,2,3, }
1
2
3
4
28
Example: 4-Queens Problem
1
1
2
3
4
X1
{ ,2,3,4}
X2
{1,2,3,4}
X3
{1,2,3,4}
X4
{1,2,3,4}
BT
2
3
4
29
Example: 4-Queens Problem
1
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, ,3, }
X4
{1, ,3,4}
BT
2
3
4
30
Example: 4-Queens Problem
1
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, ,3, }
X4
{1, ,3,4}
BT
2
3
4
31
Example: 4-Queens Problem
1
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, , , }
X4
{1, ,3, }
BT
2
3
4
32
Example: 4-Queens Problem
1
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, , , }
X4
{1, ,3, }
BT
2
3
4
33
Example: 4-Queens Problem
1
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, , , }
X4
{ , ,3, }
BT
2
3
4
34
Example: 4-Queens Problem
1
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, , , }
X4
{ , ,3, }
BT
2
3
4
35
Edge Labeling in Computer
Vision
36
Trihedral Objects
Objects in which exactly three plane
surfaces come together at each vertex.
Goal: label a 2-D object to produce a
3-D object
37
Labels of Edges
Convex edge:
two surfaces intersecting at an angle greater than 180°
Concave edge
two surfaces intersecting at an angle less than 180°
+ convex edge, both surfaces visible
− concave edge, both surfaces visible
convex edge, only one surface is visible and it is
on the right side of
38
Junction Label Sets
+
-
+
+
-
+
- -
+
+
-
+
-
-
-
-
+
+
+
(Waltz, 1975; Mackworth, 1977)
39
Edge Labeling
40
Edge Labeling
+
+
+
+
+
-
+
+
+
+
+
41
Edge Labeling as a CSP
A variable is associated with each
junction
The domain of a variable is the label set
of the corresponding junction
Each constraint imposes that the values
given to two adjacent junctions give the
same label to the joining edge
42
Edge Labeling
+
+
+
- -+
+
43
Edge Labeling
+
+
-
-
- -
-
-
+
+
+
44
Edge Labeling
+
+
+
+
+
+
+
-
+
- -
+
+
45
Edge Labeling
+
+
-
+
+
+
+
+
- 46
Removal of Arc Inconsistencies
REMOVE-ARC-INCONSISTENCIES(J,K)
removed false
X label set of J
Y label set of K
For every label y in Y do
If there exists no label x in X such that the
constraint (x,y) is satisfied then
Remove y from Y
If Y is empty then contradiction true
removed true
Label set of K Y
Return removed
47
CP Algorithm for Edge Labeling
Associate with every junction its label set
Q stack of all junctions
while Q is not empty do
J UNSTACK(Q)
For every junction K adjacent to J do
If REMOVE-ARC-INCONSISTENCIES(J,K) then
If K’s domain is non-empty then
STACK(K,Q)
Else return false
(Waltz, 1975; Mackworth, 1977)
48
© Copyright 2026 Paperzz