Programming, problem solving, and algorithms

CPSC 203, 2025 W2

March 3, 2026

Announcements

  • EX3 this week!
  • Proj2 available, due 03/22, 11:59p.

Voronoi Diagrams Fin

Pointillism

The Seurat art piece depicting 1880's park goers.

A Sunday on La Grande Jatte

Voronoi Art

  • enqueue and process the centers
  • while the queue is not empty:
    • v = dequeue
    • for each valid neighbor w of v:
      • process w
      • enqueue w

Demo

PrairieLearn Activity

Analysis

How much work is done? Let \(n\) denote the size of the image, \(n = width \cdot height\)

  1. Read image:

  2. Choose centers:

  3. Build new image:

 

  1. Write out new image:

Analysis Continued

We evaluate algorithms by looking at how their running times change as a function of the input size (n).

  • Loosely, algorithm A is faster than algorithm B if A’s running time is eventually less than B’s.

Summary!

Our Voronoi journey taught us:

  1. Voronoi Diagrams — a beautiful structure from math and science, and we used it to make art!

  2. A simple iterative algorithm — for each pixel, check every center. It works… but it’s slow.

  3. A new data structure: the Queue — implemented with Python’s deque, it lines up data for orderly, first-in-first-out processing.

  4. A new algorithm: Flood Fill — start at every center simultaneously and grow outward. The queue keeps it all synchronized.

  5. Algorithm analysis — the naive approach costs \(n \cdot n\) work. Flood fill? Just \(n\). That’s the difference between 10 billion operations and 1 million.

Graphs: A new model for representing images

  • A Graph is a collection of vertices, and edges between them. They’re used as a general model for many problems.

  • In our images every ____________ is a vertex, and every ____________ is an edge.

Our fast algorithm for Voronoi Art mirrors a classic algorithm on graphs called Breadth First Search.

Next time…

GRAPHS!!