Graph Theory, Networks and Voronoi Diagrams

☰ Contents

In graph theory a graph is not a chart. It is a set of points, called vertices, and a set of connections between them, called edges. A road map is a graph once you keep only which places are joined.

The subject began in 1736, when Euler showed that no walk could cross each of the seven bridges of Königsberg exactly once. His argument used only the connections, never the shape of the town.

What are the parts of a graph?

A graph is built from vertices, drawn as dots, and edges, drawn as lines joining pairs of vertices. Each vertex has a degree: the number of edge ends that meet it. The handshake lemma links the two: the degrees always add to twice the number of edges. A square with one diagonal has 5 edges, and its vertices have degrees 3, 2, 3 and 2, so 3 + 2 + 3 + 2 = 10 = 2 × 5. The lemma holds because every edge has two ends, and each end adds 1 to one degree. A loop, an edge from a vertex back to itself, adds 2 to that vertex's degree. One consequence: the number of odd-degree vertices is always even, because the total is even. So a group in which exactly three people each shake an odd number of hands is impossible. Check a drawing by counting its edges twice, once directly and once as half the degree total.

12210

each edge contributes 1 to two vertices, so Σ deg(v) = 2|E|

Add edges until the sum of degrees is 10

AB3CDE
The degree of a vertex counts the edges meeting it, so B has degree 3. Full lesson: Vertices, Edges and Degree

See Vertices, Edges and Degree.

A simple graph has no loop and at most one edge between any pair of vertices. A complete graph joins every pair, so on n vertices it has n(n − 1)/2 edges. A bipartite graph splits the vertices into two sets, and every edge runs from one set to the other. See Simple, Complete and Bipartite Graphs.

ABCD
A complete graph joins every pair. Four vertices give 6 edges, and every degree is 3. Full lesson: Simple, Complete and Bipartite Graphs

A graph is connected when a route joins every pair of vertices. A tree is a connected graph with no cycle, and a tree on n vertices has exactly n − 1 edges: each new vertex needs one edge to join the tree, and a second edge would close a cycle. See Subgraphs, Connectedness and Trees.

A1B2C3D4E5F6
There are six vertices and five edges. A tree always has one fewer edge than vertices. Full lesson: Subgraphs, Connectedness and Trees

A directed graph gives every edge an arrow. Arrows arriving give the in-degree and arrows leaving give the out-degree, and the graph is strongly connected when the arrows lead from any vertex to any other. See Directed Graphs and Strong Connectedness.

Now you

ABCDDegree4343

These are the degrees of a graph. How many edges does it have?

ABCDE

What is the degree of vertex D?

How do you store a network as numbers?

A network is stored as an adjacency matrix: a square table with one row and one column for each vertex. Write 1 in row A, column B when an edge joins A and B, and 0 when none does. For a triangle ABC with a vertex D joined only to C, the rows read A: 0 1 1 0, B: 1 0 1 0, C: 1 1 0 1 and D: 0 0 1 0. See The Adjacency Matrix. It is symmetric across the main diagonal, since every edge joins its two vertices both ways, and each row total is a degree: row C gives 1 + 1 + 0 + 1 = 3. In a directed graph, row A, column B records only arrows from A to B, so symmetry is lost. Check the table by adding the row totals: 2 + 2 + 3 + 1 = 8, twice the 4 edges.

ABCDAA0100BB1010CC0100DD0000

each edge sets aᵢⱼ = aⱼᵢ = 1, so A equals its own transpose

Add edges until the table holds six 1s

A0110101011010010
Take the labels off and the numbers stand alone: the adjacency matrix. Full lesson: The Adjacency Matrix

Multiply the matrix by itself. Row i, column j of A² counts the walks of length 2 from i to j, because the product adds up, over every middle vertex k, the ways of going from i to k and then to j. In the same way Aⁿ counts the walks of length n. See Counting Walks with Matrix Powers.

A²2111121111301101
Multiply the matrix by itself. Row A, column D holds 1: one walk of length 2. Full lesson: Counting Walks with Matrix Powers

A weighted graph carries a number on each edge: a distance, a cost or a time. The table holds that weight in place of the 1. See The Weighted Adjacency Table.

42563789ABCDE
A route costs the sum of the weights along it, so A to B to D costs 4 + 6 = 10. Full lesson: The Weighted Adjacency Table

Now you

ABCDA0111B1001C1000D1100

What is the degree of vertex C?

ABCD

What goes in row A, column B of the adjacency matrix?

What do the route words actually mean?

Each route word says what a route may repeat. A walk is any sequence of edges, each starting where the last ended, and it may repeat anything. A trail repeats no edge. A path repeats no vertex, and so no edge either. Circuits are trails that end where they began, and a cycle is a circuit that repeats no vertex except the start. See Walks, Trails, Paths, Circuits and Cycles. On a square ABCD with the diagonal AC, the route A-B-C-A-D is a trail but not a path, because it returns to A. The words matter because route questions depend on what may be reused: covering every street is about edges, and visiting every town is about vertices. Two triangles sharing a vertex A show the gap between a circuit and a cycle: A-B-C-A-D-E-A uses every edge once but passes through A midway, so it is not a cycle. Its length, the number of edges, is 3 + 3 = 6.

12345632322

odd vertices = 2, so an Euler trail exists, but it must run from one odd vertex to the other

Add edges until you can trace every edge once and finish where you started

ABCDE
A circuit is a trail that ends where it began: A-B-C-D-E-C-A. Full lesson: Walks, Trails, Paths, Circuits and Cycles

An Eulerian circuit is a closed trail that uses every edge exactly once. A connected graph has one exactly when every vertex has even degree. With exactly two odd vertices the graph has an Eulerian trail instead, from one odd vertex to the other.

The reason is a count. Each time a trail passes through a vertex it uses one edge to arrive and one to leave, so every vertex except the start and the finish needs an even degree, and those two each have one unpaired edge. Any other number of odd vertices allows neither, and Königsberg had four. See Eulerian Trails and Circuits.

ABC4DE
One edge arrives and one leaves at each visit, so passing through needs an even degree. Full lesson: Eulerian Trails and Circuits
A5B3C3D3
With four odd vertices no trail can cover every edge: the bridges of Königsberg. Full lesson: Eulerian Trails and Circuits

A Hamiltonian cycle visits every vertex exactly once and returns to the start. There is no degree test for it, so routes have to be tried one by one. See Hamiltonian Paths and Cycles.

Now you

A2B2C3D2E1

Does this graph have an Eulerian circuit?

A2B2C4D2E2

A connected graph has exactly two vertices of odd degree. What follows?

What is the cheapest way to connect everything?

The cheapest way to connect every vertex is a minimum spanning tree: edges that join all the vertices, contain no cycle, and have the least total weight. Kruskal's algorithm finds one by taking edges cheapest first and skipping any that would close a cycle. Take four towns with roads AB = 2, BC = 3, AC = 4, CD = 5 and BD = 6. Kruskal takes AB and BC, rejects AC because A, B and C are already joined, then takes CD, for a total of 2 + 3 + 5 = 10. A tree on 4 vertices needs 4 − 1 = 3 edges, so it stops there. The greedy choice is safe because the cheapest edge across any split of the vertices into two groups belongs to some minimum tree. Equal weights may be taken in either order, which can give different trees with the same total. To confirm, swapping CD for BD costs 2 + 3 + 6 = 11.

2345678910ABCDEFAB 2BC 3AC 4CD 5DE 6CE 7EF 8DF 9AF 10taken 2 / 5 · cost 5

2 roads taken so far, none closing a loop: a tree on V towns needs exactly V − 1 = 5

Take the roads in cost order until all six towns are joined

See Kruskal's Algorithm for a Spanning Tree.

42563789ABCDE
B to C, weight 5, would close the cycle A-B-C. Reject it and move on. Full lesson: Kruskal’s Algorithm for a Spanning Tree

Prim's algorithm grows one tree from a starting vertex: at each step take the cheapest edge that leaves the tree and reaches a new vertex. The matrix method runs it on the weighted table. Cross out the column of each vertex in the tree, find the smallest entry left in the rows of those vertices, circle it, and that vertex joins. See Prim's Algorithm and the Matrix Method.

ABCDEA—42——B4—569C25—83D—68—7E—937—
The smallest entry in row A is 2, in column C. Circle it, and C joins the tree. Full lesson: Prim’s Algorithm and the Matrix Method

Now you

5723121168ABCDE

Which edge does Kruskal add last?

139514786ABCDE

How many edges does the minimum spanning tree of these five towns have?

Routes that must cover everything

The Chinese postman problem asks for the shortest closed route that uses every edge at least once. If every vertex has even degree, an Eulerian circuit does it. Otherwise pair up the odd vertices, find the shortest route between each pair, and walk those edges twice. With four odd vertices there are three ways to pair them, so cost all three and take the cheapest. See The Chinese Postman Problem.

53462ABCD
B to C direct costs 4, and going around by D costs 6 + 2 = 8, so take the direct street. Full lesson: The Chinese Postman Problem

The traveling salesman problem asks for the cheapest cycle that visits every vertex once. No efficient exact method is known, so the answer is trapped between an upper bound and a lower bound. See The Traveling Salesman Problem.

The nearest-neighbor algorithm builds a tour: start at a vertex, always move to the nearest vertex not yet visited, and return home at the end. The best tour cannot cost more than a tour already in hand, so this cost is an upper bound. See The Nearest-Neighbor Upper Bound.

38694752610ABCDE
Another tour costs only 22, so 28 is an upper bound and not the cheapest tour. Full lesson: The Nearest-Neighbor Upper Bound

For a lower bound, delete one vertex and every edge that touches it, find a minimum spanning tree of what is left, and add back the two cheapest deleted edges. Every tour uses two edges at the deleted vertex and at least a spanning tree of the rest, so no tour can cost less. See The Deleted-Vertex Lower Bound.

38694752610ABCDE
Every tour uses two roads at A, and its other roads join B, C, D and E, which costs at least 11, so no tour can cost under 20. Full lesson: The Deleted-Vertex Lower Bound

How does a Voronoi diagram answer "what is nearest"?

A Voronoi diagram divides the plane into cells, one per site, and every point in a cell is nearer to that site than to any other. The nearest site to any point is then read from its cell, with no distances to compare. The border between sites A and B lies on the perpendicular bisector of AB, because that line holds exactly the points equally far from both. For A at (0, 0) and B at (6, 0), the bisector is the vertical line x = 3. A point at (2, 5) is on A's side: its squared distances are 2² + 5² = 29 to A and 4² + 5² = 41 to B. With three or more sites, each cell is cut down by the bisectors with its neighbors, and cell edges meet at vertices equally far from three sites. Test a point on the border: (3, 4) is 5 from both sites, since 3² + 4² = 25 = 5².

ABCDPwalls: 4 · |PD| = 1.4

each wall of P's cell is the perpendicular bisector of P and one tower — |x − P| = |x − S| on it, |x − P| < |x − S| inside — and the cell touches 4 of the 4

Drag P until it is the same distance from its two nearest towers

AB
The points equally far from A and B make the perpendicular bisector of AB. Full lesson: Voronoi Diagrams

With more sites, draw the bisector of every pair and keep, for each site, the piece of the plane where it is nearest. See Voronoi Diagrams.

ABCD
Each piece is a cell: every point in it is nearer to that site than to any other. Full lesson: Voronoi Diagrams

To find the equation of the edge between A at (1, 2) and B at (5, 4), first find the midpoint of AB, which is (3, 3). The gradient of AB is (4 − 2) ÷ (5 − 1) = 1/2, so the perpendicular gradient is the negative reciprocal, −2. The edge is the line through (3, 3) with gradient −2: y − 3 = −2(x − 3), which rearranges to y = −2x + 9. See The Equation of a Voronoi Edge.

Adding a new site cuts a new cell out of its neighbors, so the cells around it can only shrink. See Finding the Nearest Site.

The toxic waste dump problem asks for the point farthest from every site. Inside a cell you can always step farther from the nearest site, so the answer is at a Voronoi vertex, where three sites are equally near, or on the border of the region. Test every vertex, every corner and every point where a cell edge meets the border, and keep the largest circle that contains no site: its center is the answer. See The Toxic Waste Dump Problem.

3.6ABCD
The circle through those three towns holds no town inside it: an empty circle. Full lesson: The Toxic Waste Dump Problem

A Voronoi cell is a polygon, so its area comes from its corners by ordinary mensuration. See The Area of a Voronoi Cell.

Now you

-2-10123456-2-10123456ABM

What is the equation of the Voronoi edge between A and B?

-2-10123456-2-10123456AB

The gradient of AB is 1/2. What is the gradient of the Voronoi edge?

The mistakes worth naming

Where this leads next

Matrix powers count walks here in the same way they track probabilities in the matrices and Markov chains guide. A Voronoi edge is found with the midpoint and gradient methods of the coordinate geometry guide. The degree count and the Königsberg argument are counting proofs of the kind set out in the logic and proof guide.

Your turn

Three to try — tap what you get.

A triangle graph has how many edges?

A walk that uses every edge exactly once is

A Voronoi cell holds the points nearest to

Questions and answers

What is the difference between an Eulerian and a Hamiltonian path?
An Eulerian trail uses every edge exactly once, and a Hamiltonian path visits every vertex exactly once. The difference matters enormously in practice: whether a graph has an Eulerian trail is settled by counting odd-degree vertices, which takes seconds, while deciding whether a Hamiltonian cycle exists has no known efficient test at all. Two questions that sound like mirror images have completely different difficulty.
When can a network be drawn without lifting your pen?
When it is connected and has either zero or exactly two vertices of odd degree. With no odd vertices you can start anywhere and return to your start, giving a closed circuit. With exactly two, you must start at one odd vertex and finish at the other. Any other number of odd vertices makes it impossible, because every visit to a vertex uses one edge in and one out.
What is a minimum spanning tree used for?
It is the cheapest set of connections that links every point in a network with no redundancy, so it is the answer to questions about laying cable, pipe or road to reach every site at the least total cost. It has one fewer edge than it has vertices and contains no cycles. Kruskal's and Prim's algorithms both find one, and both are greedy, meaning each step takes the cheapest option available without looking ahead.
What is the Chinese postman problem?
It asks for the shortest closed route that travels along every edge of a network at least once, which is the problem faced by a postal round, a gritting lorry or a street inspector. If every vertex has even degree the answer is an Eulerian circuit with no repetition. Otherwise the odd vertices must be paired up and the shortest paths between each pair repeated, so the extra distance is the smallest total of such pairings.
What is a Voronoi diagram?
A Voronoi diagram divides a plane into regions, one per site, so that every point in a region is closer to that site than to any other. The boundary between two neighboring cells is the perpendicular bisector of the segment joining them, because that is precisely the set of points equidistant from both. It answers nearest-facility questions directly: which hospital, which transmitter, which shop.
How do you solve the toxic waste dump problem?
You look at the vertices of the Voronoi diagram, where three cells meet. The point furthest from all sites must be equidistant from three of them, or lie on the boundary of the region under consideration, so checking each Voronoi vertex and the boundary gives the answer. It is called the largest empty circle problem: find the biggest circle containing no site, and its center is the location as far as possible from every one of them.
Mr. Chalk