Graph Algorithms

Graph theory is the branch of mathematics that studies networks of vertices (nodes) connected by edges. A graph G = (V, E) consists of a set of vertices V and a set of edges E linking pairs of vertices. Edges may be undirected (two-way connections) or directed; they may also carry a weight representing cost, distance, or time. Graph algorithms give us systematic procedures for solving optimisation problems on these networks — problems that appear everywhere from GPS routing and logistics planning to circuit design and social-network analysis. Two fundamental questions drive many graph algorithms: Can we find a route through a network that uses every edge exactly once (an Eulerian trail) or visits every vertex exactly once (a Hamiltonian path)? And how do we build or traverse the network as efficiently as possible, minimising total cost? Algorithms such as Kruskal's and Prim's answer the second question by constructing a minimum spanning tree — the cheapest connected subgraph — while the Chinese Postman and Travelling Salesman problems tackle real-world route-planning challenges. At A-level and IB level, these algorithms are studied both for their theoretical elegance and their direct modelling applications. You will learn to decide whether an Eulerian circuit exists using a simple degree-parity rule, to apply Kruskal's and Prim's algorithms step by step on weighted graphs, and to find practical upper and lower bounds for the Travelling Salesman Problem (TSP) — a problem so hard that no efficient exact algorithm is known for large networks.

Key takeaways
  • Eulerian Trails and Circuits
  • Hamiltonian Paths and Cycles
  • Minimum Spanning Tree: Kruskal's Algorithm
  • Minimum Spanning Tree: Prim's Algorithm
  • The Chinese Postman Problem
  • Travelling Salesman Problem (TSP): Nearest-Neighbour Heuristic
  • TSP: Lower Bound via Deleted-Vertex Method

Eulerian Trails and Circuits

An Eulerian trail is a walk through a graph that uses every edge exactly once. An Eulerian circuit is an Eulerian trail that starts and ends at the same vertex. Euler proved in 1736 (solving the Königsberg Bridge Problem) that an Eulerian circuit exists if and only if every vertex has even degree, and the graph is connected. An Eulerian trail (not a circuit) exists if and only if exactly two vertices have odd degree — these become the start and end points. The degree of a vertex is the number of edges incident to it. Check degrees first before attempting any route.

Hamiltonian Paths and Cycles

A Hamiltonian path visits every vertex exactly once. A Hamiltonian cycle does the same and returns to the start. Unlike Eulerian circuits, no simple degree condition guarantees a Hamiltonian cycle — this is an NP-complete problem. Dirac's Theorem gives a sufficient (not necessary) condition: if every vertex in an n-vertex graph (n ≥ 3) has degree at least n/2, a Hamiltonian cycle is guaranteed to exist. In practice at school level, Hamiltonian cycles are found by inspection or with heuristic algorithms like nearest-neighbour.

Minimum Spanning Tree: Kruskal's Algorithm

A spanning tree of a connected graph is a subgraph that includes all vertices and is a tree (connected, no cycles). Its weight is the sum of its edge weights. Kruskal's algorithm finds the minimum spanning tree (MST) by a greedy edge-selection strategy: (1) List all edges in ascending order of weight. (2) Add the cheapest edge that does NOT create a cycle in the edges already selected. (3) Repeat until n−1 edges have been chosen (where n = number of vertices). The result is the MST. Checking for cycles can be done visually at school level; formally it uses a Union-Find data structure.

Minimum Spanning Tree: Prim's Algorithm

Prim's algorithm grows the MST one vertex at a time: (1) Choose any starting vertex; add it to the tree T. (2) From all edges connecting T to vertices NOT yet in T, select the minimum-weight edge and add it (and its new vertex) to T. (3) Repeat step 2 until all vertices are in T. Both Kruskal's and Prim's always produce the same MST weight (though the tree may look different if tie-breaking differs). Prim's is often easier to track in a table — note the current cheapest connection to each unvisited vertex and update as new vertices join.

The Chinese Postman Problem

A postman must traverse every street (edge) at least once and return to the start — minimising total distance. If the graph already has an Eulerian circuit (all vertices even degree), the optimal route simply follows that circuit. If some vertices have odd degree, we must repeat (duplicate) some edges to make all degrees even. The algorithm: (1) Identify all odd-degree vertices. (2) Find the minimum-weight perfect matching among them — i.e., pair them up so the total weight of shortest paths between pairs is minimised. (3) Add those paths as repeated edges. (4) Find an Eulerian circuit on the augmented graph. The minimum total route = (sum of all edge weights) + (weight of the minimum matching).

Travelling Salesman Problem (TSP): Nearest-Neighbour Heuristic

The TSP asks: given a complete weighted graph, find the Hamiltonian cycle of minimum total weight. This is NP-hard, so we use heuristics for large problems. The Nearest-Neighbour (NN) heuristic gives an upper bound: (1) Start at any vertex. (2) Move to the nearest unvisited vertex. (3) Repeat until all vertices are visited. (4) Return to the start. The result is a valid Hamiltonian cycle but not necessarily optimal. Applying NN from different starting vertices and taking the best result improves the bound.

TSP: Lower Bound via Deleted-Vertex Method

To find a lower bound for TSP: (1) Delete one vertex (and all its edges) from the graph. (2) Find the MST of the remaining graph using Kruskal's or Prim's — call its weight W. (3) Add back the two cheapest edges that originally connected the deleted vertex to the rest of the graph — call their total weight C. (4) Lower bound = W + C. This bound is valid because any Hamiltonian cycle, when vertex v is removed, leaves a Hamiltonian path on the remaining vertices, which must be at least as heavy as the MST; and the cycle must also use at least 2 edges at v. Repeating for several deleted vertices and taking the largest lower bound gives the best (tightest) lower bound.

The Key Equation

WMST=∑e∈Tw(e),∣T∣=n−1W_{\text{MST}} = \sum_{e \in T} w(e), \quad |T| = n - 1
  • W_{\text{MST}} — MST total weight (same as edge weights (e.g. km, £)): The sum of all edge weights in the spanning tree, which is minimised over all possible spanning trees.
  • T — spanning tree edge set (dimensionless): The set of n−1 edges selected to form the minimum spanning tree.
  • w(e) — edge weight (km, £, minutes, etc.): The numerical cost or distance assigned to each edge e in the graph.
  • n — number of vertices (dimensionless): The total count of vertices (nodes) in the graph; a spanning tree always has exactly n−1 edges.

Worked Example

Problem. A weighted graph has 5 vertices: A, B, C, D, E. The edge weights are: AB=4, AC=2, AD=7, BC=5, BD=3, BE=6, CD=1, CE=8, DE=4. Apply Kruskal's algorithm to find the MST and state its total weight. Then find an upper bound for the TSP using the nearest-neighbour heuristic starting at A.

WMST=1+2+3+4=10;TSP upper bound=2+1+3+6+9=21W_{\text{MST}} = 1+2+3+4 = 10; \quad \text{TSP upper bound} = 2+1+3+6+9 = 21

Answer. MST weight = 10 (edges CD, AC, BD, DE). Nearest-neighbour TSP upper bound starting at A = 21 (route A–C–D–B–E–A).

Figure
Diagram
Graph Algorithms: Weighted Graph with MST HighlightedAB=4AC=2AE=7BD=3CD=1DE=4CE=5BE=6ABCDEMST edges (Kruskal's): AC+CD+BD+DE = 10Non-MST edges
Origins · 1 minLeonhard Euler and how this was discovered
LE
Leonhard Euler
1707–1783 · Swiss

Euler founded graph theory in 1736 with his solution to the Königsberg Bridge Problem — proving that it is impossible to walk across all seven bridges of the city exactly once and return to the start. He showed this is possible only when at most two vertices have odd degree, establishing the theory of Eulerian circuits that we still use today in route-inspection algorithms. Euler lost sight in his right eye in his late 20s and became almost totally blind by age 60, yet he produced roughly half of his mathematical output after going blind — often dictating to his assistants.

  • Graph Theory Fundamentals — Understanding vertices, edges, degree, and types of graphs is essential before applying algorithms like Kruskal's or Prim's.
  • Greedy Algorithms — Both Kruskal's and Prim's are greedy algorithms — they make the locally optimal choice at each step, which happens to produce a globally optimal MST.
  • Network Flow and Optimisation — Network flow problems extend graph theory to maximising flow through a network, sharing the same framework of weighted directed graphs.
  • Linear Programming — The TSP can be formulated as an integer linear programme; understanding LP helps appreciate why TSP is computationally difficult and how bounds are derived.
  • Mathematical Modelling — Graph algorithms are a prime example of mathematical modelling — real-world logistics problems are abstracted into graphs so that algorithms can find optimal or near-optimal solutions.