TExcelTAD
Home › Discrete Mathematics › Simple Graphs and Degrees

Simple Graphs and Degrees

A simple graph G=(V,E)G = (V, E) has a set VV of vertices and a set EE of edges, each edge an unordered pair {u,v}\{u, v\} 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 uu and vv are joined by an edge
degree deg⁡(v)\deg(v) number of edges at vv
complete graph KnK_n every pair of nn vertices adjacent: (n2)\binom{n}{2} edges
cycle CnC_n nn vertices in a ring
line graph (path) LnL_n nn 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:

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

Proof. Each edge {u,v}\{u, v\} adds 1 to deg⁡(u)\deg(u) and 1 to deg⁡(v)\deg(v). ■\blacksquare

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 3,3,33, 3, 3 cannot exist.

python · try it yourself
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 BB of 2,000 buyers and a set SS 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 BB and one in SS, so counting edges from both sides gives

∑s∈Sdeg⁡(s)=∣E∣=∑b∈Bdeg⁡(b)⇒avg deg in Savg deg in B=∣B∣∣S∣\sum_{s \in S} \deg(s) = \lvert E \rvert = \sum_{b \in B} \deg(b) \quad\Rightarrow\quad \frac{\text{avg deg in } S}{\text{avg deg in } B} = \frac{\lvert B \rvert}{\lvert S \rvert}

The ratio of the averages is fixed by the sizes of the two sides: 1202=60\frac{120}{2} = 60, but 200050=40\frac{2000}{50} = 40. The numbers are inconsistent, so one of them is wrong (with 2 sellers per buyer the sellers must average 2⋅40=802 \cdot 40 = 80 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: (3,3,3)(3, 3, 3) fails the handshake lemma, and (3,3,1,1)(3, 3, 1, 1) 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 dd and subtract 1 from the next dd entries; repeat; the original is a graph's degree sequence exactly when you end with all zeros and never go negative.

python · try it yourself
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 O(∣V∣+∣E∣)O(\lvert V \rvert + \lvert E \rvert)) or adjacency matrices (O(∣V∣2)O(\lvert V \rvert^2), 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 KnK_n

How many edges does K10K_{10} 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 10⋅9=90=2∣E∣10 \cdot 9 = 90 = 2\lvert E \rvert, giving 45 edges, which is also (102)\binom{10}{2}.

Exercise 2: Impossible graphs

Can there be a graph with 7 vertices each of degree 3?

Answer

No.

Solution

The degree sum would be 7⋅3=217 \cdot 3 = 21, which is odd, but it must equal 2∣E∣2\lvert E \rvert.

Exercise 3: Havel–Hakimi by hand

Is (3,2,2,2,1)(3, 2, 2, 2, 1) a degree sequence?

Answer

Yes.

Solution

Remove 3, subtract 1 from the next three: (1,1,1,1)(1, 1, 1, 1). Remove 1, subtract from the next one: (0,1,1)→(1,1,0)(0, 1, 1) \to (1, 1, 0). Remove 1: (0,0)(0, 0). All zeros, so yes. (Example: a triangle a,b,ca, b, c plus edges aa–dd and dd–ee: degrees a:3,b:2,c:2,d:2,e:1a{:}3, b{:}2, c{:}2, d{:}2, e{:}1.)

Test yourself

A graph has degrees 4, 3, 3, 2, 2. How many edges does it have?

  1. 14
  2. 7
  3. 5
  4. 10

Which statement holds in every graph?

  1. Every vertex has even degree
  2. The number of odd-degree vertices is even
  3. There is a vertex of degree 0
  4. The number of edges is even

How many edges does the complete graph KnK_n have?

  1. nn
  2. n−1n - 1
  3. n(n−1)2\frac{n(n-1)}{2}
  4. n2n^2