Programming, problem solving, and algorithms

CPSC 203, 2025 W1

October 28, 2025

Announcements

Voronoi Diagrams Fin

Pointillism

The Seurat art piece depicting 1880's park goers.

A Sunday on La Grande Jatte

Voronoi Art

  • enqueue 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).

Desmos screenshot

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.

Graphs

The Internet, circa 2003

Graph \(G = (V, E)\)

  • \(V\):
  • \(E\):

Degree of a vertex:

A Conflict Graph

Graph \(G = (V, E)\)

  • \(V\):
  • \(E\):


Labels:


Neighbors:

What’s special about the vertex labels of this graph?

A Complete Graph

Graph \(G = (V, E)\)

  • \(V\):
  • \(E\):


A graph is complete if it contains all possible edges.


How many edges in a complete graph of n vertices?

Social Network

Graph \(G = (V, E)\)

  • \(V\):
  • \(E\):

Connected Graph:

 

Connected Component:

Rush Hour

Desmos screenshot

Graph \(G = (V, E)\)

  • \(V\):
  • \(E\):

Path:

State Machine

This graph can be used to quickly calculate whether a given number is divisible by 7.

Start at the circle node at the top. For each digit \(d\) in the given number, follow \(d\) blue (solid) edges in succession. As you move from one digit to the next, follow 1 red (dashed) edge. If you end up back at the circle node, your number is divisible by 7.


3703

The Stanford Bunny

Graph \(G = (V, E)\)

  • \(V\):
  • \(E\):

 

Planar Graph:

Graph Explorations

A graph \(G\) is defined by a set of vertices \(V\), and a set of edges \(E\).

Algorithm running time is typically described using the number of vertices, but execution usually depends on the number of edges, so we must understand the relationship between them.

How Many Edges?

Suppose I tell you a connected graph has 100 vertices. What do we know about the number of edges?

At least __________ edges because the graph is connected.

 

At most _________ edges which occurs when the graph is complete.

Handshaking

Suppose I tell you a graph has 28 vertices, each with degree 3. How many edges does it have?

The following are equivalent:

  • The number of neighbors of a vertex.
  • The number of incident edges of a vertex.
  • The degree of a vertex.

Degree Games

Suppose I drop my graph and it shatters into pieces. If I tell you the degree of each of the vertices, can you reconstruct the graph?

An algorithm for finding a graph, given a degree sequence, is described in the practice problems.

Havel-Hakimi

Pencil Puzzles

Trace each graph without lifting your pencil and without revisiting an edge.

 

Can all graphs be traced?

 

What characterizes those that cannot?