Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab: Constraint Satisfaction Problems (CSPs) - Assignment

Open In Colab

Exercise 1: Sudoku with Binary Variables (40 points)

In the lecture, we implemented Sudoku where each cell variable had a domain of {1, 2, ..., 9}. Now we’ll use a binary encoding where:

  • Variables: Xr,c,vX_{r,c,v} for each row rr, column cc, and value vv (where r,c,v∈{0,1,...,8}r, c, v \in \{0, 1, ..., 8\})

  • Domain: {0,1}\{0, 1\} for each variable

  • Meaning: Xr,c,v=1X_{r,c,v} = 1 if cell (r,c)(r, c) contains value v+1v+1, otherwise Xr,c,v=0X_{r,c,v} = 0

Constraints for Binary Sudoku

  1. Each cell has exactly one value:

    ∑v=08Xr,c,v=1,∀r,c∈{0,...,8}\sum_{v=0}^{8} X_{r,c,v} = 1, \quad \forall r, c \in \{0, ..., 8\}
  2. Each value appears exactly once in each row:

    ∑c=08Xr,c,v=1,∀r,v∈{0,...,8}\sum_{c=0}^{8} X_{r,c,v} = 1, \quad \forall r, v \in \{0, ..., 8\}
  3. Each value appears exactly once in each column:

    ∑r=08Xr,c,v=1,∀c,v∈{0,...,8}\sum_{r=0}^{8} X_{r,c,v} = 1, \quad \forall c, v \in \{0, ..., 8\}
  4. Each value appears exactly once in each 3×3 subgrid:

    ∑i=02∑j=02X3m+i,3n+j,v=1,∀m,n∈{0,1,2},∀v∈{0,...,8}\sum_{i=0}^{2} \sum_{j=0}^{2} X_{3m+i, 3n+j, v} = 1, \quad \forall m, n \in \{0, 1, 2\}, \forall v \in \{0, ..., 8\}

Exercise 1 Questions


Exercise 2: Traveling Salesman Problem (TSP) (30 points)

The Traveling Salesman Problem (TSP) asks: given a list of cities and distances between them, what is the shortest route that visits each city exactly once and returns to the starting city?

Binary CSP Formulation

  • Variables: Xi,jX_{i,j} for each pair of cities (i,j)(i, j) where i≠ji \neq j

  • Domain: {0,1}\{0, 1\}

  • Meaning: Xi,j=1X_{i,j} = 1 if the tour goes directly from city ii to city jj

Constraints

  1. Leave each city exactly once:

    ∑j≠iXi,j=1,∀i\sum_{j \neq i} X_{i,j} = 1, \quad \forall i
  2. Enter each city exactly once:

    ∑i≠jXi,j=1,∀j\sum_{i \neq j} X_{i,j} = 1, \quad \forall j
  3. Subtour elimination: Prevent disconnected cycles (we’ll use a position-based approach)

  4. Total distance constraint:

    ∑i∑j≠idi,j⋅Xi,j≤C\sum_{i} \sum_{j \neq i} d_{i,j} \cdot X_{i,j} \leq C

Exercise 2 Questions


Exercise 3: Graph Coloring Problem (30 points)

The Graph Coloring Problem asks: given a graph, assign colors to vertices such that no two adjacent vertices share the same color, using at most kk colors.

Binary CSP Formulation

  • Variables: Xv,cX_{v,c} for each vertex vv and color cc

  • Domain: {0,1}\{0, 1\}

  • Meaning: Xv,c=1X_{v,c} = 1 if vertex vv is assigned color cc

Constraints

  1. Each vertex gets exactly one color:

    ∑c=1kXv,c=1,∀v∈V\sum_{c=1}^{k} X_{v,c} = 1, \quad \forall v \in V
  2. Adjacent vertices have different colors:

    Xu,c+Xv,c≤1,∀(u,v)∈E,∀c∈{1,...,k}X_{u,c} + X_{v,c} \leq 1, \quad \forall (u,v) \in E, \forall c \in \{1, ..., k\}

This means if vertex uu has color cc, then vertex vv cannot have color cc (and vice versa).

Exercise 3 Questions