Programming, problem solving, and algorithms

CPSC 203, 2026 W1

November 5, 2026

Announcements

  • Practice Examlet 4 is available.
  • Lab 8 (memoization) and POTW 8 (mostly Sudoku validity) due Sunday.
  • Project 2 (Game of Life) released, due Monday November 16.
    • Time to get started!
  • Next week: No labs, no POTWs, no examlet.
    • Normal lecture on Thursday November 12.
  • Feeling overwhelmed? You are not alone!

State Space Graphs

Reminder: Rush Hour Game

Rush hour game state graph

Rush hour game board photo
  • \(V\): State of the board / position of all cars / configuration.
  • \(E\): Connect two configurations if there is a legal move between them.
  • Path is a sequence of legal moves, hopefully leading to a winning configuration.

Game Graphs: Tic-Tac-Toe

Two players take turns marking a cell in a 3 x 3 grid.

  • Traditionally, the marks are X and O, and X goes first.

How might we represent a state?

  • How many possible states are there?
  • What about symmetry?
  • Given a state, what moves are available?
tic-tac-toe game in a playground
photo by Przemysław Czopor

TicTacToe State Space Graph

XKCD’s map of optimal moves

Sudoku

Sudoku

  • Fill in the grid with the numbers 1 to 9.
    • No number may appear twice in a row, column, or 3x3 subgrid.

example starting board

[By Tim Stellmach - CC0, Link]

solution to example

Representing Sudoku

For an evolving system (typically Markovian):

  • A state is the current status or configuration of the system.
  • The state space is the set of all possible states.

How might you represent this Sudoku state?

State Space Graphs

  • A vertex is a (valid) state.
  • Edges connect states if there is a valid move between them.

How many neighbors does this vertex have?

Exploring the Sudoku Graph

  • Given a vertex, we need to choose a neighbour vertex to consider.
    • Choose an empty cell, build set of possible consistent values, choose one.
    • Choose an empty cell, choose a value, see if next state is valid.

Reminder: Flood Fill Pattern

Grid with each pixel numbered, pixel 63 highlighted blue, subset of pixels surrounded by a yellow curve.

  • enqueue a starting point
  • while the queue is not empty:
    • v = dequeue
    • for each valid neighbor w of v:
      • process w
      • enqueue w

Breadth First Search (BFS)

  • Explores a graph starting from a root node.
  • Explores all nodes at a specified depth (distance from the root) before exploring any at the next depth.

Other animations of BFS

Depth First Search (DFS)

  • Explores a graph starting from a root node.
  • Fully explores each subgraph before moving on to the next node at the current depth.

Other animations of DFS

Implementing DFS

  • Can be implemented via recursion.
  • We will use a stack / LIFO queue.

Pseudo-code stack functions:

  • Insert: push(item)
  • Remove: pop() \(\rightarrow\) item

Python deque (or list) functions:

  • Insert: append(item)
  • Remove: pop() \(\rightarrow\) item

Depth First Search Practice

  • push(start_vertex)
  • while the stack is not empty:
    • v = pop()
    • if v is not processed:
      • process v
      • for each valid neighbor w of v:
        • push(w)
A B C D E


Back to Sudoku

Sudoku Solver Algorithm

  • Search (DFS) through the states until you can’t go any farther.
    • If the board is complete, you win!
    • If the board is invalid, back up and try a new state in the most recent cell possible.

State Validity

Need to be able to check whether a state is valid.

Suppose we have a variable grid, representing the board, and we want to place a value called num, in position (x,y).

Need to check uniqueness in:

  • row
  • column
  • block

  • Which conditional checks a row?
  • Which conditional checks a column?
  • How shall we check a block?

Block Validity

Python tidbit: to query a region in a 2d matrix, define the bounds on the region and use in.

In the small example below, 2 in grid[0:2,0:2] returns True.

Defining Blocks

Given location \((x,y)\), determine the block in which it lives.

  • a region s:t contains \(x\) if \(s\leq x< t\).
  • Look for a pattern in the tables below for a \(4 \times 4\) grid.
  • Can we generalize to an \(r^2 \times r^2\) grid?
    • Hint: x//r is the integer part of x/r.

Which Cell Next?

We could choose next cell:

  • Randomly. We would then either:
    • Have to keep explicit track of which cells were still empty.
    • Repeatedly choose full cells and have to choose again.
  • Deterministically. We could either:
    • Build a double loop over 2D position \((x,y)\).
    • Build a single loop over 1D position and convert that to an \((x,y)\).

Linearizing the Cell Index

Write a function postup that takes an integer position p as input, and returns an (x,y) position. The grid is size \(\times\) size.

Demo

Today’s activity is already coded for you, but let’s look through the code.

Launch Class Activity: Sudoku in PrairieLearn.

Sudoku Closing Thoughts

How fast does the state space grow as board size grows?

size cells state space
2 4 x 4
3 9 x 9
4 16 x 16
5 25 x 25
\(r\) \(r^2\) x \(r^2\)

Mathematics of Sudoku

Could we be smarter about what values to consider?