🛠 Practical: CSP Solver

Hands-On

🎯 What You'll Build

  • An interactive 8-Queens demo with visual board
  • Understand backtracking visualization concepts
  • Explore a Sudoku solver example with constraint propagation
  • See map coloring as an interactive concept
  • Implement CSP algorithms in pseudocode

1. Interactive 8-Queens Demo

Click "Solve" to watch the backtracking algorithm place 8 queens on the board. Click "Reset" to clear. The algorithm tries column by column, placing a queen in a safe row, and backtracks when stuck.

💡 How the Demo Works

The solver uses the CSP formulation: one variable per column (Qi = row of queen in column i), domain {0,…,7}. It tries rows top-to-bottom in each column and backtracks when no safe row exists. The animation shows each placement and removal in real time.

2. Backtracking Visualization Concept

Understanding how backtracking unfolds is crucial. Here's the conceptual trace for 4-Queens:

4-Queens Backtracking Trace

Column 0: Try row 0
  Column 1: Try row 0 → conflict (same row) ✗
  Column 1: Try row 1 → conflict (diagonal) ✗
  Column 1: Try row 2 → OK ✓
    Column 2: Try row 0 → conflict (diagonal with Q1) ✗
    Column 2: Try row 1 → conflict (row with col0? No. diagonal with col1? Yes) ✗
    Column 2: Try row 2 → conflict (same row as col1) ✗
    Column 2: Try row 3 → conflict (diagonal) ✗
    BACKTRACK to Column 1
  Column 1: Try row 3 → OK ✓
    Column 2: Try row 0 → conflict (diagonal) ✗
    Column 2: Try row 1 → OK ✓
      Column 3: Try row 0 → conflict ✗
      Column 3: Try row 1 → conflict ✗
      Column 3: Try row 2 → conflict ✗
      Column 3: Try row 3 → conflict ✗
      BACKTRACK to Column 2
    Column 2: Try row 2 → conflict ✗
    Column 2: Try row 3 → conflict ✗
    BACKTRACK to Column 1
  Column 1: No more rows → BACKTRACK to Column 0
Column 0: Try row 1
  Column 1: Try row 3 → OK ✓
    Column 2: Try row 0 → OK ✓
      Column 3: Try row 2 → OK ✓ → SOLUTION: [1, 3, 0, 2] ★
                  

The solution Q0=1, Q1=3, Q2=0, Q3=2 means queens are placed at (row 1, col 0), (row 3, col 1), (row 0, col 2), (row 2, col 3).

3. Sudoku Solver Example

📚 Sudoku as a CSP

81 variables (one per cell), domains {1–9} (pre-filled cells have singleton domains), 27 AllDiff constraints (9 rows + 9 columns + 9 boxes).

Pseudocode: Sudoku CSP Solver

function SOLVE-SUDOKU(grid)
    // 1. Enforce node consistency (pre-filled cells)
    for each cell (i,j) with value v:
        D[i][j] ← {v}

    // 2. Run AC-3 to propagate constraints
    if not AC-3(sudoku_csp) then return failure

    // 3. Check if solved (all domains singleton)
    if all |D[i][j]| = 1 then return grid

    // 4. Pick unassigned cell with MRV
    (i,j) ← cell with smallest |D[i][j]| > 1

    // 5. Backtrack with MAC
    for each v in D[i][j] do
        saved_domains ← copy of all domains
        D[i][j] ← {v}
        if MAC(sudoku_csp, (i,j)) then
            result ← SOLVE-SUDOKU(grid)
            if result ≠ failure then return result
        restore domains from saved_domains
    return failure
                  

✍ Sudoku AC-3 Propagation Example

Given a partially filled Sudoku where cell (0,0)=5:

  • Remove 5 from all cells in Row 0
  • Remove 5 from all cells in Column 0
  • Remove 5 from all cells in top-left 3×3 box
  • If any of those cells now has a singleton domain, propagate further

With AC-3 + MRV, most easy/medium Sudoku puzzles are solved without any backtracking — propagation alone determines all values!

4. Map Coloring Interactive Concept

📚 Map Coloring CSP Implementation

The map coloring problem is one of the simplest CSPs to implement. Here is the structure for the Australia problem:

Pseudocode: Map Coloring CSP

// Define the CSP
variables = [WA, NT, SA, Q, NSW, V, T]
domains = {
    WA: [R, G, B],  NT: [R, G, B],  SA: [R, G, B],
    Q:  [R, G, B],  NSW:[R, G, B],  V:  [R, G, B],
    T:  [R, G, B]
}
constraints = {
    (WA, NT): λ(a,b): a ≠ b,
    (WA, SA): λ(a,b): a ≠ b,
    (NT, SA): λ(a,b): a ≠ b,
    (NT, Q):  λ(a,b): a ≠ b,
    (SA, Q):  λ(a,b): a ≠ b,
    (SA, NSW):λ(a,b): a ≠ b,
    (SA, V):  λ(a,b): a ≠ b,
    (Q, NSW): λ(a,b): a ≠ b,
    (NSW, V): λ(a,b): a ≠ b
}

// Solve with Backtracking + MRV + Forward Checking
solution ← BACKTRACKING-SEARCH(map_csp)
// Expected: {WA:R, NT:G, SA:B, Q:R, NSW:G, V:R, T:R}
                  

💡 Applying Heuristics to Map Coloring

MRV: Start with SA (most constraints = 5 neighbours). LCV: After SA=B, for NT, try the colour that leaves the most options for its neighbours. Forward checking: After SA=B, remove B from WA, NT, Q, NSW, V domains. This typically solves Australia in ~7 steps with no backtracking.

5. Exercises

✍ Exercise 1: 4-Queens by Hand

Using backtracking with MRV and forward checking, solve the 4-Queens problem. Show the domain of each variable after each assignment. How many backtracks occur?

Show Solution

Assign Q0=1: D1={3}, D2={0}, D3={2}. (After forward checking removes row/diagonal conflicts.)

Assign Q1=3 (MRV: D1 has 1 value): D2={0}, D3={2}.

Assign Q2=0: D3={2}.

Assign Q3=2: Solution [1,3,0,2]. 0 backtracks!

✍ Exercise 2: AC-3 Trace

Given variables A, B, C with DA={1,2,3}, DB={1,2}, DC={2,3} and constraints A > B and B < C, run AC-3 and show the final domains.

Show Solution

Process (A, B): A > B. A=1: need B < 1 → no support → remove 1. A=2: B=1 ✓. A=3: B=1 ✓. DA={2,3}.

Process (B, A): B < A. B=1: A=2 ✓. B=2: A=3 ✓. No change.

Process (B, C): B < C. B=1: C=2 ✓. B=2: C=3 ✓. No change.

Process (C, B): C > B. C=2: B=1 ✓. C=3: B=1 ✓. No change.

Final domains: DA={2,3}, DB={1,2}, DC={2,3}.

✍ Exercise 3: Cutset Conditioning

For the Australia map CSP, identify a minimum cycle cutset. How many tree-CSP solves are needed? What is the total worst-case work?

Show Solution

Minimum cutset: {SA} (size 1). Removing SA makes the rest a forest: WA–NT, Q–NSW–V, T (disconnected).

For each of SA's 3 values, solve the tree(s): 3 × O(6 × 9) = O(162). Much better than brute force 37 = 2187.