Raw

Lecture 12. Special graphs

12.0 Overview: why “special” graphs deserve their own chapter

Lecture 10 introduced graphs in the abstract — vertices, edges, degrees, walks, paths, cycles, connectivity, and the handshake identity — and Lecture 11 turned those objects into algorithms: greedy colouring, breadth-first and depth-first search, Dijkstra’s shortest paths, and the minimum-spanning-tree methods of Kruskal and Prim. A general graph, however, is almost featureless: knowing only that “some vertices are joined by some edges” tells us very little, and most interesting questions about arbitrary graphs are computationally hard.

This lecture studies special families of graphs — graphs carrying extra structure. Structure is exactly what turns a shapeless object into a theorem-rich one: the moment we insist that a graph be acyclic, or that its edges alternate between two colour classes, or that every vertex have even degree, a cascade of clean, provable consequences follows, and efficient algorithms become possible.

The centre of gravity is the tree: a connected acyclic graph. Trees are the single most important non-trivial structure in computer science. They organise:

  • file systems — a root directory, folders, files, and the unique path /home/user/notes.txt from root to any file;
  • expression and parse trees — how a compiler represents (a + b) * c and how a calculator evaluates reverse Polish notation;
  • search structures — binary search trees, AVL and red–black trees, B-trees, heaps — where the height of the tree is the cost of an operation;
  • prefix codes — Huffman coding builds an optimal full binary tree whose leaves are the symbols of an alphabet;
  • decision trees, game trees, syntax trees, phylogenetic trees, and the recursion trees we draw to analyse divide-and-conquer algorithms.

We develop trees with full rigour — their several equivalent characterizations, the leaf-counting lemmas, rooted-tree vocabulary, mm-ary and binary trees, traversals, and spanning trees — and then survey four further families that recur throughout the course: bipartite, regular, Eulerian, and Hamiltonian graphs. Two of the deepest theorems in elementary graph theory live here, and we prove both in full: the characterization of Eulerian graphs by even degrees (Euler, 1736 — the birth of graph theory), and the characterization of bipartite graphs as the graphs with no odd cycle.

Throughout, G=(V,E)G=(V,E) is a finite undirected graph with n=Vn=|V| vertices and m=Em=|E| edges, simple (no loops or parallel edges) unless we explicitly allow a multigraph. We lean constantly on the handshake identity of Lecture 10,

vVdeg(v)=2E=2m,\sum_{v\in V}\deg(v)=2\,|E| = 2m,

and on its corollary that the number of odd-degree vertices is even.


12.1 Trees: intuition and definition

Imagine wiring up nn computers into one network as cheaply as possible. You need the network connected (every machine can reach every other), and you do not want to pay for redundant cable, so you avoid cycles (a cycle would let you delete a cable and stay connected). The cheapest connected layout is therefore a connected graph with no cycles — a tree. This tension, “connected but with nothing to spare,” is the essence of treeness, and the characterization theorem below makes it precise in four different languages.

Definition (Tree). A tree is a connected undirected graph that contains no cycle. Equivalently, a connected acyclic graph.

A tree on seven vertices: connected, acyclic, with |E| = |V| − 1

Two words carry the definition. Connected means every pair of vertices is joined by at least one path (Lecture 10). Acyclic means the graph contains no cycle, i.e. no simple closed walk of length 3\ge 3 (in a simple graph the shortest possible cycle is a triangle). A single vertex with no edges is a tree (vacuously connected and acyclic); so is a single edge.

Remark (why “no cycle of length 3\ge 3”). In a simple graph there are no loops (length-1 closed walks) and no repeated edges (length-2 closed walks), so the smallest possible cycle already has length 33. When we later allow a multigraph (Königsberg), a pair of parallel edges does count as a cycle of length 22.

The definitional picture is deceptively simple. What makes trees powerful is that “connected + acyclic” is only one of many equivalent descriptions.


12.2 Leaves and the two-leaf theorem

Definition (Leaf). A leaf (or pendant vertex) of a graph is a vertex of degree 11. A non-leaf, non-isolated vertex is an internal vertex.

Leaves are the “loose ends” of a tree, and their existence drives almost every proof about trees (they give us something to peel off and induct on). Before the big theorem we isolate the key structural fact.

Theorem 12.1 (Two-leaf theorem). Every tree with n2n \ge 2 vertices has at least two leaves.

Proof. Let TT be a tree with n2n \ge 2 vertices. Since TT is finite, among all paths in TT we may choose one of maximum length; call it

P:v0,v1,,vk.P: \quad v_0,\, v_1,\, \dots,\, v_k .

Because TT is connected and has at least two vertices it has at least one edge, so k1k \ge 1 and the endpoints v0vkv_0 \ne v_k are distinct. We claim both endpoints are leaves.

Consider v0v_0. It is adjacent to v1v_1, so deg(v0)1\deg(v_0) \ge 1. Suppose, for contradiction, that v0v_0 had another neighbour wv1w \ne v_1. There are two cases.

  1. If ww does not lie on PP, then w,v0,v1,,vkw, v_0, v_1, \dots, v_k is a path of length k+1>kk+1 > k, contradicting the maximality of PP.
  2. If w=viw = v_i for some i2i \ge 2 (it cannot be v1v_1, since wv1w \ne v_1), then v0,v1,,vi,v0v_0, v_1, \dots, v_i, v_0 is a cycle (its length is i+13i+1 \ge 3), contradicting that TT is acyclic.

Both cases are impossible, so v0v_0 has no neighbour other than v1v_1; that is, deg(v0)=1\deg(v_0) = 1 and v0v_0 is a leaf. By the identical argument applied to the other end, vkv_k is a leaf. Since v0vkv_0 \ne v_k, the tree has at least two leaves. \blacksquare

Remark. The proof extracts more than the statement: the endpoints of any longest path in a tree are leaves. This “longest path” trick reappears when studying the diameter and centre of a tree.

Common pitfall. The hypothesis n2n\ge 2 is needed. The one-vertex tree has a single vertex of degree 00 — that is not a leaf (leaves have degree exactly 11), and the theorem indeed does not claim two leaves there.


12.3 The tree characterization theorem

We first record the exact edge count, then prove the master equivalence. The edge count is proved by peeling off a leaf and inducting — precisely why Theorem 12.1 came first.

Lemma 12.2 (Edge count). A tree with nn vertices has exactly n1n-1 edges.

Proof. Induction on nn. Base n=1n=1: the tree is a single vertex with 0=110 = 1-1 edges. Inductive step: let n2n \ge 2 and assume the claim for all trees with fewer than nn vertices. By Theorem 12.1, TT has a leaf vv with unique neighbour uu. Form T=TvT' = T - v by deleting vv and its one incident edge uvuv.

  • TT' is acyclic: deleting a vertex and edges cannot create a cycle.
  • TT' is connected: take any two vertices x,yvx, y \ne v. In TT there is a path from xx to yy. Because degT(v)=1\deg_T(v) = 1, the vertex vv can appear on a simple path only as an endpoint; since x,yvx, y \ne v, the xxyy path does not pass through vv and therefore survives intact in TT'.

Hence TT' is a tree on n1n-1 vertices, so by the inductive hypothesis it has (n1)1=n2(n-1)-1 = n-2 edges. Restoring vv and the edge uvuv adds exactly one edge, so TT has n1n-1 edges. \blacksquare

Corollary 12.3. In a tree with nn vertices, vVdeg(v)=2(n1)\displaystyle\sum_{v\in V}\deg(v) = 2(n-1). Consequently, if n2n \ge 2, not every vertex can have degree 2\ge 2 (that would force the degree sum to be 2n>2(n1)\ge 2n > 2(n-1)), re-proving that a tree has at least one — in fact, by Theorem 12.1, at least two — leaves.

We can now state and prove the theorem that makes trees so pleasant to work with.

Theorem 12.4 (Characterization of trees). Let TT be an undirected graph with nn vertices. The following are equivalent.

  1. TT is a tree (connected and acyclic).
  2. TT is connected and has exactly n1n-1 edges.
  3. TT is acyclic and has exactly n1n-1 edges.
  4. Between every pair of vertices there is a unique simple path.
  5. TT is minimally connected: connected, but deleting any one edge disconnects it.
  6. TT is maximally acyclic: acyclic, but adding any one edge between non-adjacent vertices creates exactly one cycle.

The proof is organised as a cycle (1)(2)(3)(1)(1)\Rightarrow(2)\Rightarrow(3)\Rightarrow(1) establishing the equivalence of the first three, then (1)(4)(1)\Leftrightarrow(4), (1)(5)(1)\Leftrightarrow(5), and (1)(6)(1)\Leftrightarrow(6). We use one small fact repeatedly, stated separately.

Fact 12.5 (Cycle edges are not bridges). If an edge e=uve=uv lies on a cycle of a graph GG, then GeG-e (delete the edge, keep the vertices) is still connected “through” that pair: any walk using ee can be rerouted along the rest of the cycle, which is a uuvv path avoiding ee. Hence removing a cycle edge never disconnects the graph.

Proof of Theorem 12.4.

(1)(2)(1)\Rightarrow(2). Immediate: a tree is connected by definition and has exactly n1n-1 edges by Lemma 12.2.

(2)(3)(2)\Rightarrow(3). Assume TT is connected with n1n-1 edges; we must show it is acyclic. Suppose not — suppose TT contains a cycle. By Fact 12.5 we may delete an edge lying on that cycle and remain connected. Repeat: each deletion removes at least one cycle while preserving connectivity. Because TT is finite this process halts, at a graph TT^\star that is connected and acyclic, i.e. a tree, on the same nn vertices. By Lemma 12.2, TT^\star has n1n-1 edges. But we deleted at least one edge, so TT^\star has at most n2<n1n-2 < n-1 edges — a contradiction. Hence TT was acyclic all along, and being also connected with n1n-1 edges it satisfies (3).

(3)(1)(3)\Rightarrow(1). Assume TT is acyclic with n1n-1 edges; we must show it is connected. Let TT have connected components C1,,CcC_1, \dots, C_c with n1,,ncn_1, \dots, n_c vertices respectively, where ini=n\sum_i n_i = n. Each component is connected (by definition of component) and acyclic (a subgraph of an acyclic graph), hence a tree, so by Lemma 12.2 component CiC_i has ni1n_i - 1 edges. Summing,

m=i=1c(ni1)=(ini)c=nc.m = \sum_{i=1}^{c}(n_i - 1) = \Big(\sum_i n_i\Big) - c = n - c.

But m=n1m = n-1 by hypothesis, so nc=n1n - c = n - 1, giving c=1c = 1. Thus TT has a single component; it is connected, and being also acyclic, it is a tree.

This closes the cycle, so (1), (2), (3) are equivalent.

(1)(4)(1)\Rightarrow(4). Let TT be a tree. Connectivity gives at least one simple path between any xx and yy; we prove there is at most one. Suppose xx and yy were joined by two distinct simple paths PP and QQ. Reading both from xx, they begin at the common vertex xx; let aa be the last vertex up to which they agree (so the two paths share the initial segment ending at aa but take different edges immediately after aa). Both paths eventually reach yy. Follow PP onward from aa and let bb be the first vertex strictly after aa on PP that also lies on QQ beyond aa; such a bb exists because yy qualifies. Now the segment of PP from aa to bb and the segment of QQ from aa to bb

  • share only their endpoints aa and bb (by the minimal choice of bb, no interior vertex of the PP-segment lies on QQ beyond aa), and
  • differ in their first edge out of aa (by the choice of aa),

so together they form a closed walk that repeats no vertex except a=ba=b at the ends — a cycle. (Its length is 3\ge 3: the two aabb segments are distinct and, in a simple graph, at most one of them can be the single edge abab, so their total length is at least 33.) This contradicts acyclicity. Hence the path is unique.

(4)(1)(4)\Rightarrow(1). Suppose every pair of vertices is joined by a unique simple path. Existence of a path for every pair means TT is connected. And TT is acyclic: a cycle through two of its vertices u,vu, v would provide two distinct uuvv paths (its two arcs), contradicting uniqueness. So TT is a tree, and (1)(4)(1)\Leftrightarrow(4).

(1)(5)(1)\Rightarrow(5). Let TT be a tree; it is connected. Take any edge e=uve=uv. By (4)(4) the only simple uuvv path is the single edge ee itself. Removing ee therefore leaves no uuvv path (a surviving path plus ee would be a cycle, impossible), so TeT-e is disconnected. Thus TT is minimally connected.

(5)(1)(5)\Rightarrow(1). Suppose TT is minimally connected. It is connected by hypothesis. If it had a cycle, Fact 12.5 lets us delete a cycle edge and stay connected, contradicting minimality. So TT is acyclic — a tree. Hence (1)(5)(1)\Leftrightarrow(5).

(1)(6)(1)\Rightarrow(6). Let TT be a tree; it is acyclic. Let u,vu,v be non-adjacent, and add the edge e=uve=uv. By (4)(4) there is a unique simple uuvv path PP in TT; then P+eP + e is a cycle. It is the only cycle in T+eT+e: every cycle of T+eT+e must use ee (since TT itself is acyclic), and a cycle through ee consists of ee together with a simple uuvv path in TT — of which there is exactly one. So adding ee creates exactly one cycle; TT is maximally acyclic.

(6)(1)(6)\Rightarrow(1). Suppose TT is maximally acyclic; it is acyclic. If TT were disconnected, pick u,vu, v in different components. They are non-adjacent, and adding uvuv creates no cycle (a cycle would require a pre-existing uuvv path, but they lie in different components). That contradicts maximal acyclicity, which demands that adding an edge create a cycle. So TT is connected — a tree, and (1)(6)(1)\Leftrightarrow(6).

All six statements are equivalent. \blacksquare

Worked example. Take vertices {1,2,3,4,5,6}\{1,2,3,4,5,6\} with edges {12,13,34,35,56}\{1\text–2,\, 1\text–3,\, 3\text–4,\, 3\text–5,\, 5\text–6\}.

        2
        |
        1
        |
        3
       / \
      4   5
          |
          6

There are n=6n=6 vertices and m=5=n1m=5 = n-1 edges; the graph is connected and has no cycle, so it is a tree (any one of characterizations (1)–(3) certifies this). The unique path from 22 to 66 is 213562\text–1\text–3\text–5\text–6, illustrating (4). Deleting the edge 353\text–5 splits it into {1,2,3,4}\{1,2,3,4\} and {5,6}\{5,6\}, confirming minimal connectivity (5). The leaves are 2,4,62,4,6 (degree 11); vertex 33 has degree 33. Degree sum =1+2+3+1+2+1=10=2(n1)= 1+2+3+1+2+1 = 10 = 2(n-1), matching Corollary 12.3.

Common pitfall (the count n1n-1 is not by itself sufficient). Having n1n-1 edges does not make a graph a tree. The 4-vertex graph consisting of a triangle {12,23,31}\{1\text–2, 2\text–3, 3\text–1\} plus an isolated vertex 44 has n=4n=4 and m=3=n1m=3 = n-1, yet it is neither connected nor acyclic. Characterization (2) requires both n1n-1 edges and connectivity; (3) requires both n1n-1 edges and acyclicity. Drop either extra hypothesis and the statement fails.


12.4 Forests

Definition (Forest). A forest is an acyclic undirected graph (not necessarily connected). Equivalently, a disjoint union of trees: each connected component of a forest is a tree.

Proposition 12.6. A forest with nn vertices and cc connected components has exactly ncn - c edges.

Proof. Let the components be trees on n1,,ncn_1, \dots, n_c vertices with ini=n\sum_i n_i = n. By Lemma 12.2 component ii has ni1n_i - 1 edges, so the total is i(ni1)=nc\sum_i(n_i - 1) = n - c. \blacksquare

A tree is the special case c=1c = 1, recovering m=n1m = n-1. Turning the equation around, c=nmc = n - m: in a forest, the number of components equals vertices minus edges. This is the tiny counting engine behind the union–find data structure used by Kruskal’s MST algorithm (Lecture 11): each accepted edge merges two trees into one, dropping the component count by exactly one.

Example. A graph on 88 vertices made of one 33-vertex tree, one 44-vertex tree, and one isolated vertex is a forest with c=3c=3 components; it has 83=58 - 3 = 5 edges. Adding an edge that joins two different components lowers cc to 22 and keeps it a forest; adding an edge inside a single component would create a cycle (by the maximal-acyclic property, Theorem 12.4(6)).


12.5 Rooted trees and terminology

Un-rooted trees are symmetric — no vertex is special. Computing almost always wants a distinguished starting point, so we single one out.

Definition (Rooted tree). A rooted tree is a tree in which one vertex is designated the root rr. Choosing a root orients every edge conceptually away from rr and imposes a parent–child hierarchy determined by the unique paths of Theorem 12.4(4).

A rooted tree drawn by levels: the root at depth 0, internal vertices below it, and leaves at the bottom

Fix a rooted tree with root rr. For a vertex vv:

  • Parent — the vertex adjacent to vv on the unique path from vv to rr; the root has no parent, every other vertex has exactly one.
  • Child — any vertex whose parent is vv.
  • Siblings — vertices sharing the same parent.
  • Ancestors — the vertices on the path from vv up to rr, excluding vv; descendants — the reverse relation (vv’s descendants are the vertices having vv as an ancestor).
  • Leaf — a vertex with no children; internal vertex — a vertex with at least one child. (In a rooted tree “leaf” means childless, which coincides with degree 11 for all non-root vertices, and can also apply to the root if it has a single child in the un-rooted sense — a mild abuse we tolerate.)
  • Subtree rooted at vvvv together with all its descendants and the edges among them; itself a rooted tree with root vv.
  • Depth (level) of vv — the length (number of edges) of the path from rr to vv; the root has depth 00.
  • Height of the tree — the maximum depth over all vertices, i.e. the length of the longest root-to-leaf path.
Depth 0:            A            (root)
                   / \
Depth 1:          B   C
                 / \
Depth 2:        D   E

Example. In the rooted tree drawn above (root AA; children B,CB, C of AA; children D,ED, E of BB): AA is the root at depth 00; B,CB, C are at depth 11 and are siblings; D,ED, E at depth 22 are siblings and children of BB; AA is an ancestor of DD, and DD is a descendant of AA. Leaves are C,D,EC, D, E; internal vertices are A,BA, B. The subtree rooted at BB is {B,D,E}\{B, D, E\}. The height of the tree is 22.

CS connection (file systems). A Unix-like file system is a rooted tree: the root directory / is rr, directories are internal vertices, files are leaves, and the unique path guaranteed by Theorem 12.4(4) is exactly the absolute pathname /home/user/notes.txt. Path uniqueness is what makes a pathname a well-defined address. (Symbolic links deliberately break the tree structure, turning it into a general graph — which is why link loops can hang naïve directory traversals.)


12.6 mm-ary and binary trees; counting and height bounds

Definition. An mm-ary tree is a rooted tree in which every internal vertex has at most mm children. It is a full mm-ary tree if every internal vertex has exactly mm children. A binary tree is the case m=2m = 2, with each child distinguished as the left child or the right child — so a binary tree is an ordered rooted tree, and the empty left/right slot matters.

A full binary tree of height 2 with four leaves (every internal vertex has exactly two children)

Two further shape adjectives (terminology varies between textbooks, so we pin them down):

  • Perfect (a.k.a. complete) mm-ary tree — full, and all leaves at the same depth.
  • Balanced tree — every leaf is at depth hh or h1h-1, where hh is the height; the tree is “as short as possible” for its number of leaves. AVL trees and red–black trees are binary search trees that self-balance after each insertion or deletion, keeping the height Θ(logn)\Theta(\log n).

Counting vertices and leaves in a full mm-ary tree

Theorem 12.7. A full mm-ary tree with ii internal vertices has

n=mi+1vertices and=(m1)i+1leaves.n = m\,i + 1 \quad\text{vertices and}\quad \ell = (m-1)\,i + 1 \quad\text{leaves}.

Proof. Every vertex except the root is the child of exactly one internal vertex, and each of the ii internal vertices has exactly mm children (fullness). Counting children, the number of non-root vertices is mim\,i; adding the root gives n=mi+1n = m\,i + 1. The leaves are the vertices that are not internal, so =ni=(mi+1)i=(m1)i+1\ell = n - i = (m\,i + 1) - i = (m-1)\,i + 1. \blacksquare

For a full binary tree (m=2m = 2) this reads n=2i+1n = 2i + 1 and =i+1\ell = i + 1: the number of leaves is exactly one more than the number of internal vertices.

Example. A full binary tree with i=10i = 10 internal vertices has =11\ell = 11 leaves and n=21n = 21 vertices; check i+=10+11=21=ni + \ell = 10 + 11 = 21 = n. \checkmark

CS connection (Huffman coding). An optimal binary prefix code corresponds to a full binary tree: the symbols sit at the leaves, and the codeword for a symbol is the sequence of left/right (0/1) turns from the root. “Prefix-free” means no codeword is a prefix of another, which holds precisely because no leaf is an ancestor of another leaf. Huffman’s greedy algorithm repeatedly merges the two least-frequent subtrees, and =i+1\ell = i + 1 guarantees the arithmetic of the merge tree works out.

The height–leaf bound

Theorem 12.8 (Leaf bound). An mm-ary tree of height hh has at most mhm^{h} leaves.

Proof. Strong induction on hh. Base h=0h = 0: the tree is a single vertex (the root), which is a leaf, so it has 1=m01 = m^0 leaf. Step: let h1h \ge 1. The root has at most mm children; deleting the root splits the tree into at most mm subtrees, each of height at most h1h-1, hence each with at most mh1m^{h-1} leaves by the inductive hypothesis. Since h1h \ge 1 the root is internal, so every leaf of the whole tree is a leaf of one of these subtrees. Therefore the number of leaves is at most mmh1=mhm \cdot m^{h-1} = m^{h}. \blacksquare

Corollary 12.9 (Height lower bound). An mm-ary tree with \ell leaves has height hlogmh \ge \lceil \log_m \ell \rceil. For binary trees, hlog2h \ge \lceil \log_2 \ell \rceil.

Proof. From mh\ell \le m^{h} take base-mm logarithms: logmh\log_m \ell \le h. As hh is an integer, hlogmh \ge \lceil \log_m \ell \rceil. \blacksquare

Counting vertices level by level (at most mkm^k vertices at depth kk, proved by the same induction) gives the companion bound: an mm-ary tree of height hh has at most mh+11m1\frac{m^{h+1}-1}{m-1} vertices, and for binary trees at most 2h+112^{h+1}-1.

Example (perfect binary tree). A perfect binary tree of height 33 has levels 0,1,2,30,1,2,3 with 1,2,4,81,2,4,8 vertices, so 23=82^3 = 8 leaves and 241=152^{4}-1 = 15 vertices in total — the bounds are met with equality exactly for perfect trees.

Example (why balance matters). A binary search tree holding 1,000,0001{,}000{,}000 keys in its leaves needs height at least log2106=20\lceil \log_2 10^{6}\rceil = 20. A balanced tree achieves height 20\approx 20, so a lookup touches about 2020 nodes; a degenerate tree (a path — every internal node has one child) has height 106\approx 10^6, so a lookup can touch a million nodes. Balancing is exactly the discipline of keeping the height near its lower bound log2\log_2 \ell, which is why AVL/red–black trees guarantee O(logn)O(\log n) operations.

Remark (counting shapes — Catalan numbers). The number of distinct shapes of a binary tree with nn nodes (left/right distinguished, unlabelled) is the nn-th Catalan number

Cn=1n+1(2nn),C_n = \frac{1}{n+1}\binom{2n}{n},

so C1=1,C2=2,C3=5,C4=14,C_1=1, C_2=2, C_3=5, C_4=14,\dots. Catalan numbers also count balanced parenthesizations and full binary trees with n+1n+1 leaves — a recurring motif in combinatorics, though beyond our present scope.


12.7 Tree traversals

Definition (Traversal). A traversal of a rooted tree is a systematic order that visits every vertex exactly once. The cost of any of the traversals below is Θ(n)\Theta(n) time (each vertex and edge is handled a constant number of times).

We use the specific binary tree of the slides.

(Figure — the slide binary tree.) Root AA; AA’s children are BB (left) and CC (right); BB’s children are DD (left) and EE (right); CC’s children are FF (left) and GG (right) — a perfect binary tree of three levels.

          A
        /   \
       B     C
      / \   / \
     D   E F   G

Levels are — depth 00: AA; depth 11: B,CB, C; depth 22: D,E,F,GD, E, F, G.

Breadth-first (level-order) traversal

Visit vertices level by level, left to right, using a queue: enqueue the root; repeatedly dequeue a vertex, visit it, and enqueue its children left-to-right. For the figure:

A    B    C    D    E    F    G\boxed{A\;\;B\;\;C\;\;D\;\;E\;\;F\;\;G}

This is exactly breadth-first search from the root (Lecture 11) applied to the tree.

Depth-first orders

The three depth-first orders are defined recursively. For a node xx with left subtree LL and right subtree RR:

  • Pre-order — visit xx, then LL, then RR (root first).
  • In-order — visit LL, then xx, then RR (root in the middle; meaningful for binary trees).
  • Post-order — visit LL, then RR, then xx (root last).

Tracing the recursion on the figure:

Order Sequence
Pre-order A    B    D    E    C    F    GA\;\;B\;\;D\;\;E\;\;C\;\;F\;\;G
In-order D    B    E    A    F    C    GD\;\;B\;\;E\;\;A\;\;F\;\;C\;\;G
Post-order D    E    B    F    G    C    AD\;\;E\;\;B\;\;F\;\;G\;\;C\;\;A

As a sanity check: the root AA appears first in pre-order and last in post-order — a property that holds for every rooted tree and is a handy way to spot a miscomputed sequence.

Reconstruction. Pre-order together with in-order (or post-order together with in-order) uniquely determines a binary tree: the pre-order tells you the root; the in-order then tells you which vertices fall in the left subtree (those before the root) and which in the right (those after), and you recurse. This is precisely how the tree above is recovered from the slide’s sequences, and it is a standard exercise and interview question.

Common pitfall. In-order is only well-defined for binary (more generally, ordered) trees, because it must interleave the node “between” its left and right subtrees. For a general mm-ary or unordered tree, pre- and post-order still make sense but in-order does not.

CS connection (expression trees). An arithmetic expression tree for (a+b)×c(a+b)\times c has ×\times at the root, left subtree (+ab)(+\,a\,b), and right leaf cc:

        ×
       / \
      +   c
     / \
    a   b
  • Pre-order yields prefix / Polish notation: ×  +  a  b  c\times\;+\;a\;b\;c.
  • Post-order yields postfix / reverse Polish notation: a  b  +  c  ×a\;b\;+\;c\;\times.
  • In-order (with parentheses) yields the usual infix form: (a+b)×c(a+b)\times c.

Post-order matches a stack machine: scanning ab+c×a\,b\,+\,c\,\times, push operands, and on each operator pop its arguments, compute, push the result. This is why reverse Polish calculators and many virtual machines (e.g. the JVM’s operand stack) evaluate in post-order.


12.8 Spanning trees, Cayley’s formula, and the Matrix-Tree theorem

Definition (Spanning tree). Given a connected graph G=(V,E)G=(V,E), a spanning tree is a subgraph TT that (i) is a tree and (ii) contains every vertex of GG. By Theorem 12.4, a spanning tree has all nn vertices and exactly n1n-1 edges; it is a minimally connected / maximally acyclic skeleton of GG.

Theorem 12.10. Every connected graph has a spanning tree.

Proof. Two constructions, either suffices.

Edge-deletion (top-down). While GG contains a cycle, delete an edge on that cycle. By Fact 12.5 connectivity is preserved and the process strictly decreases the edge count, so it terminates. The result is connected and acyclic — a spanning tree (it still contains every vertex, since we never delete vertices).

Search-tree (bottom-up). Run BFS or DFS from any vertex (Lecture 11). The tree edges — those that first discover a new vertex — form a connected, acyclic, spanning subgraph, hence a spanning tree. \blacksquare

A disconnected graph has no spanning tree, but each component has one; together they form a spanning forest. When GG carries edge weights, a minimum spanning tree (MST) is a spanning tree of least total weight — constructed by Kruskal’s and Prim’s algorithms in Lecture 11, which we do not repeat here. Spanning trees model loop-free broadcast in networks (the spanning-tree protocol in Ethernet switching exists precisely to break cycles that would otherwise cause broadcast storms).

Counting spanning trees: Cayley’s formula

Theorem 12.11 (Cayley’s formula, 1889). The complete graph KnK_n has exactly nn2n^{\,n-2} spanning trees. Equivalently, there are nn2n^{\,n-2} distinct labelled trees on the vertex set {1,2,,n}\{1,2,\dots,n\}.

The complete graph K₄, whose 4² = 16 spanning trees illustrate Cayley's formula

We state it and illustrate it for small nn rather than proving it in full; the cleanest proof is the bijection of the next paragraph.

  • n=1n = 1: 11=11^{-1} = 1 tree (the single vertex), by convention.
  • n=2n = 2: 20=12^{0} = 1 tree (the single edge 121\text–2).
  • n=3n = 3: 31=33^{1} = 3 trees. Every tree on 33 vertices is a path; the three labelled paths differ by which vertex is the middle: 1231\text–2\text–3, 1321\text–3\text–2, 2132\text–1\text–3.
  • n=4n = 4: 42=164^{2} = 16 trees. Every tree on 44 vertices is either a path P4P_4 or a star K1,3K_{1,3}. Labelled paths: 4!/2=124!/2 = 12 (order the four vertices, divide by the two directions). Labelled stars: 44 (choose the centre). Total 12+4=1612 + 4 = 16. \checkmark

Prüfer’s bijection (idea). Cayley’s exponent n2n-2 is explained by a beautiful encoding due to Prüfer (1918). Given a labelled tree on {1,,n}\{1,\dots,n\} with n2n \ge 2, repeat n2n-2 times: find the leaf with the smallest label, record the label of its unique neighbour, and delete the leaf. This produces a Prüfer sequence — a string of length n2n-2 over the alphabet {1,,n}\{1,\dots,n\}. The map is a bijection between labelled trees and such strings, and there are exactly nn2n^{\,n-2} strings, which is Cayley’s formula. (Decoding reverses the process.) The vertices appearing in the sequence are the internal vertices; a vertex’s number of appearances is its degree minus one — a useful fact in itself.

The Matrix-Tree theorem (for general graphs)

For a graph other than KnK_n, the count is given by a determinant.

Theorem 12.12 (Kirchhoff’s Matrix-Tree theorem, 1847). Let GG be a connected graph with adjacency matrix AA and diagonal degree matrix DD, and form the Laplacian L=DAL = D - A. Then the number of spanning trees of GG equals the value of any cofactor of LL (delete any one row and the matching column, and take the determinant of what remains).

We use it as a black box. Applied to KnK_n (whose Laplacian is nIJnI - J) it recovers Cayley’s nn2n^{n-2}, tying the two results together.

Examples.

  • K4K_4 has 442=164^{\,4-2} = 16 spanning trees; K5K_5 has 53=1255^{3} = 125; K6K_6 has 64=12966^{4} = 1296.
  • The cycle CnC_n has exactly nn spanning trees: deleting any one of its nn edges leaves a path (a tree), and no other subgraph on n1n-1 edges spans it, so there are precisely nn choices.
K4 (left) and two of its 16 spanning trees:

   1---2        1---2        1   2
   |\ /|        |            |   |
   | X |        |            |   |
   |/ \|        |            |   |
   4---3        4---3        4---3

12.9 Bipartite graphs and the odd-cycle theorem

Definition (Bipartite graph). A graph GG is bipartite if its vertex set can be partitioned into two disjoint sets V1,V2V_1, V_2 (the parts) so that every edge joins a vertex of V1V_1 to a vertex of V2V_2 — no edge has both endpoints in the same part. Equivalently, GG is properly 22-colourable: colour V1V_1 red and V2V_2 blue and no edge is monochromatic.

A bipartite graph with its two vertex parts drawn in different colours; a graph is bipartite iff it has no odd cycle

Definition (Complete bipartite graph). Km,nK_{m,n} has parts of sizes mm and nn with every cross pair joined; it has mnmn edges.

Bipartite graphs model any two-sided relationship: students–courses, jobs–applicants, tasks–time-slots, senders–receivers. The central question “can these two sides be matched up?” (bipartite matching) is efficiently solvable, whereas the analogous question in general graphs is much harder — another example of structure buying tractability.

Matchings and Hall’s theorem. A matching is a set of edges no two of which share an endpoint; it is perfect if it covers every vertex. In a bipartite graph with parts X,YX,Y, whether XX can be completely matched into YY is settled by Hall’s marriage theorem: a matching saturating XX exists if and only if every subset SXS\subseteq X satisfies N(S)S|N(S)|\ge|S| — no kk vertices on the left collectively see fewer than kk neighbours on the right. Necessity is immediate; sufficiency is proved with augmenting paths — find a path that alternates unmatched and matched edges between two exposed vertices and flip it, enlarging the matching until Hall’s condition finally blocks growth. This augmenting-path idea is the efficient algorithm alluded to above.

Theorem 12.13 (Odd-cycle characterization; cf. Lecture 10, Theorem 10.17). A graph GG is bipartite if and only if it contains no cycle of odd length.

Proof.

(\Rightarrow) Bipartite     \implies no odd cycle. Suppose GG is bipartite with parts AA and BB, and let v0,v1,,vk=v0v_0, v_1, \dots, v_k = v_0 be any cycle. Each edge crosses between the parts, so the vertices alternate: if v0Av_0 \in A then v1Bv_1 \in B, v2Av_2 \in A, and in general viAv_i \in A iff ii is even. Returning to vk=v0Av_k = v_0 \in A forces kk to be even. Hence every cycle has even length; there is no odd cycle.

(\Leftarrow) No odd cycle     \implies bipartite. It suffices to 22-colour each connected component, since a disjoint union of bipartite graphs is bipartite (colour the components independently). So assume GG is connected and has no odd cycle.

By Theorem 12.10, GG has a spanning tree TT; root it at any vertex rr. Colour each vertex vv by the parity of its depth in TT (the length of the unique rrvv path in TT): white if even, black if odd. This partitions VV into two parts; we must check that every edge of GG joins different colours.

Take an edge uvE(G)uv \in E(G).

  • If uvTuv \in T: the depths of uu and vv differ by exactly 11, so their parities differ and the colours differ.
  • If uvTuv \notin T: adding uvuv to TT creates exactly one cycle (Theorem 12.4(6)), namely the unique tree path PP from uu to vv together with the edge uvuv. This cycle lies in GG, so by hypothesis its length P+1|P| + 1 is even, whence P|P| is odd. The tree-path length satisfies

Pdepth(u)+depth(v)(mod2)|P| \equiv \operatorname{depth}(u) + \operatorname{depth}(v) \pmod 2

(because P=depth(u)+depth(v)2depth(lca)|P| = \operatorname{depth}(u) + \operatorname{depth}(v) - 2\operatorname{depth}(\text{lca}), where lca denotes their lowest common ancestor, and 2depth(lca)2\operatorname{depth}(\text{lca}) is even). Since P|P| is odd, depth(u)\operatorname{depth}(u) and depth(v)\operatorname{depth}(v) have different parities, so uu and vv get different colours.

Every edge is thus bichromatic, so the colouring is proper and GG is bipartite. \blacksquare

Corollary 12.14. Every tree is bipartite (it has no cycle at all, hence no odd cycle). More generally, every forest is bipartite.

Example. The 44-cycle C4=v1v2v3v4C_4 = v_1v_2v_3v_4 is bipartite with parts {v1,v3}\{v_1, v_3\} and {v2,v4}\{v_2, v_4\}: BFS from v1v_1 assigns levels 0,1,2,10,1,2,1, two colours with no clash. The 55-cycle C5C_5 is not bipartite — it is itself an odd cycle, so any attempt to 22-colour it collides after going once around.

Example (the hypercube). The dd-dimensional hypercube QdQ_d, whose vertices are the binary strings of length dd with edges between strings differing in one bit, is bipartite: colour each vertex by the parity of its number of $1$s. Every edge flips one bit, hence changes the parity, so it is bichromatic. (This is Exercise S7.)

Common pitfall (Eulerian \ne bipartite, etc.). “Bipartite” constrains cycles (their lengths), while “Eulerian” constrains degrees (their parities). They are unrelated: C4C_4 is both; C6C_6 is both; C3C_3 is Eulerian but not bipartite; the path P3P_3 is bipartite but not Eulerian. Do not conflate a parity condition on cycle lengths with a parity condition on vertex degrees.


12.10 Regular graphs

Definition (Regular graph). A graph is kk-regular if every vertex has the same degree kk. A 33-regular graph is also called cubic.

Proposition 12.15. A kk-regular graph on nn vertices has m=nk2m = \dfrac{nk}{2} edges. In particular nknk must be even: no kk-regular graph exists when both nn and kk are odd.

Proof. By the handshake identity, 2m=vdeg(v)=nk2m = \sum_v \deg(v) = nk, so m=nk/2m = nk/2. Since mm is an integer, nknk is even. \blacksquare

Familiar regular graphs: the cycle CnC_n is 22-regular; the complete graph KnK_n is (n1)(n-1)-regular; the complete bipartite Kn,nK_{n,n} is nn-regular; the hypercube QdQ_d is dd-regular; the Petersen graph is cubic (33-regular) on 1010 vertices.

Example. Can a 33-regular graph have 55 vertices? Then nk=15nk = 15 is odd, so m=15/2m = 15/2 is not an integer — impossible. A cubic graph must have an even number of vertices (e.g. K4K_4 is cubic on 44 vertices with 43/2=64\cdot 3/2 = 6 edges).


12.11 Eulerian graphs and Euler’s theorem

Definition. An Euler circuit (Eulerian circuit) is a closed walk that uses every edge exactly once and returns to its start. An Euler trail (Eulerian path) uses every edge exactly once but may start and end at different vertices. A graph with an Euler circuit is an Eulerian graph.

These conditions are about covering all edges, in contrast to the Hamiltonian conditions of §12.12 which cover all vertices. We prove Euler’s characterization for connected graphs; it holds verbatim for connected multigraphs, which we need for Königsberg. (Isolated vertices, having degree 00, are irrelevant and are ignored; “connected” means the edges live in one piece.)

Theorem 12.16 (Euler, 1736). Let GG be a connected (multi)graph with at least one edge.

  1. GG has an Euler circuit if and only if every vertex has even degree.
  2. GG has an Euler trail but no Euler circuit if and only if exactly two vertices have odd degree; those two are the endpoints of every such trail.

Proof of (1).

(\Rightarrow) Necessity. Suppose GG has an Euler circuit CC. Fix a vertex vv and walk along CC. Every time CC passes through vv it arrives on one edge and departs on another, consuming two edges incident to vv; and because CC is closed, even the visits at the start/end pair up (the very first edge leaving the start and the very last edge returning to it form one such pair). Since CC uses each edge exactly once, the edges at vv are partitioned into these entry–exit pairs. Hence deg(v)\deg(v) is even, for every vv.

(\Leftarrow) Sufficiency. Suppose GG is connected, has an edge, and every vertex has even degree. We construct an Euler circuit (this is Hierholzer’s algorithm, 1873).

Step 1 — any maximal trail from a vertex is closed. Start at any vertex uu that has an incident edge and greedily extend a trail (a walk with no repeated edge), leaving each vertex on a not-yet-used edge, continuing as long as possible. Claim: the trail can only get stuck back at uu. Indeed, consider the moment the trail arrives at some vertex wuw \ne u. Counting edges of ww used so far: each earlier visit to ww used two (in and out), and this arrival uses one more, an odd total. Since deg(w)\deg(w) is even, at least one edge at ww remains unused, so the trail can leave ww — it is not stuck. Only at uu can the parity work out to leave no exit. Therefore the maximal trail is a closed trail C0C_0 ending at uu.

Step 2 — splice in the leftover edges. If C0C_0 uses every edge of GG, it is an Euler circuit and we are done. Otherwise, let HH be the subgraph of unused edges. In passing from GG to HH we removed the closed trail C0C_0, which uses an even number of edges at every vertex (entry–exit pairs again), so every vertex of HH still has even degree. Because GG is connected, some vertex ww lying on C0C_0 is incident to an unused edge, i.e. degH(w)>0\deg_H(w) > 0. Apply Step 1 inside HH starting at ww to obtain a closed trail C1C_1 through ww using only unused edges. Now splice C1C_1 into C0C_0: traverse C0C_0 until reaching ww, detour completely around C1C_1 (returning to ww), then finish C0C_0. The result is a longer closed trail using strictly more edges.

Step 3 — terminate. Each splice increases the number of used edges, and GG has finitely many edges, so after finitely many splices every edge is used. The final closed trail is an Euler circuit. \blacksquare

Proof of (2). First, the number of odd-degree vertices is always even (handshake corollary), so “exactly one odd vertex” is impossible and the only traversable cases are 00 or 22 odd vertices.

(\Leftarrow) Suppose exactly two vertices u,vu, v have odd degree. Add a new edge e=uve = uv (a parallel edge if u,vu,v are already adjacent; the graph is allowed to be a multigraph). This raises deg(u)\deg(u) and deg(v)\deg(v) by 11, making all degrees even, and keeps the graph connected. By part (1) there is an Euler circuit; deleting the added edge ee from it breaks the circuit open into a trail that uses every original edge exactly once and runs between uu and vv — an Euler trail.

(\Rightarrow) Suppose GG has an Euler trail from aa to bb (aba \ne b, since there is no Euler circuit). Add the edge abab; the trail plus abab is an Euler circuit, so by part (1) all degrees in G+abG + ab are even. Removing abab lowers only deg(a)\deg(a) and deg(b)\deg(b) by 11, making exactly aa and bb odd and all others even. Thus exactly two vertices (a,ba, b) have odd degree, and they are the trail’s endpoints. \blacksquare

Example (Seven Bridges of Königsberg). Königsberg’s four land masses and seven bridges form a multigraph with vertex degrees 3,3,3,53, 3, 3, 5 — four odd-degree vertices.

        (A) 3
        /|  \
       / |   \
    (B)3-+---(C)3        [seven edges = seven bridges]
       \ |   /
        \|  /
        (D) 5

Euler’s theorem needs 00 odd vertices for a circuit or exactly 22 for a trail; with four odd vertices Königsberg has neither — no walk crosses every bridge exactly once. Euler’s 1736 analysis of this puzzle is regarded as the founding result of graph theory. By contrast, K5K_5 is 44-regular (all degrees even) and connected, so it is Eulerian.

CS connection. The “even degree” condition powers de Bruijn graph genome assembly, where reconstructing a DNA sequence from overlapping fragments reduces to finding an Euler trail. The route-inspection (“Chinese Postman”) problem asks for the shortest closed walk covering every edge and starts from exactly this theory.


12.12 Hamiltonian graphs; Dirac’s and Ore’s conditions

Definition. A Hamiltonian path visits every vertex exactly once. A Hamiltonian cycle (circuit) does so and returns to its start. A graph with a Hamiltonian cycle is a Hamiltonian graph.

Superficially this is Euler’s problem with “edge” replaced by “vertex,” but it is fundamentally harder. There is no known simple necessary-and-sufficient criterion for Hamiltonicity, and deciding whether a graph is Hamiltonian is NP-complete — one of Karp’s original 21 NP-complete problems, and the pure form of the Travelling Salesman Problem. What we do have are useful sufficient (not necessary) conditions that guarantee Hamiltonicity when the graph is “dense enough.”

Theorem 12.17 (Ore, 1960). Let GG be a simple graph with n3n \ge 3 vertices such that deg(u)+deg(v)n\deg(u) + \deg(v) \ge n for every pair of non-adjacent vertices u,vu, v. Then GG is Hamiltonian.

Corollary 12.18 (Dirac, 1952). If GG is a simple graph with n3n \ge 3 vertices and deg(v)n/2\deg(v) \ge n/2 for every vertex vv, then GG is Hamiltonian.

Dirac follows instantly from Ore (any two vertices, adjacent or not, have degree sum n/2+n/2=n\ge n/2 + n/2 = n), so we prove Ore. The argument is a classic and illustrates the “maximal counterexample + rotation” technique.

Proof of Theorem 12.17 (Ore). Suppose, for contradiction, that some graph satisfies Ore’s condition yet is not Hamiltonian. Adding an edge never decreases any degree, so Ore’s condition is preserved under adding edges; also, KnK_n is Hamiltonian for n3n \ge 3. Therefore, starting from our graph and adding edges one at a time, we reach a graph GG that is a maximal non-Hamiltonian graph satisfying Ore’s condition: GG is not Hamiltonian, but adding any missing edge creates a Hamiltonian cycle.

Since GG is not complete, choose non-adjacent vertices u,vu, v. Adding uvuv produces a Hamiltonian cycle, which must use the new edge uvuv (it is the only new edge). Deleting uvuv from that cycle leaves a Hamiltonian path in GG from uu to vv:

x1,x2,,xn,x1=u, xn=v,x_1, x_2, \dots, x_n, \qquad x_1 = u,\ x_n = v,

listing all nn vertices.

Define two index sets inside {1,2,,n1}\{1, 2, \dots, n-1\}:

S={i:uxi+1},T={i:vxi}.S = \{\, i : u \sim x_{i+1}\,\}, \qquad T = \{\, i : v \sim x_i \,\}.

Here S=deg(u)|S| = \deg(u) (each neighbour xi+1x_{i+1} of u=x1u=x_1 contributes the index ii) and T=deg(v)|T| = \deg(v) (each neighbour xix_i of v=xnv = x_n contributes ii).

Claim: SS and TT are disjoint. Suppose some iSTi \in S \cap T, so uxi+1u \sim x_{i+1} and vxiv \sim x_i. Then

x1x2xi xnxn1xi+1reversed x1x_1\, x_2 \cdots x_i\ \underbrace{x_n\, x_{n-1} \cdots x_{i+1}}_{\text{reversed}}\ x_1

is a cycle: xixnx_i \sim x_n (=vxi=v \sim x_i), the middle is the path reversed, and xi+1x1x_{i+1} \sim x_1 (=uxi+1= u \sim x_{i+1}). It visits every vertex exactly once — a Hamiltonian cycle in GG, contradicting that GG is non-Hamiltonian. So ST=S \cap T = \varnothing.

Both SS and TT are subsets of the (n1)(n-1)-element set {1,,n1}\{1, \dots, n-1\} and are disjoint, so

deg(u)+deg(v)=S+T=STn1<n,\deg(u) + \deg(v) = |S| + |T| = |S \cup T| \le n - 1 < n,

contradicting Ore’s condition for the non-adjacent pair u,vu, v. This contradiction proves the theorem. \blacksquare

Common pitfall (sufficient, not necessary). Dirac’s and Ore’s conditions can fail on graphs that are Hamiltonian. The cycle CnC_n (for large nn) is Hamiltonian by definition, yet every vertex has degree 2<n/22 < n/2, so Dirac’s hypothesis fails badly. Never conclude “not Hamiltonian” from the failure of Dirac/Ore.

Necessary conditions (easy direction). A Hamiltonian graph is connected and has minimum degree 2\ge 2 (a Hamiltonian cycle enters and leaves every vertex). Hence no tree with 3\ge 3 vertices is Hamiltonian — trees have leaves. Deleting any kk vertices from a Hamiltonian graph leaves at most kk components (toughness); this rules out many graphs but is still far from sufficient.

Example. KnK_n (n3n \ge 3) is Hamiltonian — list the vertices in any order and close the loop; Dirac holds since deg=n1n/2\deg = n - 1 \ge n/2. Every cycle CnC_n is trivially Hamiltonian. But the Petersen graph (cubic, 1010 vertices) has a Hamiltonian path yet no Hamiltonian cycle — a famous demonstration that regularity and high symmetry are not enough.


12.13 Euler versus Hamilton: a side-by-side

Eulerian Hamiltonian
Covers every… edge once vertex once
Clean criterion? Yes — even degrees (Thm 12.16) No — NP-complete in general
Testable in poly time? Yes (O(n+m)O(n+m)) Not known (NP-complete)
Sufficient conditions the degree parity is the criterion Dirac, Ore (dense graphs)
Historical seed Königsberg bridges, Euler 1736 Icosian game, Hamilton 1857

The two properties are independent. The bowtie graph — two triangles sharing a single vertex — is Eulerian but not Hamiltonian: every degree is even (the shared vertex has degree 44, the rest degree 22), yet the shared vertex is a cut-vertex, so no cycle can visit all five vertices exactly once. Conversely, K4K_4 is Hamiltonian but not Eulerian: it contains the 44-cycle 123411\text–2\text–3\text–4\text–1, but all of its degrees are 33 (odd). And K2,3K_{2,3} is neither. The lesson: an edge condition and a vertex condition genuinely capture different structure.

Side-by-side comparison of Eulerian traversal (every edge once, testable by even degrees) versus Hamiltonian traversal (every vertex once, NP-hard)

Common pitfall. Do not assume a graph that “obviously can be drawn in one stroke” is Hamiltonian, or vice versa. Drawing in one pen-stroke is the Eulerian question (traverse each edge/line once); visiting each junction once is the Hamiltonian one.


Historical notes

  • 1736 — Leonhard Euler, Solutio problematis ad geometriam situs pertinentis, solved the Seven Bridges of Königsberg, proving no such walk exists and inventing what we now call graph theory. His degree-parity insight is Theorem 12.16.
  • 1847 — Gustav Kirchhoff derived the Matrix-Tree theorem (Theorem 12.12) from the theory of electrical circuits, counting spanning trees via determinants.
  • 1857 — William Rowan Hamilton marketed the Icosian Game, a puzzle of finding a cycle through all 2020 vertices of a dodecahedron — the origin of the term Hamiltonian cycle (though such cycles had been studied earlier by Kirkman).
  • 1873 — Carl Hierholzer gave the constructive proof of Euler’s theorem used in §12.11.
  • 1889 — Arthur Cayley published the formula nn2n^{n-2} for labelled trees (Theorem 12.11); Heinz Prüfer (1918) later gave the elegant bijective proof.
  • 1952 — Gabriel Dirac and 1960 — Øystein Ore proved the sufficient degree conditions for Hamiltonicity (Theorem 12.17, Corollary 12.18).

Chapter summary

  • A tree is a connected acyclic graph. Theorem 12.4 shows this is equivalent to: connected with n1n-1 edges; acyclic with n1n-1 edges; a unique path between every pair; minimally connected; maximally acyclic. Neither “n1n-1 edges” alone nor “connected” alone suffices — you need a pair.
  • A tree on nn vertices has exactly n1n-1 edges (Lemma 12.2) and, for n2n\ge 2, at least two leaves (Theorem 12.1, via the longest-path argument). A forest with cc components has ncn-c edges.
  • A rooted tree adds root/parent/child/sibling/ancestor/descendant structure, with the depth of a vertex and the height of the tree.
  • mm-ary / binary trees. A full mm-ary tree with ii internal vertices has mi+1mi+1 vertices and (m1)i+1(m-1)i+1 leaves (Theorem 12.7). Height hh forces at most mhm^h leaves (Theorem 12.8), so a tree with \ell leaves has hlogmh \ge \lceil \log_m \ell\rceil — the reason balanced search trees run in Θ(logn)\Theta(\log n).
  • Traversals. Breadth-first (level order) and the depth-first pre-/in-/post-order. On the slide tree: BFS =ABCDEFG= A\,B\,C\,D\,E\,F\,G, pre =ABDECFG= A\,B\,D\,E\,C\,F\,G, in =DBEAFCG= D\,B\,E\,A\,F\,C\,G, post =DEBFGCA= D\,E\,B\,F\,G\,C\,A. Pre+in (or post+in) rebuild a binary tree; expression trees give prefix/infix/postfix notation.
  • Spanning trees have all nn vertices and n1n-1 edges; every connected graph has one. KnK_n has nn2n^{n-2} of them (Cayley, via Prüfer sequences); the Matrix-Tree theorem counts them for any graph.
  • A graph is bipartite     \iff it is 22-colourable     \iff it has no odd cycle (Theorem 12.13, proved via a spanning tree). Every tree/forest is bipartite.
  • A kk-regular graph on nn vertices has nk/2nk/2 edges, so nknk is even.
  • Euler (Theorem 12.16): a connected graph has an Euler circuit     \iff all degrees even; an Euler trail (and no circuit)     \iff exactly two odd vertices.
  • Hamilton: visits every vertex once; no simple criterion (NP-complete), but Ore (degu+degvn\deg u + \deg v \ge n for non-adjacent u,vu,v) and its corollary Dirac (degn/2\deg \ge n/2) are sufficient. Euler is about edges, Hamilton about vertices.

Exercises

Warm-up

W1. Draw all unlabelled trees on 55 vertices, and say how many there are.

W2. A forest has 2020 vertices and 1717 edges. How many connected components does it have?

W3. A full binary tree has 2525 internal vertices. How many leaves does it have, and how many vertices in total?

W4. For the rooted binary tree with root RR, RR’s children XX (left) and YY (right), XX’s children PP (left) and QQ (right), and YY’s single left child ZZ, write the pre-order, in-order, post-order, and level-order sequences.

W5. Decide whether C6C_6 and C7C_7 are bipartite, giving the reason in terms of odd cycles; for the bipartite one, exhibit the two parts.

W6. Does a 55-regular graph on 1111 vertices exist? Justify with the handshake identity.

W7. How many spanning trees does each of K2K_2, K6K_6, and the cycle C7C_7 have?

W8. State the four vertex degrees of the Königsberg multigraph and use Euler’s theorem to decide whether an Euler trail or circuit exists.

Standard

S1. Prove that adding a single edge between two non-adjacent vertices of a tree creates exactly one cycle. (This is Theorem 12.4(6); prove it directly from the unique-path property.)

S2. Prove that if a tree has a vertex of degree kk, then it has at least kk leaves. (Hint: the kk edges at that vertex lead into kk subtrees.)

S3. Let TT be a tree and LL its number of leaves. Prove the identity

L=2+v: deg(v)3(deg(v)2).L = 2 + \sum_{v:\ \deg(v)\ge 3}\bigl(\deg(v) - 2\bigr).

(Hint: evaluate v(deg(v)2)\sum_v(\deg(v)-2) using Corollary 12.3.)

S4. Verify Cayley’s formula for n=4n = 4 by classifying every labelled tree on {1,2,3,4}\{1,2,3,4\} as a path or a star, counting each type, and checking the total is 42=164^{2} = 16.

S5. Prove that a connected graph with nn vertices and exactly nn edges has exactly one cycle (such graphs are called unicyclic). (Hint: compare with a spanning tree.)

S6. For the graph on vertices {1,2,3,4,5}\{1,2,3,4,5\} with edges {12,23,34,45,51,13}\{12,23,34,45,51,13\}, list the degrees and determine whether it has an Euler circuit, an Euler trail, or neither; if a trail exists, name its endpoints.

S7. Prove that the hypercube QdQ_d is bipartite by exhibiting an explicit 22-colouring, and state the two part sizes.

S8. Show directly (without Theorem 12.13) that every tree is bipartite by 22-colouring it via the parity of depth from a chosen root, and apply your method to the tree of §12.3 to list the two parts.

S9. Compute all four traversal orders (pre-, in-, post-, level-) for the expression tree of (ab)×(c+d)(a - b) \times (c + d), and read off the prefix and postfix forms of the expression.

S10. Prove that a connected graph in which every vertex has even degree can be decomposed into edge-disjoint cycles. (Hint: min degree 2\ge 2 forces a cycle; remove it and induct.)

Challenge

C1. Give a complete, self-contained proof of the equivalence (1)(2)(3)(1)\Leftrightarrow(2)\Leftrightarrow(3) in Theorem 12.4 for a graph on nn vertices, quoting the leaf theorem and edge-count lemma where needed.

C2. Prove Ore’s theorem (Theorem 12.17) in your own words, filling in every step of the maximal-counterexample and rotation argument, and then deduce Dirac’s theorem as a corollary.

C3. Prove that every tree has a centre consisting of either one vertex or two adjacent vertices. (Hint: repeatedly delete all current leaves simultaneously; show the process preserves the tree’s “middle” and terminates at K1K_1 or K2K_2.)

C4. Using the Matrix-Tree theorem, write down the Laplacian of the cycle C4C_4, compute a cofactor, and confirm it equals 44, matching the direct count of spanning trees of CnC_n.

C5. Prove that a graph is bipartite if and only if every one of its subgraphs HH has an independent set containing at least half of HH’s vertices. (One direction uses the 22-colouring; the other produces an odd cycle.)


Selected answers & hints

W2. Components c=nm=2017=3c = n - m = 20 - 17 = 3 (Proposition 12.6).

W3. Full binary: =i+1=26\ell = i + 1 = 26 leaves, n=2i+1=51n = 2i + 1 = 51 vertices (Theorem 12.7). Check i+=25+26=51i + \ell = 25 + 26 = 51.

W4. Tree shape:

        R
       / \
      X   Y
     / \  /
    P   Q Z

Pre-order RXPQYZR\,X\,P\,Q\,Y\,Z; in-order PXQRZYP\,X\,Q\,R\,Z\,Y; post-order PQXZYRP\,Q\,X\,Z\,Y\,R; level-order RXYPQZR\,X\,Y\,P\,Q\,Z.

W5. C6C_6 is bipartite (an even cycle, no odd cycle): parts {v1,v3,v5},{v2,v4,v6}\{v_1,v_3,v_5\},\{v_2,v_4,v_6\}. C7C_7 is not bipartite: it is an odd cycle, and by Theorem 12.13 a graph with an odd cycle cannot be 22-coloured.

W6. nk=115=55nk = 11 \cdot 5 = 55 is odd, so m=55/2m = 55/2 is not an integer — no such graph exists (Proposition 12.15).

W7. K2K_2: 20=12^{0} = 1. K6K_6: 64=12966^{4} = 1296 (Cayley). C7C_7: 77 spanning trees (delete any one of the 77 edges).

W8. Degrees 3,3,3,53,3,3,5 — four odd-degree vertices. Euler requires 00 (circuit) or 22 (trail) odd vertices, so Königsberg has neither.

S3. For a tree, v(deg(v)2)=2m2n=2(n1)2n=2\sum_v(\deg(v) - 2) = 2m - 2n = 2(n-1) - 2n = -2. Splitting by degree and using that a tree with n2n\ge 2 has no isolated vertices: leaves contribute 1-1 each (L-L total), degree-22 vertices contribute 00, and the rest contribute deg(v)21\deg(v) - 2 \ge 1. Hence L+deg3(deg(v)2)=2-L + \sum_{\deg\ge 3}(\deg(v)-2) = -2, i.e. L=2+deg(v)3(deg(v)2)L = 2 + \sum_{\deg(v)\ge 3}(\deg(v) - 2).

S4. Labelled paths on 44 vertices: 4!/2=124!/2 = 12; labelled stars K1,3K_{1,3}: 44 (choose the centre). Total 12+4=16=4212 + 4 = 16 = 4^{2}. \checkmark

S5. A spanning tree TT of the connected graph uses n1n-1 edges (Theorem 12.10, Lemma 12.2), so exactly one edge ee of GG is outside TT. Adding ee to TT creates exactly one cycle (Theorem 12.4(6)); since G=T+eG = T + e, that is the unique cycle of GG.

S6. Degrees: deg1=3, deg2=2, deg3=3, deg4=2, deg5=2\deg 1 = 3,\ \deg 2 = 2,\ \deg 3 = 3,\ \deg 4 = 2,\ \deg 5 = 2 (sum =12=2m= 12 = 2m, m=6m = 6). Exactly two odd-degree vertices (11 and 33), and the graph is connected, so it has an Euler trail but no Euler circuit, with endpoints 11 and 33.

S7. Colour each binary string of QdQ_d by the parity of its number of $1$s. Every edge flips one bit, so it changes the parity and is bichromatic; hence QdQ_d is bipartite. The two parts (even-weight and odd-weight strings) each have 2d12^{d-1} vertices.

S9. Expression tree: root ×\times, left subtree (ab)(-\,a\,b), right subtree (+cd)(+\,c\,d). Pre-order (prefix) ×ab+cd\times\,-\,a\,b\,+\,c\,d; in-order (infix, with parentheses) (ab)×(c+d)(a-b)\times(c+d); post-order (postfix) abcd+×a\,b\,-\,c\,d\,+\,\times; level-order ×+abcd\times\,-\,+\,a\,b\,c\,d.

C1. Follow the arc (1)(2)(3)(1)(1)\Rightarrow(2)\Rightarrow(3)\Rightarrow(1) of §12.3: Lemma 12.2 gives (1)(2)(1)\Rightarrow(2); the cycle-edge-deletion argument with Fact 12.5 gives (2)(3)(2)\Rightarrow(3); and summing ni1n_i - 1 over components gives (3)(1)(3)\Rightarrow(1). Each step relies on the leaf theorem only through Lemma 12.2.

C2. See the proof of Theorem 12.17. The three moving parts are: (i) adding edges preserves Ore’s condition, so a maximal non-Hamiltonian example exists; (ii) a non-adjacent pair u,vu,v yields a Hamiltonian uuvv path; (iii) the rotation trick makes S={i:uxi+1}S = \{i: u\sim x_{i+1}\} and T={i:vxi}T = \{i: v\sim x_i\} disjoint subsets of an (n1)(n-1)-set, forcing degu+degvn1<n\deg u + \deg v \le n-1 < n — the contradiction. Dirac follows because degn/2\deg \ge n/2 for all vertices implies degu+degvn\deg u + \deg v \ge n for every pair.


Further reading

  • Kenneth H. Rosen, Discrete Mathematics and Its Applications — the chapters on Graphs and on Trees cover this material at the same level, with many additional examples on tree traversals, spanning trees, and Euler/Hamilton paths.
  • Douglas B. West, Introduction to Graph Theory — a more advanced treatment: trees and distance, Eulerian circuits, and Hamiltonian cycles (including full proofs of Ore’s and Dirac’s theorems and much on the Matrix-Tree theorem).
  • Cross-references within this course: Lecture 10 (graph basics: degrees, walks, paths, cycles, connectivity, the handshake identity) and Lecture 11 (BFS/DFS, shortest paths, and Kruskal’s / Prim’s minimum-spanning-tree algorithms).

Lectures/CDM-L12.md · 60.0 KB · updated 2026-08-01 20:12