Programming, problem solving, and algorithms

CPSC 203, 2026 W1

October 22, 2026

Announcements

  • Practice examlet 3 available.
  • Book your slot for examlet 3.
  • Lab 6 (stacks) and POTW 6 (summations and stacks) due Sunday.
  • Project 1 due Monday (~96 hours!).

Voronoi Diagrams (continued)

Algorithmic Art Reminder

A modern photographic recreation of Seurat's painting.

The Voronoi version of the photograph.

  1. Select a subset of points from the original image as centers
  2. Build the Voronoi diagram for those centers.
  3. To visualize the Voronoi regions, fill each region in a new image with the same colour as the center of that region.

Aside: Some Technical Bits in the Loop-Based Solution

Look back in last lecture’s Class Activity: Voronoi Art at voronoi_loop_answer_nb.py.

Analysis (Loop-based Version)

How much work is done?

  1. Read image:
  2. Choose centers:
  3. Build new image:




  4. Write out new image:

Size of the image: \(n\)
(\(n =\) width \(\times\) height)

Density of centers: \(d\)

Number of centers: \(dn\)

Can we do better?

A large grid with 3 colored centers.

Data Structure: Queue

To implement the fill we use a datastructure called a queue.

Penguins in a line jumping off an ice flow into the water.

Operations on a queue:

  • enqueue(d): places data d at the “end” / “bottom” of the queue.
  • dequeue(): removes and returns the element at the “start” / “top” of the queue.

Pattern: Flood Fill

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

Queue for Voronoi Art

Grid with each pixel numbered, pixel 63 highlighted blue, pixel 21 highlighted yellow.

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

Designing a Solution

We’ll use collections.deque as our queue.

  1. What should we put on the queue?
  2. What deque methods will we use?
  3. How can we check if a deque is empty?
  4. What are the “neighbors” of pixel (x,y)?
  5. What would be an invalid neighbor?

Aside: Queues are also available from Python’s queue library.

Demo

Open today’s activity. We’ll implement the queue-based version of the algorithm.

Aside: Measuring Distance and the Choice of Neighbours

How far away are the diagonal pixels?

regular distance

four neighbours

eight neighbours

Analysis (Queue-based Version)

How much work is done?

  1. Read image: \(O(n)\)
  2. Choose centers: \(O(dn)\)
  3. Build new image:




  4. Write out new image: \(O(n)\)

Size of the image: \(n\)
(\(n =\) width \(\times\) height)

Density of centers: \(d\)

Number of centers: \(dn\)