2025-05-14
The goal of today’s lecture is to have you see graphs everywhere in the world.
Definition 1 (Graphs) A graph \(G = (V, E)\) is a pair of sets \((V, E)\), where \(V\) is a set of vertices and \(E\) is a set of pairs of vertices.
If \(E\) is a set of unordered pairs, the graph is called undirected, and if \(E\) is a set of ordered pairs, the graph is called directed.
Two vertices \(u, v \in V\) are adjacent if \(\{u, v\} \in E\)
The ability to model real-life problems as graph problems is extremely useful.
You will study algorithms to solve graph problems in CSC373.
Sometimes, modeling the problem is enough, as there are libraries that implement the algorithms for you.
One such library is the networkx (Python). We will see some examples...
Definition 2 (Matching) Let \(G = (V, E)\) be a graph. A matching \(M \subseteq E\) is a subset of edges that do not share any endpoints. I.e., every vertex appears in at most one edge in \(M\).
A matching is perfect if every vertex appears exactly once in the matching.
If each edge has a weight, then the weight of a matching is the sum of the weights of edges in \(M\).
Can you find a matching in the following graph?
Input: A graph \(G = (V, E)\).
Output: A matching \(M \subseteq E\).
Usually, we want \(|M|\) to be as large as possible. Sometimes, we also want to maximize or minimize the weight of the matching.
Here’s the setup: There are \(n\) students in a class that need to be matched with partners. Each student fills out a form to indicate a list of times they are available to work and their preference for working in-person or virtually.
Let \(a\), \(b\) be any two distinct students. \(a\) and \(b\) are incompatible if they don’t share any available times.
Otherwise, the compatibility score for \(a\) and \(b\) is the number of time slots in which they overlap in their availability, plus one if they also have the same preference for working in person or virtually.
Your task is to find a pairing with the following properties.
Definition 3 (Path) Let \(G = (V, E)\) be a graph. A sequence of distinct vertices \((v_1,...,v_n)\) is a path from \(v_1\) to \(v_n\) if for every \(i \in \{1,...,n-1\}\), \(v_i\) and \(v_{i+1}\) are adjacent. The length of the path is the number of edges in the path.
Input: A graph \(G = (V, E)\) and two vertices \(u, v \in V\).
Output: A path from \(u\) to \(v\) in \(G\). I.e. a sequence of vertices \((v_1, v_2,...,v_n)\) where \(v_1 = u\) and \(v_n = v\).
Typically, we want to find the path with the smallest length. If each edge has a weight, we may also want to find the path with the smallest total weight.
Here are the rules:
Let’s play!
Let \(G = (V, E)\). Then, given a random Wikipedia page \(u\), the shortest path from \(u\) to \(\mathrm{University of Toronto}\) is the optimal solution for the Wikipedia Game.
Definition 4 (Cycle) Let \(G = (V, E)\) be a graph
A sequence of vertices \((v_1,...,v_n)\), is a cycle if \((v_1,...,v_{n-1})\) is a path, \(v_1 = v_n\), and \(\{v_{n-1}, v_n\} \in E\).
A cycle is called Hamiltonian if every vertex in the graph appears in the cycle exactly once (except for the start/end vertex, which appears twice).
The minimum length of a cycle is \(3\) (i.e., \(n \geq 4\)).
Input: A graph \(G = (V, E)\) and a starting vertex \(h\)
Output: A Hamiltonian cycle in \(G\) starting from \(h\) that minimizes the total edge weights.
Imagine you’re a door-to-door strawberry salesperson. Let \(H\) be the set of homes in your neighborhood. You live at \(h \in H\).
Definition 5 (Connected, Acyclic, Tree) Let \(G = (V, E)\) be any graph.
If \(G\) has many edges, it’s more likely to be connected, but also more likely to have a cycle.
If \(G\) has fewer edges, it’s more likely to be acyclic but less likely to be connected.
Since trees are both connected and acyclic, trees represent a perfect compromise. However, as we will see on the next slide, any addition or subtraction of an edge will destroy the balance.
Trees are minimally connected, meaning that it is connected but removing any edge causes the tree to be disconnected.
Trees are maximally acyclic, meaning that there is no cycle, but adding any edge creates a cycle.
Tree: connected and acyclic.
WTS: Removing any edge causes the tree to be disconnected.
Let \(G = (V, E)\) be a tree. Suppose \(\{u, v\}\) is an edge in \(G\). Let \(G' = (V, E \setminus\{\{u, v\}\})\) be the graph with \(\{u, v\}\) removed. We’ll show that \(G'\) is disconnected - in particular, there is no path from \(u\) to \(v\).
By contradiction, if there was a path \(P = (u = v_1,v_2,...,v_n=v)\) from \(u\) to \(v\) in \(G'\), then the \(C = (u = v_1,v_2,...,v_n=v, u)\) is a cycle in \(G\). However, \(G\) is acyclic, so this is a contradiction.
Tree: connected and acyclic.
WTS: Adding any edge creates a cycle
Let \(G = (V, E)\) be a tree. Suppose \(\{u, v\}\) is an edge not in \(G\). Let \(G' = (V, E \cup\{\{u, v\}\})\) be the graph with the edge \(\{u, v\}\) added. We’ll show that \(G'\) has a cycle.
Since \(G\) is connected, there is some path \(P = (u=v_1,...,v_n = v)\) from \(u\) to \(v\) in \(G\). Then \(C = (u=v_1,...,v_n=v, u)\) is a cycle in \(G'\).
Let \(G\) be a graph.
If \(G\) is minimally connected. Then \(G\) is a tree.
If \(G\) is maximally acyclic. Then \(G\) is a tree.
Are these also true? Yes!
Input: A connected, weighted graph \(G = (V, E)\).
Output: A graph \(T = (V, E')\), where \(E' \subseteq E\), such that \(T\) is a tree that minimizes the total edge weights.
Here are some references for some algorithms in case you are curious.
CSC236 Summer 2025