Programming, problem solving, and algorithms

CPSC 203, 2026 W1

November 12, 2026

Announcements

  • Project 2 (Game of Life) due Monday November 16.
  • No lab or POTW this week.
    • Next week: Lab 9 (dynamic programming) and POTW 9 (potpourri) due Sunday Nov 22.
  • Practice Examlet 4 is available.
    • Examlet 4 window is Thursday November 19 to Monday November 23.
  • Feeling overwhelmed? You are not alone!

Sudoku Continued

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?

Where to Next?

We have seen graphs and corresponding algorithms for:

Data Task Algorithm
Images / grids Voronoi diagrams / flood fill breadth first search
Music / evolving systems sampling / simulation Markov chains
Games path to solution depth first search

Next few lectures:

  • Look at geospatial data / maps,
  • For the purpose of path planning / route finding,
  • Using a dynamic programming algorithm.