Artificial Intelligence & Neural Networks β Problem Solving and Searching Techniques, NEC licence examination syllabus (Nepal Engineering Council).
Constraint Satisfaction Problem (CSP)
A special kind of problem where knowing the structure lets you prune hard.
π Where this lives: university timetabling, staff rostering, exam scheduling, register allocation in compilers, and Sudoku are all constraint satisfaction problems, and they are solved by CSP techniques rather than general search because the structure is visible. When your university publishes an exam timetable where no student has two papers at once and no room is double-booked, a constraint solver produced it β and the reason it finished at all is the propagation and ordering heuristics in this topic. Search "constraint programming timetabling solver".
THE CLASSIC EXAMPLE β colouring the seven regions of Australia
so that no two neighbours share a colour.
VARIABLES WA, NT, Q, NSW, V, SA, T
DOMAIN {red, green, blue} for each
CONSTRAINTS the neighbouring pairs must differ:
SAβ WA, SAβ NT, SAβ Q, SAβ NSW, SAβ V,
WAβ NT, NTβ Q, Qβ NSW, NSWβ V
(T is an island β no constraints at all)
THE CONSTRAINT GRAPH:
WA ββββ NT ββββ Q
\ β /β
\ β / β
βββ SA ββββ β
β \ β
V ββββ NSW T (isolated)
NAIVE SEARCH: 3^7 = 2,187 complete assignments to test. With
4 colours it would be 4^7 = 16,384.
BACKTRACKING SEARCH is the basic algorithm: assign one variable
at a time, and backtrack as soon as a constraint is violated.
function BACKTRACKING-SEARCH(csp) returns a solution or failure
return BACKTRACK({}, csp)
function BACKTRACK(assignment, csp)
if assignment is complete then return assignment
var β SELECT-UNASSIGNED-VARIABLE(csp)
for each value in ORDER-DOMAIN-VALUES(var, assignment, csp)
if value is consistent with assignment then
add {var = value} to assignment
inferences β INFERENCE(csp, var, value)
if inferences β failure then
add inferences to assignment
result β BACKTRACK(assignment, csp)
if result β failure then return result
remove {var = value} and inferences from assignment
return failure
THE THREE PLACES TO BE CLEVER are the three named subroutines,
and this is what the exam asks about.
1. VARIABLE ORDERING β MINIMUM REMAINING VALUES (MRV)
Choose the variable with the FEWEST legal values remaining.
Also called "most constrained variable" or "fail first".
WHY IT WORKS: if a variable has only one legal value, assign
it now β and if it has none, discover the failure
immediately rather than after assigning ten other variables.
WORKED: after WA=red and NT=green, SA has only {blue} left.
MRV picks SA next, forcing the value and pruning everything
that would have contradicted it.
2. TIE-BREAKING β DEGREE HEURISTIC
Choose the variable involved in the most constraints on
unassigned variables. Used to break MRV ties, and to pick the
first variable when all domains are equal.
WORKED: at the start every region has 3 values, so MRV cannot
choose. SA has degree 5 β more than any other β so start
there. Assigning SA first immediately reduces five other
domains.
3. VALUE ORDERING β LEAST CONSTRAINING VALUE
Prefer the value that rules out the fewest choices for the
neighbouring variables.
NOTE THE ASYMMETRY, which is a favourite exam point:
FOR VARIABLES WE WANT TO FAIL FAST (most constrained first),
BUT FOR VALUES WE WANT TO SUCCEED (least constraining first).
The reason is that we need only ONE value to work, but ALL
variables must be assigned.
INFERENCE β the technique that makes CSPs tractable:
FORWARD CHECKING
When X is assigned, delete from the domain of each unassigned
neighbour any value inconsistent with X.
β detects failure earlier than plain backtracking
β does not look ahead far enough to see all implications
CONSTRAINT PROPAGATION AND ARC CONSISTENCY (AC-3)
A variable Xα΅’ is ARC-CONSISTENT with respect to Xβ±Ό if for
every value in Dα΅’ there is SOME value in Dβ±Ό satisfying the
constraint. A network is arc-consistent when every arc is.
AC-3 repeatedly revises arcs, re-queueing neighbours whenever
a domain shrinks, until no domain changes.
COMPLEXITY: O(cdΒ³) for c binary constraints and domains of
size d.
WHAT FORWARD CHECKING MISSES AND AC-3 CATCHES:
suppose after some assignments NT and SA both have domain
{blue}. Forward checking sees nothing wrong β each has a
legal value. AC-3 examines the arc NTβSA, finds that
choosing blue for NT leaves nothing for SA, empties a
domain, and reports failure immediately.
THE POINT: propagation detects inconsistency BEFORE any
further search, and detecting it one level earlier can prune
an entire subtree.
THE MEASURED EFFECT β published comparisons on hard CSPs show
plain backtracking taking many millions of consistency checks
on instances that MRV plus forward checking solves in a few
thousand. THE HEURISTICS ARE NOT MARGINAL IMPROVEMENTS; they
are the difference between solvable and not.
Structure, local search, and the n-queens comparison
EXPLOITING PROBLEM STRUCTURE β the deepest idea in CSP:
INDEPENDENT SUBPROBLEMS
Tasmania is not connected to anything, so it is an independent
subproblem and should be solved separately. Finding the
CONNECTED COMPONENTS of the constraint graph decomposes the
problem: if a CSP splits into subproblems of n/c variables
each, the cost falls from d^n to (n/c)Β·d^c β LINEAR in n
rather than exponential.
WORKED: 80 variables, domain size 2, and suppose it splits
into 20 subproblems of 4 variables.
undecomposed: 2^80 β 1.2 Γ 10^24
decomposed: 20 Γ 2^4 = 320
At a million nodes per second the first takes about
38 billion years and the second is instantaneous.
TREE-STRUCTURED CSPs
If the constraint graph is a TREE (no cycles), the CSP can be
solved in O(nΒ·dΒ²) β POLYNOMIAL time. The method: pick a root,
order the variables topologically, make each arc consistent
with its parent working backwards, then assign forwards with
no backtracking at all.
THIS IS A GENUINELY IMPORTANT RESULT: the difference between
exponential and polynomial is a property of the GRAPH'S
SHAPE, not of the algorithm.
CUTSET CONDITIONING
If the graph is nearly a tree, find a small subset of
variables (a CYCLE CUTSET) whose removal leaves a tree,
enumerate its assignments, and solve the remaining tree
quickly for each. Cost O(d^c Β· (nβc)dΒ²) for a cutset of size
c β practical when c is small.
LOCAL SEARCH FOR CSPs β a completely different approach:
Start with a complete but inconsistent assignment and repair
it. The MIN-CONFLICTS heuristic reassigns the variable in a
violated constraint to the value that minimises the number of
remaining conflicts.
β REMARKABLY EFFECTIVE. Min-conflicts solves the
million-queens problem in about 50 steps on average,
essentially independent of n β a result that is genuinely
startling next to systematic search.
β works well for ONLINE problems, where a schedule must be
repaired after a disruption rather than recomputed
β INCOMPLETE: it can get stuck in a local minimum and cannot
prove that no solution exists
THE N-QUEENS COMPARISON, which puts the whole topic in one
table:
n = 8, naive assignment of a square per queen
8^8 = 16,777,216 candidates
as a CSP, one queen per column, permutation formulation
8! = 40,320
n = 20, naive
20^20 β 1.05 Γ 10^26
n = 20, permutation
20! β 2.43 Γ 10^18
n = 1,000,000, min-conflicts local search
about 50 STEPS
THE PROGRESSION IS THE ARGUMENT OF THIS SECTION: better
formulation, then better inference and ordering, then β where
completeness can be sacrificed β a local method that ignores
the size of the space entirely. Each step is a larger win than
the last, and none of them is a faster computer.
The asymmetry between variable and value ordering is the detail that shows whether the heuristics are understood: choose the most constrained variable but the least constraining value. The reason is structural β every variable must eventually be assigned, so failing fast on variables saves work, while only one value per variable needs to succeed, so keeping options open helps.
π Go further: the industrial descendants of these techniques are SAT and SMT solvers, and they are startlingly good. Modern conflict-driven clause-learning solvers routinely handle problems with millions of variables β not by better search order alone but by learning a new constraint from every failure, so the same dead end is never re-entered. They now underpin hardware verification, program analysis, package dependency resolution (your package manager is running one) and automated theorem proving. It is one of the clearest cases in computing of a theoretically intractable problem being made practically tractable by exploiting structure. Search "CDCL SAT solver clause learning practical".
π‘ Exam angle: define a CSP by its three components (variables, domains, constraints) and define consistent, complete and partial assignments. Draw the map-colouring constraint graph and solve it β this is the standard worked question. Explain backtracking search and the three improvements: MRV, the degree heuristic for tie-breaking, and least-constraining-value, being ready to justify the asymmetry. Distinguish forward checking from arc consistency (AC-3) with an example of what forward checking misses. Know that tree-structured CSPs are solvable in polynomial time and that min-conflicts local search solves million-queens in about 50 steps.
Syllabus points
CSP definition and examples
Create a free account to tick topics off, take notes as you read, watch the video lessons and get a day-by-day study plan built around your exam date.
Related topics in Problem Solving and Searching Techniques