Graphs
Vertices and edges, degrees, walks and connectedness, Euler circuits, Hamiltonian cycles, and graph search
The topics of this chapter, and their order, follow the course calendar and Kameron Decker Harris’s Fall 2026 lecture notes on graphs. These come from those notes:
- a graph as a set of vertices and a set of edges, each edge with one or two endpoints;
- loops, parallel (multiple) edges, simple graphs and directed graphs;
- adjacent vertices, the degree of a vertex, with a loop counted twice, and the in-degree and out-degree in a directed graph;
- the handshake theorem, the fact that a graph has an even number of vertices of odd degree, and the sums of the in-degrees and out-degrees;
- complete graphs \(K_n\), cycle graphs \(C_n\), bipartite graphs and subgraphs;
- adjacency lists and adjacency matrices, with parallel edges counted in the matrix, and when a list takes less space than a matrix;
- walks (which the notes call paths), circuits and cycles, connected graphs and connected components, and strongly connected directed graphs;
- counting walks with the powers of the adjacency matrix, proved by induction on the length.
The pseudocode for breadth-first and depth-first search, and the use of a queue and of recursion in them, follow the course’s handout on graph extensions. Euler circuits and trails and Hamiltonian cycles follow the course calendar, with the definitions cited with them. Incident edges, neighborhoods and weakly connected directed graphs are added here. The proofs of the handshake theorem, of the even number of vertices of odd degree and of counting walks follow the lecture notes. The examples, the exercises, the Racket functions and the other proofs are new.
1 Graphs
2 Degree
Proof. Each edge has two ends. An edge with two endpoints has one end at each endpoint, and a loop has both ends at its one endpoint. We count the set of all ends of all edges in two ways.
Counted edge by edge, each edge gives two ends, so there are \(2 {\mathopen{}\left|E\right|\mathclose{}}\) ends.
Counted vertex by vertex, the ends at \(v\) are one end of each non-loop edge with endpoint \(v\) and two ends of each loop at \(v\). That is \(\deg(v)\) ends, by Definition 9, and each end is at exactly one vertex, so there are \(\sum_{v \in V} \deg(v)\) ends.
The two counts count the same set, so they are equal.
Proof. Let \(G = (V, E)\) be a graph, let \(O\) be the set of vertices of odd degree, and let \(N\) be the set of vertices of even degree. By Theorem 1,
\[ \sum_{v \in O} \deg(v) = 2 {\mathopen{}\left|E\right|\mathclose{}} - \sum_{v \in N} \deg(v). \]
The right side is an even number minus a sum of even numbers, so it is even (Definition: Even integer). The left side is a sum of \({\mathopen{}\left|O\right|\mathclose{}}\) odd numbers. Pair the odd numbers off two at a time. The sum of each pair is even. If \({\mathopen{}\left|O\right|\mathclose{}}\) were odd, one odd number would be left over, and the left side would be an even number plus an odd number, which is odd. So \({\mathopen{}\left|O\right|\mathclose{}}\) cannot be odd, and it is even.
Proof. Each edge starts at exactly one vertex, so sorting the edges by the vertex where they start puts each edge in exactly one group. The group of \(v\) has \(\deg^+(v)\) edges, so the sizes of the groups add up to \(\sum_{v \in V} \deg^+(v) = {\mathopen{}\left|E\right|\mathclose{}}\). Sorting the edges by the vertex where they end gives \(\sum_{v \in V} \deg^-(v) = {\mathopen{}\left|E\right|\mathclose{}}\) in the same way.
3 Special graphs and subgraphs
4 Representing graphs
5 Graphs in Racket
6 Walks, trails and paths
Proof. The lengths of the walks from \(u\) to \(v\), each plus 1, form a nonempty set of positive integers, so that set has a smallest element (Remark: Smallest elements), and subtracting 1 from it gives the smallest length of a walk from \(u\) to \(v\). Let \(v_0, e_1, v_1, \ldots, e_n, v_n\) be a walk from \(u\) to \(v\) of that smallest length \(n\).
Suppose, for contradiction (Definition: Proof by contradiction), that it is not a path. Then \(v_i = v_j\) for some \(i < j\). Leaving out the part between them gives the sequence
\[ v_0, e_1, \ldots, e_i, v_i, e_{j + 1}, v_{j + 1}, \ldots, e_n, v_n, \]
which is still a walk from \(u\) to \(v\), since the edge \(e_{j + 1}\) joins \(v_j = v_i\) to \(v_{j + 1}\) (in a directed graph, it goes from \(v_i\) to \(v_{j + 1}\)). When \(j = n\), the sequence ends at \(v_i = v_n = v\). Its length is \(n - (j - i) < n\), which contradicts the choice of \(n\). So the walk is a path.
Proof. Write \((M)_{ij}\) for the entry in row \(i\) and column \(j\) of a matrix \(M\), and \(a_{ij} = (A_G)_{ij}\). We use induction (Definition: Proof by induction) on \(r\).
Base case. A walk of length 1 from \(v_i\) to \(v_j\) is a sequence \(v_i, g, v_j\), where \(g\) is an edge with endpoints \(v_i\) and \(v_j\) (from \(v_i\) to \(v_j\), in a directed graph). There is one such walk for each such edge, so there are \(a_{ij}\) of them, by Definition 17. A loop \(g\) at \(v_i\) gives one walk \(v_i, g, v_i\), and adds 1 to \(a_{ii}\).
Inductive step. Suppose that \((A_G^r)_{ik}\) is the number of walks of length \(r\) from \(v_i\) to \(v_k\), for all \(i\) and \(k\). Every walk of length \(r + 1\) from \(v_i\) to \(v_j\) is a walk of length \(r\) from \(v_i\) to some vertex \(v_k\), followed by an edge from \(v_k\) to \(v_j\) and the vertex \(v_j\). Sort these walks by the vertex \(v_k\) just before the last edge. For each \(k\), a walk is made by choosing one of the \((A_G^r)_{ik}\) walks to \(v_k\) and one of the \(a_{kj}\) edges, and different choices give different walks, so there are \((A_G^r)_{ik} \, a_{kj}\) walks with that \(v_k\). Adding over \(k\), and using the definition of the matrix product (Definition: Matrix multiplication), the number of walks of length \(r + 1\) from \(v_i\) to \(v_j\) is
\[ \sum_{k = 1}^{n} (A_G^r)_{ik} \, a_{kj} = (A_G^r A_G)_{ij} = (A_G^{r + 1})_{ij}. \]
By induction, the statement holds for every \(r \ge 1\).
7 Connected graphs
8 Euler circuits and trails
Proof. The edge \(e_i\) has one end at \(v_{i - 1}\) and its other end at \(v_i\), both at the same vertex when \(e_i\) is a loop. The edges \(e_1, \ldots, e_n\) are all different, so no end is counted twice. Let \(k\) be the number of indices \(i\) with \(1 \le i \le n - 1\) and \(v_i = w\), and write \([P]\) for 1 if \(P\) is true and 0 if \(P\) is false. The ends at \(v_{i - 1}\), for \(i = 1, \ldots, n\), give \(k + [v_0 = w]\) ends at \(w\), and the ends at \(v_i\), for \(i = 1, \ldots, n\), give \(k + [v_n = w]\) ends at \(w\). So the count is
\[ 2k + [v_0 = w] + [v_n = w]. \]
If \(v_0 = v_n\), the last two terms are equal, so the count is even. If \(v_0 \ne v_n\), the last two terms add up to 1 when \(w\) is \(v_0\) or \(v_n\), and to 0 otherwise.
Proof. Suppose, for contradiction (Definition: Proof by contradiction), that \(v_n \ne v_0\). By Lemma 1, the edges of the trail have an odd number of ends at \(v_n\). The degree \(\deg(v_n)\) is the number of ends at \(v_n\) of all the edges of \(G\), and it is even. So some end at \(v_n\) belongs to an edge that is not in the trail. That edge has endpoint \(v_n\) and is not one of \(e_1, \ldots, e_n\), which contradicts the assumption that the trail cannot be extended.
Proof. Only if. Let \(C\) be an Euler circuit, and let \(w\) be a vertex. Every edge of \(G\) is an edge of \(C\), so the ends at \(w\) of the edges of \(C\) are all the ends at \(w\), and there are \(\deg(w)\) of them. By Lemma 1, that number is even.
If. Suppose every vertex has even degree. A trail has at most \({\mathopen{}\left|E\right|\mathclose{}}\) edges, since its edges are all different. So, given an edge and one of its endpoints, there is a trail of greatest length among the trails that start at that endpoint with that edge. That trail cannot be extended, so it is a circuit, by Lemma 2. So \(G\) has at least one circuit. Let \(C\) be a circuit \(v_0, e_1, v_1, \ldots, e_n, v_n\) of greatest length in \(G\). Suppose, for contradiction (Definition: Proof by contradiction), that some edge \(h\) is not in \(C\).
Some edge that is not in \(C\) has an endpoint on \(C\). Let \(y\) be an endpoint of \(h\). If \(y\) is a vertex of \(C\), take \(g = h\). Otherwise, since \(G\) is connected, there is a path (Definition 20) \(y = w_0, w_1, \ldots, w_m = v_0\), by Theorem 3. Let \(j\) be the smallest index with \(w_j\) a vertex of \(C\), so that \(j \ge 1\). Let \(g\) be the edge of the path between \(w_{j - 1}\) and \(w_j\). It is not in \(C\), since its endpoint \(w_{j - 1}\) is not a vertex of \(C\), and its endpoint \(w_j\) is a vertex of \(C\).
A circuit outside \(C\). Let \(H\) be the graph with the vertices of \(G\) and the edges of \(G\) that are not in \(C\). By Lemma 1, the edges of \(C\) have an even number of ends at each vertex, so every vertex has even degree in \(H\) too. The edge \(g\) is in \(H\) and has an endpoint \(v_k\) on \(C\). As in the first paragraph, a trail of greatest length in \(H\) that starts at \(v_k\) with the edge \(g\) is a circuit \(D\) in \(H\), from \(v_k\) to \(v_k\), by Lemma 2.
A longer circuit. Follow \(C\) from \(v_0\) to \(v_k\), then go around \(D\) back to \(v_k\), then follow the rest of \(C\) to \(v_n\). This is a walk from \(v_0\) to \(v_0\). No edge repeats in it, since \(C\) and \(D\) are trails with no common edge, so it is a circuit. Its length is the length of \(C\) plus the length of \(D\), which is greater than the length of \(C\). That contradicts the choice of \(C\). So \(C\) contains every edge, and it is an Euler circuit.
Proof. Let \(G'\) be the graph \(G\) with one new edge \(h\) with endpoints \(u\) and \(v\). It may be parallel (Definition 3) to an edge of \(G\). \(G'\) is connected, and has at least one edge. The edge \(h\) adds 1 to the degrees of \(u\) and \(v\), and changes no other degree. So the condition on the degrees in \(G\) says exactly that every vertex has even degree in \(G'\).
Only if. Let \(u = v_0, e_1, \ldots, e_n, v_n = v\) be an Euler trail of \(G\). Adding \(h\) at the end gives the circuit \(v_0, e_1, \ldots, e_n, v, h, u\) in \(G'\), which contains every edge of \(G'\). So every vertex has even degree in \(G'\), by Theorem 5.
If. Every vertex has even degree in \(G'\), so \(G'\) has an Euler circuit, by Theorem 5, and \(h\) is one of its edges. A circuit can start at any of its vertices: moving the part before a vertex to the end gives a circuit with the same edges. Reading a circuit backward gives a circuit with the same edges, so we may assume that the Euler circuit crosses \(h\) from \(v\) to \(u\). So \(G'\) has an Euler circuit that starts at \(v\) with the edge \(h\), as \(v, h, u, \ldots, v\). Leaving out \(v\) and \(h\) at the start gives a trail from \(u\) to \(v\) that contains every edge of \(G\).
9 Hamiltonian cycles
10 Breadth-first search
Proof. Write \(V\) for the vertex set of \(G\).
It stops. A vertex is added to \(Q\) only when it is marked, and a marked vertex is never marked again, so at most \({\mathopen{}\left|V\right|\mathclose{}}\) vertices are ever added to \(Q\). Each pass through the loop removes one of them, so there are at most \({\mathopen{}\left|V\right|\mathclose{}}\) passes.
Each label is the length of a walk. We show, by induction (Definition: Proof by induction) on the order in which the vertices are marked, that for each marked \(u\) there is a walk from \(s\) to \(u\) of length \(\texttt{dist}[u]\). For \(s\), the walk \(s\) has length \(0 = \texttt{dist}[s]\). Any other \(u\) is marked in the pass that removes some \(v\) from \(Q\), where \(v\) was marked earlier and \(u\) is in the list of \(v\). A walk from \(s\) to \(v\) of length \(\texttt{dist}[v]\), followed by the edge from \(v\) to \(u\), is a walk from \(s\) to \(u\) of length \(\texttt{dist}[v] + 1 = \texttt{dist}[u]\). So every marked vertex has a walk from \(s\), and \(\texttt{dist}[u] \ge \operatorname{dist}(s, u)\).
The labels leave \(Q\) in order. Let \(I\) be the statement that, if \(x_1, \ldots, x_k\) are the vertices in \(Q\) from front to back, then
\[ \texttt{dist}[x_1] \le \texttt{dist}[x_2] \le \cdots \le \texttt{dist}[x_k] \le \texttt{dist}[x_1] + 1. \]
\(I\) is true at the first test of the loop, when \(Q\) holds only \(s\). Suppose \(I\) is true at a test, and the pass removes \(v = x_1\). The vertices \(x_2, \ldots, x_k\) that remain have labels from \(\texttt{dist}[v]\) to \(\texttt{dist}[v] + 1\), in order, and every vertex added in the pass gets the label \(\texttt{dist}[v] + 1\). So the labels are still in order, and the last is at most \(\texttt{dist}[v] + 1\), which is at most the new first label plus 1. So \(I\) is a loop invariant, by Theorem: The loop invariant theorem. Every label in \(Q\) after the pass is at least \(\texttt{dist}[v]\), so the next vertex removed has a label at least \(\texttt{dist}[v]\). So the vertices leave \(Q\) with labels in nondecreasing order.
Each label is the distance. We show, by strong induction (Definition: Proof by strong induction) on \(k \ge 0\), that every vertex \(v\) with a walk from \(s\) and \(\operatorname{dist}(s, v) = k\) is marked with \(\texttt{dist}[v] \le k\). If \(k = 0\), then \(v = s\), which is marked with label 0. Let \(k \ge 1\), and let \(s = w_0, w_1, \ldots, w_k = v\) be a walk of length \(k\) from \(s\) to \(v\). Then \(w = w_{k - 1}\) has a walk of length \(k - 1\) from \(s\), so \(\operatorname{dist}(s, w) \le k - 1\). By the induction hypothesis, \(w\) is marked with \(\texttt{dist}[w] \le k - 1\). Then \(w\) was added to \(Q\), so some pass removed it, since the loop runs until \(Q\) is empty. That pass looks at \(v\), which is in the list of \(w\).
- If \(v\) is not marked yet, the pass marks it with \(\texttt{dist}[v] = \texttt{dist}[w] + 1 \le k\).
- If \(v\) is already marked, then, since \(v \ne s\), it was marked in an earlier pass that removed some \(x\), with \(\texttt{dist}[v] = \texttt{dist}[x] + 1\). Since \(x\) left \(Q\) before \(w\), \(\texttt{dist}[x] \le \texttt{dist}[w] \le k - 1\), so \(\texttt{dist}[v] \le k\).
So every vertex with a walk from \(s\) is marked, and \(\texttt{dist}[v] \le \operatorname{dist}(s, v) \le \texttt{dist}[v]\), by the second step.
11 Depth-first search
Proof. It stops. Each call dfs(G, u) is made only when \(u\) is not marked, and it marks \(u\) first, so there is at most one call for each vertex. Each call goes once through a finite list.
Only vertices with a walk from \(s\) are marked. The call dfs(G, u) is either the first call, with \(u = s\), or is made by a call dfs(G, v) with \(u\) in the list of \(v\), so that there is an edge from \(v\) to \(u\). By induction (Definition: Proof by induction) on the order of the calls, each marked vertex has a walk from \(s\): a walk from \(s\) to \(v\) followed by the edge from \(v\) to \(u\) is a walk from \(s\) to \(u\).
Every vertex with a walk from \(s\) is marked. Suppose, for contradiction (Definition: Proof by contradiction), that some vertex \(v\) with a walk \(s = w_0, w_1, \ldots, w_k = v\) is not marked. Let \(i\) be the smallest index with \(w_i\) not marked. Then \(i \ge 1\), since \(s\) is marked, and \(w_{i - 1}\) is marked, so the call to dfs on \(w_{i - 1}\) was made. That call looks at \(w_i\), which is in the list of \(w_{i - 1}\). Either \(w_i\) is marked already, or the call to dfs on \(w_i\) marks it. Either way \(w_i\) is marked, which contradicts the choice of \(i\).
Proof. Only if. If \(G\) is connected, every vertex has a walk from \(s\), so every vertex is marked, by Theorem 8.
If. Suppose every vertex is marked, and let \(u\) and \(v\) be vertices. By Theorem 8, there are walks from \(s\) to \(u\) and from \(s\) to \(v\). The edges of \(G\) have no direction, so the walk from \(s\) to \(u\), read backward, is a walk from \(u\) to \(s\). Following it with the walk from \(s\) to \(v\) gives a walk from \(u\) to \(v\). So \(G\) is connected.








