impera Open Impera

IB Diploma Mathematics AI · Geometry and trigonometry

Graph theory (HL)

1The language of graphs

A graph is a set of vertices (points) joined by edges.

  • The degree of a vertex is the number of edges meeting there.
  • Handshaking lemma: the sum of degrees $= 2 \times$ the number of edges, so the number of odd vertices is always even.
  • A simple graph has no loops and no repeated edges.
  • A graph is connected if there is a route between every pair of vertices.
  • A complete graph $K_n$ has every pair of its $n$ vertices joined: $\frac{n(n-1)}{2}$ edges.
  • In a directed graph the edges have arrows. In a weighted graph each edge has a number (a distance, cost or time).
  • A walk is any sequence of edges; a trail repeats no edge; a path repeats no vertex; a cycle is a closed path.
  • An Eulerian circuit uses every edge once and returns to the start: it exists when every vertex is even. An Eulerian trail exists when exactly two vertices are odd (it starts at one and ends at the other).
  • A Hamiltonian cycle visits every vertex once and returns to the start.
Example

a graph with degrees 3, 3, 2, 2, 4 has $\frac{3 + 3 + 2 + 2 + 4}{2} = 7$ edges.

Check yourself

How many edges does the complete graph K₆ have?

  • 15

  • 36

  • 6

  • 30

Show the answer

15

6 × 5 ÷ 2 = 15.

2Adjacency matrices and walks

An adjacency matrix $A$ records the graph: the entry in row $i$, column $j$ is the number of edges from vertex $i$ to vertex $j$.

  • For an undirected graph the matrix is symmetric.
  • The entry in row $i$, column $j$ of $A^k$ is the number of walks of length $k$ from $i$ to $j$.
  • A transition matrix for a random walk divides each vertex's edges equally: from a vertex of degree 3, each neighbour has probability $\frac{1}{3}$. Its steady state shows the long-run share of time at each vertex (the idea behind ranking web pages).
Worked example

Counting walks

  1. Graph with edges $PQ$, $PR$, $QR$, $RS$. Order the rows $P, Q, R, S$.
  2. $A$ has rows $(0,1,1,0)$, $(1,0,1,0)$, $(1,1,0,1)$, $(0,0,1,0)$.
  3. With technology, $A^3$ has row $P$ equal to $(2, 3, 4, 1)$.
  4. So there are 4 walks of length 3 from $P$ to $R$, and 2 from $P$ back to $P$ ($P\!-\!Q\!-\!R\!-\!P$ and $P\!-\!R\!-\!Q\!-\!P$).
Example

for a random walk on this graph the steady state is proportional to the degrees $2, 2, 3, 1$, so in the long run the walker is at $R$ for $\frac{3}{8}$ of the time.

Check yourself

Which entry tells you the number of walks of length 4 from vertex 2 to vertex 5?

  • Row 2, column 5 of A⁴

  • The sum of row 2 of A

  • Row 4, column 5 of A

  • Row 2, column 5 of 4A

Show the answer

Row 2, column 5 of A⁴

Powers of A count walks.

3Trees and minimum spanning trees

A tree is a connected graph with no cycles. A tree with $n$ vertices has exactly $n - 1$ edges.

A minimum spanning tree (MST) joins every vertex with the smallest possible total weight, for example the cheapest network of cables.

  • Kruskal's algorithm: list the edges from lightest to heaviest. Add each one unless it makes a cycle. Stop at $n - 1$ edges.
  • Prim's algorithm: start at any vertex. Repeatedly add the lightest edge joining the tree to a new vertex. Prim's can also be run on a table (matrix) of weights.
  • Both give a tree of the same minimum total weight.

Kruskal's algorithm: green edges form the minimum spanning tree

Worked example

Kruskal's algorithm

  1. Edges: $BC = 2$, $AC = 3$, $DE = 3$, $AB = 4$, $BD = 5$, $CD = 6$, $CE = 7$.
  2. Add $BC$ (2), $AC$ (3), $DE$ (3).
  3. $AB$ (4) would make the cycle $A\!-\!B\!-\!C$, so reject it.
  4. Add $BD$ (5). That is 4 edges for 5 vertices, so stop.
  5. Minimum total weight $= 2 + 3 + 3 + 5 = 13$.
Check yourself

A tree has 12 vertices. How many edges does it have?

  • 11

  • 13

  • 66

  • 12

Show the answer

11

A tree always has n − 1 edges.

4The Chinese postman problem

The Chinese postman problem asks for the shortest closed route that travels every edge at least once, like a postal worker or a gritting lorry.

  • If every vertex is even, an Eulerian circuit exists: the answer is just the total weight.
  • With two odd vertices, find the shortest route between them. That route is repeated, so add its length to the total.
  • With four odd vertices, try all three ways of pairing them up, and add the smallest total of the two shortest routes.
Worked example

Two odd vertices

  1. Use the graph above. Total weight $= 4 + 3 + 2 + 5 + 6 + 7 + 3 = 30$.
  2. Degrees: $A$ 2, $B$ 3, $C$ 4, $D$ 3, $E$ 2. The odd vertices are $B$ and $D$.
  3. Shortest route $B$ to $D$: directly 5 (via $C$ it is $2 + 6 = 8$).
  4. Shortest postman route $= 30 + 5 = 35$, repeating edge $BD$.
Check yourself

A graph has total weight 50 and odd vertices X and Y, with shortest route 8 between them. What is the shortest Chinese postman route?

  • 58

  • 42

  • 66

  • 50

Show the answer

58

The X–Y route must be travelled twice.

5The travelling salesman problem

The travelling salesman problem (TSP) asks for the shortest closed route that visits every vertex. It is usually set on a complete graph of shortest distances. No quick method is known for large graphs ($K_n$ has $\frac{(n-1)!}{2}$ different cycles), so we find bounds.

  • Upper bound, nearest neighbour: from the start, always go to the nearest unvisited vertex, then return to the start. Any real route gives an upper bound; the smallest upper bound is the best.
  • Lower bound, deleted vertex: delete one vertex, find the MST of the rest, then add the two shortest edges from the deleted vertex. The largest lower bound is the best.
  • The optimal length lies between: lower bound ≤ optimum ≤ upper bound.
Worked example

Bounding a tour of five towns

  1. Distances: $AB$ 7, $AC$ 5, $AD$ 9, $AE$ 6, $BC$ 4, $BD$ 8, $BE$ 10, $CD$ 6, $CE$ 7, $DE$ 5.
  2. Nearest neighbour from $A$: $A \to C$ (5), $C \to B$ (4), $B \to D$ (8), $D \to E$ (5), $E \to A$ (6). Upper bound $= 28$.
  3. Delete $A$. MST of $B, C, D, E$: $BC$ 4, $DE$ 5, $CD$ 6, total 15.
  4. Add the two shortest edges from $A$: $AC$ 5 and $AE$ 6. Lower bound $= 15 + 11 = 26$.
  5. So $26 \le$ optimum $\le 28$. (Checking all 12 cycles shows the optimum is 28.)
Check yourself

Nearest neighbour gives 41 and the deleted-vertex method gives 36. What can you say about the shortest tour T?

  • 41 ≤ T ≤ 77

  • T = 41 exactly

  • T = 36 exactly

  • 36 ≤ T ≤ 41

Show the answer

36 ≤ T ≤ 41

Lower bound ≤ optimum ≤ upper bound.

6Eulerian and Hamiltonian

Two key types of route:

  • Eulerian circuit: uses every edge exactly once and returns to the start. It exists if every vertex has even degree.
  • Eulerian trail: uses every edge once but starts and ends at different vertices; exactly two vertices have odd degree.
  • Hamiltonian cycle: visits every vertex exactly once and returns to the start.
Example

The bridges of Königsberg
In 1736, Leonhard Euler showed that it was impossible to walk across each of Königsberg's seven bridges exactly once, because more than two land areas had an odd number of bridges. This is often seen as the start of graph theory.

Check yourself

When does a graph have an Eulerian circuit?

  • When every vertex has even degree

  • When every vertex has odd degree

  • Always

  • When it has exactly one odd vertex

Show the answer

When every vertex has even degree

Euler's result.

7Kruskal's and Prim's algorithms

Both find a minimum spanning tree: connecting all vertices with the smallest total weight and no cycles.

  • Kruskal's: list edges from smallest to largest; add each one unless it creates a cycle.
  • Prim's: start from any vertex; repeatedly add the smallest edge connecting the tree to a new vertex.
Worked example

Kruskal's on edges AB 3, BC 4, AC 5, CD 2, BD 6

  1. Add CD (2).
  2. Add AB (3).
  3. Add BC (4); now A, B, C, D are connected.
  4. Skip AC and BD (they would create cycles).
  5. Minimum spanning tree weight: $2 + 3 + 4 = 9$.
Check yourself

A spanning tree for 6 vertices has how many edges?

  • 4

  • 5

  • 7

  • 6

Show the answer

5

$n - 1$.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera