Basic Constraint Satisfaction Problem (CSP) with Three Variables and Backtracking

Basic Constraint Satisfaction Problem (CSP) with Three Variables and Backtracking

Verified Sources
Nov 4, 2026

A constraint satisfaction problem (CSP) is defined by: (i) a set of variables, (ii) a domain, and (iii) a set of constraints that restrict combinations of values. In a CSP, the goal is to find an assignment of values to all variables such that all constraints are satisfied.

To make this concrete, we will define a simple CSP with three variables X,Y,ZX, Y, Z, each having a small finite domain, plus binary constraints that relate pairs of variables. We will then solve it using backtracking search, where we assign variables one by one, check constraint satisfaction so far, and backtrack when we hit a dead end.

CSP components (what you “plug in”)

  • Domain: limits the values each variable can take.
  • Unary constraint: restricts one variable’s values.
  • Binary constraint: restricts allowed value pairs.
  • Consistency check: used during backtracking.

Constraint Satisfaction Problem (CSP) and Backtracking Search (Intro)

A Concrete 3-Variable CSP Example

We define a CSP with three variables:

  • X∈{1,2,3}X \in \{1,2,3\}
  • Y∈{1,2,3}Y \in \{1,2,3\}
  • Z∈{1,2,3}Z \in \{1,2,3\}

And constraints:

  1. X≠YX \neq Y (all different between XX and YY)
  2. Y≠ZY \neq Z (all different between YY and ZZ)
  3. X=ZX = Z (ties XX and ZZ together)

This CSP is small but illustrates everything you asked for: the role of the domain (what values are even allowed), and the role of the constraints (which combinations/relations are permitted).

Formal CSP definition

  • Variables: V={X,Y,Z}V=\{X,Y,Z\}
  • Domains:
    D(X)=D(Y)=D(Z)={1,2,3}D(X)=D(Y)=D(Z)=\{1,2,3\}
  • Constraints:
    • CXY:X≠YC_{XY}: X \neq Y
    • CYZ:Y≠ZC_{YZ}: Y \neq Z
    • CXZ:X=ZC_{XZ}: X = Z

What constraints do (intuitively)

  • The domain says “you may try these values.”
  • The constraints say “you may not keep these combinations if they violate the rules.”

So in backtracking, we don’t just search blindly—we prune when the partial assignment already violates any constraint.

Pro Tip: Think “pruning”

In backtracking CSP solving, constraints are used to prune invalid partial assignments early, reducing the search space.

Role of the Domain and Constraints (in backtracking)

1) Domain controls branching factor

Backtracking assigns values from each variable’s domain. If D(X)D(X) has size kk, then at the point when you pick XX, there are up to kk choices. Smaller domains generally make search faster.

2) Constraints control pruning

A constraint can rule out:

  • complete assignments (when all involved variables are assigned), and
  • partial assignments (when some involved variables are assigned).

For our example:

  • If you already assigned XX and you try YY, the constraint X≠YX \neq Y can reject a candidate immediately.
  • If you assigned YY and you try ZZ, the constraint Y≠ZY \neq Z can reject immediately.
  • If you attempt to maintain X=ZX = Z, then once XX is assigned, ZZ becomes forced to that value (or rejected if you try something else).

Backtracking Backtracking-Solve the 3-Variable CSP

  1. 1
    Step 1

    Use the order X→Y→ZX \rightarrow Y \rightarrow Z (any fixed order works).

  2. 2
    Step 2

    Start with {}\{\} (no variables assigned yet).

  3. 3
    Step 3

    Try X=1X=1; since there are no constraints violated yet (no other variables assigned), keep the partial assignment.

  4. 4
    Step 4

    Given X=1X=1, enforce XeqYX eq Y. Allowed YY values are {2,3}\{2,3\}. Try Y=2Y=2 first.

  5. 5
    Step 5

    Given X=1X=1 and Y=2Y=2, constraints require: (i) YeqZY eq Z so Zeq2Z eq 2, and (ii) X=ZX=Z so Z=1Z=1. Check: Z=1Z=1 satisfies Zeq2Z eq 2.

  6. 6
    Step 6

    Now X=1,Y=2,Z=1X=1, Y=2, Z=1 is a complete assignment and satisfies all constraints.

  7. 7
    Step 7

    If you had tried Y=1Y=1 while X=1X=1, it would violate XeqYX eq Y immediately; you would reject and try the next YY value.

Backtracking Search Tree (visual)

Below is a simplified search tree showing how constraints prune choices.

Search Impact of Constraints (Qualitative)

This illustrates that constraints reduce the number of candidate branches kept during search.

Common CSP/Backtracking Details

Backtracking CSP Solving Lifecycle

Model

1

Specify variables, domains, and constraints."

Order & Start

2

Pick a variable order and begin with an empty assignment."

Select Value

3

Try a value from the current variable’s domain."

Check Constraints

4

If any constraint is violated, reject immediately."

Recurse or Backtrack

5

If consistent, move deeper; otherwise undo and try next value."

Finish

6

When all variables assigned consistently, output the solution."

CSP + Backtracking Quick Recall

1 / 5
Question · Term

What is a CSP?

Click to reveal
Answer · Definition

A constraint satisfaction problem: variables with domains must satisfy constraints; goal is a satisfying assignment.

Knowledge Check

Question 1 of 4
Q1Single choice

In a CSP, the domain of a variable specifies: