Back to CSP Lecture

CSP Formulation: Map Coloring

Learn CSP Fundamentals through Interactive Map Coloring

Learning Objectives

๐ŸŽฏ Variables: Understand how real-world entities become CSP variables
๐ŸŽจ Domains: Learn how to define possible values for each variable
๐Ÿ”— Constraints: Master relationships between variables that must be satisfied
1
2
3
4
Step 2: Interactive Map Coloring - Click regions and choose colors

Example 1: Regional Map Coloring

Interactive map with adjacent regions that must have different colors

Interactive Map

Click on a region to select it, then choose a color from the palette below.

A B C D E F G H I J K
Problem Goal
Objective: Color all map regions such that no two adjacent regions share the same color.

Challenge: Use the minimum number of colors possible!
Current CSP State
0
Regions Colored
0
Violations
CSP Formulation
Variables (X)

X = {A, B, C, D, E, F, G, H, I, J, K} - All regions on the map

Domains (D)

For all Xi: D(Xi) = {Red, Blue, Green, Yellow, Purple, Orange}

Constraints (C)

Adjacent regions must have different colors: Xi โ‰  Xj for all adjacent pairs

Active Constraints

Real-time constraint checking shows which adjacency rules are satisfied (โœ“) or violated (โœ—)

Course Schedule Builder

Assign courses to time slots while respecting all scheduling constraints.

Time
Monday
Tuesday
Wednesday
Thursday
Friday
9:00
11:00
1:00
Available Courses
CS 101-A
Intro Programming
Dr. Smith โ€ข 60 students
CS 101-B
Intro Programming
Dr. Smith โ€ข 50 students
MATH 101
Calculus I
Dr. Johnson โ€ข 80 students
CS 201
Data Structures
Dr. Smith โ€ข 45 students
PHYS 101
Physics I
Dr. Brown โ€ข 70 students
MATH 201
Linear Algebra
Dr. Johnson โ€ข 40 students
Scheduling Goal
Objective: Schedule all courses without conflicts.

Challenge: Respect instructor availability, room capacity, and prerequisites!
Current Selection
0
Courses Scheduled
0
Conflicts
CSP Formulation
Variables (X)

X = {CS101-A, CS101-B, MATH101, CS201, PHYS101, MATH201} - Course sections to schedule

Domains (D)

D(Xi) = {Mon-9, Tue-9, ..., Thu-13} - Available room-time slots (13 total)

Constraints (C)

โ€ข Instructor Conflict: Dr. Smith cannot teach both CS sections simultaneously
โ€ข Room Capacity: Large lectures need big rooms (A101=100, others=50 seats)
โ€ข Instructor Availability: Dr. Brown unavailable Friday afternoons

Active Scheduling Constraints

Real-time constraint checking shows scheduling conflicts and satisfied requirements

Interactive N-Queens Solver

Place N queens on an Nร—N chessboard so no two queens attack each other.

N-Queens Goal
Objective: Place N queens on an Nร—N chessboard such that no two queens attack each other.

Challenge: Queens attack horizontally, vertically, and diagonally!
Current State
0
Queens Placed
0
Violations
CSP Formulation
Variables (X)

X = {Qโ‚, Qโ‚‚, ..., Qโ‚™} - Position of each queen (row i, column xแตข)

Domains (D)

D(Qแตข) = {1, 2, ..., N} - Each queen can be in any column of its row

Constraints (C)

For all i โ‰  j:
โ€ข Column: xแตข โ‰  xโฑผ
โ€ข Diagonal: |xแตข - xโฑผ| โ‰  |i - j|

Algorithm

Backtracking Search:

  1. Place queen in next row
  2. Check constraints
  3. If valid, continue to next row
  4. If invalid, backtrack and try next column
  5. Repeat until solution found

Time: O(N!), Space: O(N)

Active Constraints

Real-time constraint checking shows which queens are attacking each other

Interactive Cryptarithmetic Solver

Assign unique digits (0-9) to letters to make the arithmetic equation valid.

Puzzle Goal
Objective: Assign unique digits (0-9) to letters so the arithmetic equation is valid.

Rules: Each letter = one digit, different letters = different digits, no leading zeros!
Current State
0
Letters Assigned
0
Violations
CSP Formulation
Variables (X)

X = {S, E, N, D, M, O, R, Y} - Each unique letter in the puzzle

Domains (D)

D(Xi) = {0, 1, 2, ..., 9} - Each letter can be assigned any digit

Constraints (C)

โ€ข All Different: Each letter gets unique digit
โ€ข No Leading Zero: First letters โ‰  0
โ€ข Arithmetic Valid: SEND + MORE = MONEY

Algorithm

Constraint Propagation + Backtracking:

  1. Assign digit to next letter
  2. Check all constraints (uniqueness, arithmetic)
  3. If valid, continue to next letter
  4. If invalid, backtrack and try next digit
  5. Use constraint propagation to reduce domains

Time: O(10^n), Space: O(n) where n = unique letters

Active Constraints

Real-time constraint checking for digit assignments and arithmetic validity

Interactive Sudoku Solver

Fill the 9ร—9 grid so each row, column, and 3ร—3 box contains digits 1-9.

Active Constraints

Real-time constraint checking for rows, columns, and 3ร—3 boxes

Sudoku Goal
Objective: Fill the 9ร—9 grid with digits 1-9 such that each row, column, and 3ร—3 box contains all digits exactly once.

Rules: No duplicates in rows, columns, or 3ร—3 boxes!
Current State
0
Cells Filled
0
Violations
CSP Formulation
Variables (X)

X = {Cโ‚โ‚, Cโ‚โ‚‚, ..., Cโ‚‰โ‚‰} - Each empty cell in the 9ร—9 grid

Domains (D)

D(Cแตขโฑผ) = {1, 2, 3, 4, 5, 6, 7, 8, 9} - Each cell can contain any digit

Constraints (C)

โ€ข Row: All different in each row
โ€ข Column: All different in each column
โ€ข Box: All different in each 3ร—3 box

Algorithm

Constraint Propagation + Backtracking:

  1. Find empty cell with fewest possibilities
  2. Try each valid digit (1-9)
  3. Check row, column, and box constraints
  4. If valid, move to next cell
  5. If invalid, backtrack and try next digit

Time: O(9^n), Space: O(1) where n = empty cells