Simple Graphs and Degrees
A simple graph has a set of vertices and a set of edges, each edge an unordered pair of two different vertices. No directions, no loops, no repeated edges. Friendships, road networks without one-way streets, molecules, chip wiring and mesh triangulations are simple graphs. Graph theory is the part of discrete math with the most direct uses in code, and the Data Structures & Algorithms course covers the algorithms; here we focus on the mathematics.
Vocabulary
| Term | Meaning |
|---|---|
| adjacent | and are joined by an edge |
| degree | number of edges at |
| complete graph | every pair of vertices adjacent: edges |
| cycle | vertices in a ring |
| line graph (path) | vertices in a row |
| subgraph | some of the vertices and some of the edges between them |
The Handshake Lemma
Theorem. In every graph, the sum of the degrees is twice the number of edges:
Proof. Each edge adds 1 to and 1 to .
Corollary. The number of vertices with odd degree is even. (If it were odd, the degree sum would be odd.)
So at a party, the number of people who shook an odd number of hands is even. And a graph with degrees cannot exist.
import random
def random_graph(n, p, rng):
return [(u, v) for u in range(n) for v in range(u + 1, n) if rng.random() < p]
rng = random.Random(11)
for trial in range(4):
n = rng.randint(5, 12)
E = random_graph(n, 0.4, rng)
deg = [sum(1 for e in E if v in e) for v in range(n)]
odd = sum(d % 2 for d in deg)
print(f"n={n:2d} |E|={len(E):2d} degree sum={sum(deg):2d} odd-degree vertices={odd}")n=12 |E|=28 degree sum=56 odd-degree vertices=4
n= 5 |E|= 5 degree sum=10 odd-degree vertices=2
n=11 |E|=24 degree sum=48 odd-degree vertices=6
n= 6 |E|= 9 degree sum=18 odd-degree vertices=2
Averages on two sides of a bipartite graph
A marketplace has a set of 2,000 buyers and a set of 50 sellers, with an edge whenever a buyer has bought from a seller. A report says "the average seller has 120 customers" and "the average buyer has used 2 sellers". Can both be true? Each edge has one end in and one in , so counting edges from both sides gives
The ratio of the averages is fixed by the sizes of the two sides: , but . The numbers are inconsistent, so one of them is wrong (with 2 sellers per buyer the sellers must average customers). This double-counting check catches errors in survey and analytics data.
Degree sequences
The degree sequence lists all degrees in decreasing order. Not every sequence is possible: fails the handshake lemma, and fails because two vertices of degree 3 in a 4-vertex graph are adjacent to everything, so no vertex can have degree 1. The Havel–Hakimi algorithm decides any sequence: remove the largest degree and subtract 1 from the next entries; repeat; the original is a graph's degree sequence exactly when you end with all zeros and never go negative.
def graphical(seq):
seq = sorted(seq, reverse=True)
while seq and seq[0] > 0:
d = seq.pop(0)
if d > len(seq):
return False
for i in range(d):
seq[i] -= 1
if seq[i] < 0:
return False
seq.sort(reverse=True)
return True
for s in [(3, 3, 3), (3, 3, 1, 1), (3, 3, 2, 2, 2), (4, 4, 4, 4, 4), (5, 3, 3, 3, 2, 2)]:
print(s, graphical(s))(3, 3, 3) False
(3, 3, 1, 1) False
(3, 3, 2, 2, 2) True
(4, 4, 4, 4, 4) True
(5, 3, 3, 3, 2, 2) True
In software
Graphs are stored as adjacency lists (a dict of neighbour lists, memory ) or adjacency matrices (, fast edge lookup). The handshake lemma is a cheap consistency check for graph data: the total length of all adjacency lists must be even, and each edge must appear in both endpoint lists. In social networks the degree distribution is heavy-tailed, so a few "hub" users have enormous degree.
Exercises
Exercise 1: Edges of
How many edges does have? What is the degree of each vertex?
Answer
45 edges, degree 9.
Solution
Each vertex is adjacent to the other 9, so the degree sum is , giving 45 edges, which is also .
Exercise 2: Impossible graphs
Can there be a graph with 7 vertices each of degree 3?
Answer
No.
Solution
The degree sum would be , which is odd, but it must equal .
Exercise 3: Havel–Hakimi by hand
Is a degree sequence?
Answer
Yes.
Solution
Remove 3, subtract 1 from the next three: . Remove 1, subtract from the next one: . Remove 1: . All zeros, so yes. (Example: a triangle plus edges – and –: degrees .)
Test yourself
A graph has degrees 4, 3, 3, 2, 2. How many edges does it have?
- 14
- 7
- 5
- 10
Which statement holds in every graph?
- Every vertex has even degree
- The number of odd-degree vertices is even
- There is a vertex of degree 0
- The number of edges is even
How many edges does the complete graph have?