Programming, problem solving, and algorithms

CPSC 203, 2026 W1

October 29, 2026

Announcements

  • Examlet 3 today through Monday.
  • Lab 7 (in-place list manipulation) and POTW 7 (numpy arrays) due Sunday.
  • Project 2 (Game of Life) released, due Monday November 16.
    • One week grace period for projects at 50% credit.
  • Final exam:
    • Booking opens Tuesday December 1.
    • Slots available Tuesday December 15 through Saturday December 19 (inclusive).
  • Feeling overwhelmed? You are not alone!

Graphs

Graphs: A Model for Representing Relationships between Entities

  • Nodes or vertices represent entities.
  • Edges denote a relationship.
  • Nodes are neighbours if they share an edge.

Graphs: Representation for Humans

A graph is a pair of lists \(G = (V,E)\).

  • The nodes / vertices \(V = \{ v_i \}\).
  • The edges are pairs of nodes \(E = \{ (v_i, v_j) \}\).

Surprise: We Have Already Been Using a Graph

Consider the neighbourhood of a pixel in our Voronoi algorithm.

  • Each pixel is a node in a graph.
  • Edges of central pixel (denoted by “+”) shown in red.
  • Edges of all other pixels shown in black.

Our original visualization

4 neighbours

8 neighbours

Example Graphs

The Internet, circa 2003

In this case:

  • \(V\): The routers which communicate between local networks.
  • \(E\): Identify which routers are connected together.

The degree of a vertex is the number of edges to which it connects.

A Conflict Graph

The nodes of this graph are labelled by their shape and colour.

  • \(V\): Courses at the university.
  • \(E\): Connect courses with a common student enrolled in both.

Labels can be attached to nodes or edges to capture features of the underlying problem.

  • What is special about this labelling?

A Weighted Graph

  • \(V\): Major characters in The Office.
  • \(E\): Conversations. Weight denotes the number of conversations.

A graph is complete if it contains all possible edges.

  • Is this graph complete?

How many edges in a complete graph of \(n\) vertices?

Rush Hour Game

Desmos screenshot

  • \(V\): State of the board / position of all cars / configuration.
  • \(E\): Connect two configurations if there is a legal move between them.

A path is a sequence of vertices with an edge between every adjacent pair of vertices.

Social Network (Cinda’s Facebook in 2012)

  • \(V\): Cinda’s friends.
  • \(E\): Connect friends who are friends of each other.

A graph is connected if there is a path between any pair of vertices.

A connected component is a subgraph which is connected.

State Machine: Is a Number Divisible by Seven?

  • Start at the circle node at the top.
  • Read the number from left to right.
  • 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.

Consider the number 3703.

In a directed graph, edges connect nodes only in one direction.

The Stanford Bunny

  • \(V\): Points on the surface of the bunny.
  • \(E\): Connect vertices into triangles.

In a planar graph, the vertices can be placed on the plane and the graph drawn without any crossing edges.

Complexity of Graph Algorithms

Graph Explorations

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

  • We will often denote the size of these sets by \(|V|\) and \(|E|\).

Algorithm complexity is often described in terms of the number of vertices.

  • Actual execution time usually depends on the number of edges.
  • So how are \(|V|\) and \(|E|\) related?

Demo

Open today’s activity. You’ll work through some graph size calculations.