Graphs and Degrees

Study Sheet

Graphs and Degrees

Vertices and edges, degree, the handshake lemma, and the graphs every course uses

What Is a Graph?

Concept
Vertices, edges, order and size

A (simple) graph G=(V,E)G = (V, E) consists of a finite set VV of vertices together with a set EE of edges, each edge an unordered pair {u,v}\{u, v\} of distinct vertices. uu and vv are adjacent (or neighbors) when {u,v}∈E\{u,v\} \in E. The order of GG is ∣V∣|V| and its size is ∣E∣|E|.

A graph records only which pairs are connected, not where the vertices sit on the page or in what order you listed them: two very different-looking drawings can be the same graph, an idea made precise in the next topic (Isomorphism and Subgraphs). Writing an edge as the unordered pair {u,v}\{u,v\} rather than the ordered pair (u,v)(u,v) builds in that a friendship or a road goes both ways; directed graphs, where (u,v)≠(v,u)(u,v) \ne (v,u), wait until Topic 10. Requiring u≠vu \ne v rules out loops, and simple additionally rules out a repeated edge between the same two vertices, so at most one edge joins any pair. Strip away everything from a map or a circuit diagram except which pairs are joined, and what is left is a graph.

Example
A graph on five vertices
12345
G1G_1: five vertices, five edges.

Let G1G_1 have vertex set {1,2,3,4,5}\{1,2,3,4,5\} and edge set {{1,2},{1,3},{2,3},{3,4},{4,5}}\{\{1,2\}, \{1,3\}, \{2,3\}, \{3,4\}, \{4,5\}\}. Its order is ∣V∣=5|V| = 5 and its size is ∣E∣=5|E| = 5. Vertices 11 and 44 are not adjacent, since no edge joins them, while 11 and 33 are, since {1,3}∈E\{1,3\} \in E.

Example
A genuine problem: a round robin

Six teams play a round robin: every pair of teams plays exactly one game. Model this with one vertex per team and an edge per game; every pair of the 66 vertices is joined, so counting edges means counting pairs of teams: (62)=6⋅52=15\binom{6}{2} = \dfrac{6\cdot5}{2} = 15 games in all. (This all-pairs-joined graph is named K6K_6 in the catalog later in this topic.)

Example
The empty graph
wxyz
Order 4, size 0: four vertices, no edges.

A graph can have vertices and no edges at all: take V={w,x,y,z}V = \{w,x,y,z\} and E=∅E = \emptyset. Its order is 44 and its size is 00 — a perfectly good graph, and the extreme opposite of the round robin above, where every pair was joined.

Side note
Where it goes wrong

Listing {3,4}\{3,4\} and {4,3}\{4,3\} as two different edges inflates the edge count, since an edge is an unordered pair: {3,4}={4,3}\{3,4\} = \{4,3\} is a single edge. In G1G_1 above, ∣E∣=5|E| = 5, not 1010 — double-listing every edge in both orders is the single most common miscount in this course.

Try it
Try it: an eight-team round robin

Eight teams play a round robin, every pair exactly once. How many games are played?

Show the answer

Model it as one vertex per team with an edge per game; every pair of the 88 vertices is joined, so the count is (82)=8⋅72=28\binom{8}{2} = \dfrac{8\cdot7}{2} = \mathbf{28} games.

Degree and the Handshake Lemma

Concept
Degree and the handshake lemma

The degree of vertex vv, written deg⁡(v)\deg(v), is the number of edges incident to vv (equivalently, the number of neighbors of vv). The handshake lemma:

∑v∈Vdeg⁡(v)=2∣E∣.\sum_{v \in V} \deg(v) = 2|E|.

Each edge {u,v}\{u,v\} has exactly two endpoints, so it adds exactly 11 to deg⁡(u)\deg(u) and 11 to deg⁡(v)\deg(v): a single edge contributes 22 to the total of all degrees, never 11. Summing degrees is therefore the same as counting (edge, endpoint) pairs, and there are two such pairs per edge, which is the whole proof (spelled out in Why It Works). The lemma is the reason the sum of all degrees is always even, and it is the single most useful counting fact in the subject: a quick parity check rules out whole families of degree sequences before any drawing is attempted. It also makes ∣E∣|E| computable from degrees alone, without ever listing the edges.

Example
Degrees of G1G_1
12345
Degrees 2,2,3,2,12,2,3,2,1 sum to 10=2∣E∣10 = 2|E|.

In G1G_1 from the last section, deg⁡(1)=2\deg(1) = 2, deg⁡(2)=2\deg(2) = 2, deg⁡(3)=3\deg(3) = 3, deg⁡(4)=2\deg(4) = 2, deg⁡(5)=1\deg(5) = 1. Summing gives 2+2+3+2+1=102+2+3+2+1 = 10, and indeed 2∣E∣=2⋅5=102|E| = 2\cdot5 = 10: the handshake lemma checks out.

Example
A genuine problem: a party puzzle

Can 77 people at a party each shake hands with exactly 33 others? If so, the degree sum would be 7⋅3=217\cdot3 = 21. But 2121 is odd, and the handshake lemma forces the degree sum to equal 2∣E∣2|E|, always even. No such handshaking pattern exists, for any graph, regardless of how the 77 people are arranged.

Example
Isolated and universal vertices

In a graph of order nn, no vertex can have degree above n−1n-1 (there are only n−1n-1 other vertices to be adjacent to); a vertex of degree 00 is isolated, and one of degree n−1n-1 is universal. In G1G_1 (order 55), the maximum possible degree is 44; vertex 33, with degree 33, is adjacent to all but one other vertex — it misses only vertex 55.

Side note
Where it goes wrong

Writing ∑deg⁡(v)=∣E∣\sum \deg(v) = |E| undercounts by a factor of 22: summing the degrees of G1G_1 gives 2+2+3+2+1=102+2+3+2+1=10, not 55. Each edge is counted once at each of its two endpoints, so the degree sum is always 2∣E∣2|E|, never ∣E∣|E| itself — divide by 22 to recover the edge count.

Try it
Try it: find the missing degree

A graph has 99 edges and degree sequence 4,4,3,3,2,x4,4,3,3,2,x. Find xx.

Show the answer

By the handshake lemma the degrees sum to 2⋅9=182\cdot9 = 18. The five known degrees sum to 4+4+3+3+2=164+4+3+3+2=16, so x=18−16=2x = 18 - 16 = \mathbf{2}.

Degree Sequences

Concept
Degree sequences and the Havel-Hakimi test

The degree sequence of GG lists deg⁡(v)\deg(v) for every vertex, usually sorted d1≥d2≥⋯≥dnd_1 \ge d_2 \ge \cdots \ge d_n. A sequence is graphical if some simple graph has it as its degree sequence. Havel-Hakimi: (d1,…,dn)(d_1, \ldots, d_n) with d1≥⋯≥dn≥0d_1\ge\cdots\ge d_n\ge0 is graphical if and only if, after deleting d1d_1 and subtracting 11 from each of the next d1d_1 largest terms, the resulting sequence of length n−1n-1 is graphical (the empty sequence is graphical by convention).

The test turns an existence question into an algorithm: repeatedly take the largest remaining degree d1d_1, connect that vertex to the d1d_1 vertices with the next-largest degrees to use it up, and recurse on the smaller sequence. If this process ever needs a negative degree, the sequence cannot be realized; if it reaches all zeros, reversing the construction builds an explicit graph. The one subtlety the proof must address is why it is always safe to connect the top-degree vertex specifically to the next-highest vertices rather than to any d1d_1 of them; Why It Works shows that if a valid graph exists at all, one exists with that choice too. Degree sequences compress a graph into a single list, and this theorem says exactly which lists are achievable — not every non-increasing list of non-negative integers with an even sum is one.

Example
An immediate obstruction

Is (4,3,2)(4,3,2) graphical? Its sum is 4+3+2=94+3+2=9, which is odd. By the handshake lemma the degree sum of any graph is 2∣E∣2|E|, always even, so no graph has degree sequence (4,3,2)(4,3,2) — there is no need to even run Havel-Hakimi once the sum fails to be even.

Example
Realizing (3,3,2,2,2)(3,3,2,2,2)
abcde
A graph realizing degree sequence (3,3,2,2,2)(3,3,2,2,2).

The sum 3+3+2+2+2=123+3+2+2+2=12 is even, so try Havel-Hakimi. Take the largest degree, 33 (call its vertex aa), and connect it to the 33 vertices with the next-largest degrees, b,c,db,c,d (each degree 3,2,23,2,2), reducing them by 11: the sequence becomes (2,1,1,2)(2,1,1,2) for b,c,d,eb,c,d,e, i.e. sorted (2,2,1,1)(2,2,1,1) on {b,e,c,d}\{b,e,c,d\}. Repeat: connect bb (degree 22) to ee and the next vertex, say cc, giving (1,0,1)(1,0,1) on {e,c,d}\{e,c,d\}... following the construction through to the end gives the edges ab,ac,ad,be,bc,edab, ac, ad, be, bc, ed, whose degrees are exactly 3,3,2,2,23,3,2,2,2 for a,b,c,d,ea,b,c,d,e. The sequence is graphical.

Example
A genuine problem: is (4,3,3,2,2,1,1)(4,3,3,2,2,1,1) graphical?
abcdefg
A graph realizing degree sequence (4,3,3,2,2,1,1)(4,3,3,2,2,1,1).

The sum is 4+3+3+2+2+1+1=164+3+3+2+2+1+1=16, even, so the handshake lemma does not rule it out. Running Havel-Hakimi on 77 terms reaches an all-zero sequence without ever going negative, so it is graphical; carrying out the construction gives the edges ab,ac,ad,ae,bc,bd,ce,fgab, ac, ad, ae, bc, bd, ce, fg on vertices a,…,ga,\ldots,g, with degrees 4,3,3,2,2,1,14,3,3,2,2,1,1 exactly as required.

Side note
Where it goes wrong

An even sum is necessary but not sufficient. (3,3,3,1)(3,3,3,1) sums to 1010, even, yet Havel-Hakimi fails: deleting the first 33 and subtracting 11 from the next 33 terms gives (2,2,0)(2,2,0), and deleting that 22 and subtracting from the next 22 terms gives (1,−1)(1,-1) — a negative degree. No graph has degree sequence (3,3,3,1)(3,3,3,1), despite the even sum.

Try it
Try it: realize (4,2,2,1,1)(4,2,2,1,1)

Is the degree sequence (4,2,2,1,1)(4,2,2,1,1) graphical? If so, build a graph with it.

Show the answer

The sum 4+2+2+1+1=104+2+2+1+1=10 is even. Havel-Hakimi: connect the degree-44 vertex aa to all 44 others, using up aa and reducing the rest to (1,1,0,0)(1,1,0,0); then connect the remaining degree-11 vertex bb to the next, cc, reaching all zeros. The edges ab,ac,ad,ae,bcab, ac, ad, ae, bc give degrees 4,2,2,1,14,2,2,1,1 for a,b,c,d,ea,b,c,d,e: the sequence is graphical\mathbf{graphical}.

A Catalog of Special Graphs

Concept
Six families worth naming

KnK_n (complete graph): every pair of its nn vertices is adjacent; ∣E∣=(n2)|E| = \binom{n}{2} and every vertex has degree n−1n-1. CnC_n (cycle, n≥3n\ge3): vertices v1,…,vnv_1,\ldots,v_n in a ring, each viv_i adjacent to vi−1v_{i-1} and vi+1v_{i+1}; ∣E∣=n|E|=n and CnC_n is 22-regular. PnP_n (path): nn vertices in a line; ∣E∣=n−1|E| = n-1, two endpoints have degree 11 and the rest degree 22. Km,nK_{m,n} (complete bipartite): parts of size mm and nn with every cross pair adjacent and no edge inside a part; ∣E∣=mn|E| = mn. A graph is kk-regular if every vertex has degree kk; so KnK_n is (n−1)(n-1)-regular and CnC_n is 22-regular.

These are the graphs every later topic measures itself against: a Hamiltonian cycle (Topic 6) is literally a copy of CnC_n sitting inside a bigger graph, a tree (Topic 4) is what is left of a connected graph once every cycle is broken, and Km,nK_{m,n} is the extreme case of a bipartite graph (Topic 7), with every allowed edge present. Regularity is a symmetry condition: a kk-regular graph looks the same, degree-wise, from every vertex, and it already tells you ∣E∣=nk/2|E| = nk/2 by the handshake lemma, before a single edge is drawn. Naming these families turns a drawing into a sentence: saying a network is K3,4K_{3,4} states everything about its edges at once. The Petersen graph below is the standard example of a graph that is regular without being anywhere close to complete.

Example
Edges and degree of K5K_5
abcde
K5K_5: every pair of 55 vertices joined, 1010 edges.

For K5K_5: ∣E∣=(52)=10|E| = \binom{5}{2} = 10 and every one of the 55 vertices has degree 5−1=45-1=4. Check with the handshake lemma: 5⋅4=20=2⋅105\cdot4 = 20 = 2\cdot10.

Example
A genuine problem: a scheduling graph
m1m2m3e1e2e3e4
K3,4K_{3,4}: 33 managers, 44 engineers, 1212 meetings.

A firm has 33 managers and 44 engineers; every manager must hold a one-on-one meeting with every engineer. Model this as K3,4K_{3,4}, with the managers and engineers as the two parts. The number of meetings is ∣E∣=3⋅4=12|E| = 3\cdot4 = 12; each manager attends 44 meetings (degree 44) and each engineer attends 33 (degree 33). Check: 3⋅4+4⋅3=24=2⋅123\cdot4 + 4\cdot3 = 24 = 2\cdot12.

Example
The Petersen graph, checked two ways
o0o1o2o3o4i0i1i2i3i4
The Petersen graph: 1010 vertices, 1515 edges, 33-regular.

The Petersen graph has 1010 vertices and is 33-regular. Counting edges directly from its usual drawing (an outer 55-cycle, an inner 55-vertex star, and 55 spokes joining them) gives 5+5+5=155+5+5=15 edges. Counting instead from regularity, by the handshake lemma, gives ∣E∣=nk/2=10⋅3/2=15|E| = nk/2 = 10\cdot3/2 = 15. Both methods agree.

Side note
Where it goes wrong

K3,4K_{3,4} is not the same as a complete graph on 3+4=73+4=7 vertices: K7K_7 has (72)=21\binom{7}{2}=21 edges, one for every pair, including pairs within a side. K3,4K_{3,4} has only the 3⋅4=123\cdot4=12 cross edges and none within a part — mixing the two formulas is the most common error with bipartite graphs.

Try it
Try it: a path on eight vertices

Find ∣E∣|E| and the degree sequence of P8P_8.

Show the answer

A path on n=8n=8 vertices has ∣E∣=n−1=7|E| = n-1 = \mathbf{7} edges; its two endpoints have degree 11 and the other 66 vertices have degree 22, so the degree sequence is (2,2,2,2,2,2,1,1)\mathbf{(2,2,2,2,2,2,1,1)}. Check: 1+1+2⋅6=14=2⋅71+1+2\cdot6 = 14 = 2\cdot7.

Adjacency Matrices

Concept
The adjacency matrix

For G=(V,E)G=(V,E) with V={v1,…,vn}V=\{v_1,\ldots,v_n\}, the adjacency matrix AA is the n×nn\times n matrix with Aij=1A_{ij} = 1 if {vi,vj}∈E\{v_i,v_j\}\in E and Aij=0A_{ij}=0 otherwise (so Aii=0A_{ii}=0 for a simple graph). AA is symmetric, its ii-th row sum is deg⁡(vi)\deg(v_i), and (Ak)ij(A^k)_{ij} counts the walks of length kk from viv_i to vjv_j.

Encoding a graph as a matrix turns combinatorial questions into linear algebra: row sums recover degrees instantly, and matrix powers count walks because (Ak)ij=∑ℓAiℓ(Ak−1)ℓj(A^k)_{ij} = \sum_\ell A_{i\ell}(A^{k-1})_{\ell j} builds every length-kk walk by extending a length-(k−1)(k-1) walk one more step, exactly as the proof in Why It Works spells out. The diagonal of A2A^2 is especially useful: (A2)ii=deg⁡(vi)(A^2)_{ii} = \deg(v_i), since a closed walk of length 22 from viv_i is just out-and-back along one edge. Symmetry reflects that the graph is undirected, Aij=AjiA_{ij}=A_{ji}; Topic 10 drops this when the edges point one way. A matrix also mechanizes the whole theory: a computer stores and manipulates AA instantly, which is how every algorithm later in this course, from shortest paths to coloring, actually runs.

Example
Building the matrix
1234
G2G_2: a 44-cycle with diagonal 1313.

Let G2G_2 have vertices 1,2,3,41,2,3,4 and edges 12,23,34,41,1312,23,34,41,13 (a 44-cycle with one diagonal). Its adjacency matrix, in the order 1,2,3,41,2,3,4, is

A=(0111101011011010).A = \begin{pmatrix} 0&1&1&1 \\ 1&0&1&0 \\ 1&1&0&1 \\ 1&0&1&0 \end{pmatrix}.

Row sums are 3,2,3,23,2,3,2, matching deg⁡(1)=3\deg(1)=3, deg⁡(2)=2\deg(2)=2, deg⁡(3)=3\deg(3)=3, deg⁡(4)=2\deg(4)=2 read directly off the edge list.

Example
Walks of length two

Squaring the matrix above gives (A2)1,4=1(A^2)_{1,4}=1: there is exactly one walk of length 22 from 11 to 44, namely 1-3-41\text{-}3\text{-}4 (vertex 11 and 44 are already adjacent directly, but that is a walk of length 11, not 22). More strikingly, (A2)2,4=2(A^2)_{2,4}=2: vertices 22 and 44 are not adjacent at all, yet there are two length-22 walks between them, 2-1-42\text{-}1\text{-}4 and 2-3-42\text{-}3\text{-}4, one through each of their two common neighbors.

Example
A genuine problem: counting triangles

How many triangles does G2G_2 sit inside? Computing A3A^3 and taking its trace gives tr⁡(A3)=12\operatorname{tr}(A^3) = 12. Each triangle contributes 66 to this trace (a closed walk can start at any of its 33 vertices and go around in either of 22 directions), so the number of triangles is 12/6=212/6=2. Checking directly: {1,2,3}\{1,2,3\} is a triangle (edges 12,23,1312,23,13 all present) and so is {1,3,4}\{1,3,4\} (edges 13,34,4113,34,41 all present); no other 3-vertex subset has all three edges, so there are exactly 22 triangles, confirming the matrix count.

Side note
Where it goes wrong

The diagonal of AA is 00 for a simple graph, but the diagonal of A2A^2 is usually NOT 00: (A2)1,1=3(A^2)_{1,1}=3 in G2G_2, equal to deg⁡(1)\deg(1), since Aii2A^2_{ii} counts closed walks i-x-ii\text{-}x\text{-}i for each of the deg⁡(i)\deg(i) neighbors xx. Squaring a matrix with a zero diagonal does not keep the diagonal zero.

Try it
Try it: a walk count

In G2G_2 above, how many walks of length 22 are there from vertex 11 to vertex 22?

Show the answer

A length-22 walk from 11 to 22 passes through a common neighbor of both. The neighbors of 11 are 2,3,42,3,4 and the neighbors of 22 are 1,31,3; their only common neighbor is 33, giving the single walk 1-3-21\text{-}3\text{-}2. So (A2)1,2=1(A^2)_{1,2} = \mathbf{1}.

Why It Works

Form the set of (edge, endpoint) pairs {(e,v):v∈e}\{(e,v) : v \in e\}. Count it two ways. Grouping by vertex: each vv appears in this set once for every edge incident to it, so the count is ∑v∈Vdeg⁡(v)\sum_{v\in V} \deg(v). Grouping by edge: each edge {u,w}\{u,w\} has exactly two endpoints, uu and ww, so it contributes exactly 22 pairs, and the count is 2∣E∣2|E|. The two counts of the same set must agree, so ∑vdeg⁡(v)=2∣E∣\sum_{v} \deg(v) = 2|E|. Corollary: the number of odd-degree vertices is even. Split the sum into vertices of even and odd degree; the even-degree vertices contribute an even total, so the odd-degree vertices must also contribute an even total (an even sum minus an even sum is even). A sum of odd numbers is even only when there is an even count of them, so the number of odd-degree vertices is even.

One direction is immediate: if the reduced sequence (d2−1,…,dd1+1−1,dd1+2,…,dn)(d_2-1,\ldots,d_{d_1+1}-1,d_{d_1+2},\ldots,d_n) is realized by a graph HH on v2,…,vnv_2,\ldots,v_n, add a new vertex v1v_1 joined to exactly v2,…,vd1+1v_2,\ldots,v_{d_1+1} (the vertices whose degrees were reduced); this restores their original degrees and gives v1v_1 degree d1d_1, realizing the original sequence. The other direction needs an exchange argument. Suppose (d1,…,dn)(d_1,\ldots,d_n), sorted non-increasingly, is realized by some graph GG on v1,…,vnv_1,\ldots,v_n. If v1v_1 already happens to be adjacent to v2,…,vd1+1v_2,\ldots,v_{d_1+1}, deleting v1v_1 realizes the reduced sequence directly. If not, then since deg⁡(v1)=d1\deg(v_1)=d_1 but v1v_1 misses some vjv_j with j≤d1+1j \le d_1+1, v1v_1 must instead be adjacent to some vkv_k with k>d1+1k > d_1+1 (to reach degree d1d_1 at all). Since dj≥dkd_j \ge d_k (the sequence is sorted) and v1v_1 is adjacent to vkv_k but not vjv_j, while deg⁡(vj)≥deg⁡(vk)\deg(v_j) \ge \deg(v_k), there must be some vertex vmv_m adjacent to vjv_j but not to vkv_k (otherwise the neighborhood of vkv_k would contain that of vjv_j, forcing deg⁡(vk)>deg⁡(vj)\deg(v_k) > \deg(v_j) once the edge to v1v_1 is counted, a contradiction). Swapping edges v1vkv_1v_k and vmvjv_mv_j for v1vjv_1v_j and vmvkv_mv_k keeps every degree the same while making v1v_1 adjacent to one more of its intended targets; repeating this swap finitely many times makes v1v_1 adjacent to exactly v2,…,vd1+1v_2,\ldots,v_{d_1+1}, after which deleting v1v_1 realizes the reduced sequence.

Induction on kk. Base case k=1k=1: a walk of length 11 from viv_i to vjv_j is exactly an edge between them, and (A1)ij=Aij(A^1)_{ij}=A_{ij} is 11 if that edge exists and 00 otherwise, so the claim holds. Inductive step: assume (Ak−1)ℓj(A^{k-1})_{\ell j} counts walks of length k−1k-1 from vℓv_\ell to vjv_j for every ℓ\ell. A walk of length kk from viv_i to vjv_j is a step from viv_i to some neighbor vℓv_\ell, followed by a walk of length k−1k-1 from vℓv_\ell to vjv_j; the step exists exactly when Aiℓ=1A_{i\ell}=1. So the number of length-kk walks from viv_i to vjv_j is ∑ℓAiℓ⋅(walks of length k−1 from vℓ to vj)=∑ℓAiℓ(Ak−1)ℓj\sum_{\ell} A_{i\ell}\cdot(\text{walks of length } k-1 \text{ from } v_\ell \text{ to } v_j) = \sum_{\ell} A_{i\ell}(A^{k-1})_{\ell j}, which is exactly the matrix product entry (A⋅Ak−1)ij=(Ak)ij(A\cdot A^{k-1})_{ij} = (A^k)_{ij}. This completes the induction.