JB Math Club 26-27 · Lecture notes

Directed Graphs and Tournaments

Strong components, Hamiltonian paths and cycles

Can we visit every player in a tournament by following the results from winner to loser? Can we get back to the start? The difference between these two questions is strong connectivity.

Route through the lecture. We count arrows, identify groups with two-way travel, and build a small strongly connected spanning subgraph. We then use the tournament rule to find a king, construct a path, extend a cycle and recover a ranking.

1. Reading a directed graph

On a map of one-way streets, a route out of a neighbourhood need not give a route back. A tournament result has the same asymmetry: if one player beats another, reversing the arrow changes its meaning.

Definition. A directed graph, or digraph, is a pair $G=(V,E)$. Its vertices are the elements of $V$. An edge $(u,v)\in E$, written $u\to v$, starts at $u$ and ends at $v$. We work with finite graphs without loops or repeated edges. A digraph may contain both $u\to v$ and $v\to u$. An oriented graph allows at most one of these two edges.

The out-degree $d^+(v)$ counts arrows leaving $v$. The in-degree $d^-(v)$ counts arrows entering it. In the figure, three arrows end at vertex $3$, from $1,4,6$. Two leave it, for $2,5$.

Eleven directed edges on eight vertices
Figure 1. Read the arrowheads at vertex $3$. The other edges are shown in grey.

Degree-sum identity. The total in-degree and total out-degree both equal the number of directed edges.

$$\sum_{v\in V} d^-(v)=|E|=\sum_{v\in V}d^+(v).$$

Proof. Pick one edge $u\to v$. Counting tails counts it once, at $u$. Counting heads also counts it once, at $v$. Summing over all edges gives the identity. In Figure 1 both sums are $11$, even though the two degrees at vertex $3$ differ.

Worked example 1. Lakes and rivers

Problem. Alex describes a country with lakes connected by rivers. Exactly $5$ rivers flow out of each lake and exactly $3$ flow into it. Exactly $50$ rivers flow into the sea. Every river starts at a lake and ends at a lake or in the sea, and no river leaves the sea. How many lakes are there?

Choosing the model. Use one vertex for each lake and one for the sea, with each river directed from its source to its endpoint. Count every river separately, even if two rivers share the same endpoints. The degree-sum proof still applies: each river has one start and one end.

Three rivers enter one lake and five leave it
Figure 2. The local degree pattern at one lake. The $50$ rivers ending at the sea are counted across the whole country, not at each lake.

Count starts. Suppose there are $L$ lakes. Each contributes five river starts. The sea contributes none, so there are $5L$ rivers in total.

Count ends. Each lake contributes three river ends. The sea contributes another fifty, so the same total is $3L+50$. Hence

$$5L=3L+50,\qquad 2L=50.$$
$$L=25.$$

Equivalently, each lake has two more outgoing rivers than incoming rivers. Summed over all lakes, the internal rivers cancel: every river from one lake to another contributes one start and one end. The remaining excess is exactly the fifty rivers ending at the sea.

2. Which vertices can reach one another?

A directed path follows the arrows and visits distinct vertices. A single vertex is a path of length zero. Write $u\leadsto v$ if a path goes from $u$ to $v$. A digraph is strongly connected if $u\leadsto v$ for every ordered pair of vertices.

Strongly connected component (SCC). The component containing $v$ consists of the vertices $u$ for which both $v\leadsto u$ and $u\leadsto v$. It is a maximal set with mutual reachability.

To see why, take two such vertices $x,y$. Travel from $x$ through $v$ to $y$, or from $y$ through $v$ to $x$. If a vertex repeats, erase the closed part of the walk. Every vertex on these routes can also get to $v$ and back, so the routes stay inside the component. Mutual reachability is an equivalence relation and its classes partition $V$.

Let $R^+(3)$ be the set reachable from $3$ and $R^-(3)$ the set that can reach $3$. Both include $3$. In Figure 1,

$$\begin{aligned}R^+(3)&=\{1,2,3,5,6,7,8\},\\ R^-(3)&=\{1,3,4,5,6\}.\end{aligned}$$

Their intersection is $\{1,3,5,6\}$. Vertex $3$ reaches $2$ but cannot get back from it. Vertex $4$ reaches $3$ but cannot be reached from $3$.

Eleven directed edges on eight vertices
Figure 3. The directed cycles $1\to3\to5\to6\to1$ and $2\to7\to8\to2$ certify mutual reachability inside the two larger groups. Vertex $4$ forms a component by itself.

A singleton component need not be isolated. Here $4$ has two outgoing edges and no incoming edge. The path of length zero accounts for its reachability from itself.

3. The graph of components

Contract each SCC to one vertex. Draw an arrow from one component to another whenever an original edge crosses in that direction. Ignore edges internal to a component and merge repeated arrows. The resulting graph is called the condensation.

The three strongly connected components in topological order
Figure 4. The edges $4\to3$, $3\to2$ and $4\to8$ account for all three arrows between components.

Theorem. The condensation of any finite digraph is acyclic. Its components admit a topological order, an order in which every arrow points forward.

Proof. Suppose several components formed a directed cycle in the condensation. Within each component we can travel from the head of one crossing edge to the tail of the next. Going around the cycle then gives routes in both directions between its components. They must already be a single SCC, a contradiction.

Every nonempty finite acyclic digraph has a source, a vertex with no incoming edge. Otherwise we could keep following incoming edges backwards. Eventually a vertex would repeat and produce a directed cycle. Put a source first, remove it, and repeat. Each remaining graph is still acyclic, so this constructs the required order. If several sources are available, more than one order may work.

Checking the eight-vertex example

import networkx as nx

G = nx.DiGraph()
G.add_edges_from([(1, 3), (6, 1), (3, 2), (2, 7), (8, 2),
                  (4, 3), (3, 5), (6, 3), (5, 6), (7, 8), (4, 8)])
parts = nx.strongly_connected_components(G)
print(sorted(sorted(part) for part in parts))
# [[1, 3, 5, 6], [2, 7, 8], [4]]

The code requires NetworkX. Sorting here only makes the printed sets easy to compare. The topological order in Figure 4 is $A,B,C$, beginning with $\{4\}$.

4. Keeping strong connectivity with few edges

Problem. Let $G$ be a strongly connected digraph on $n\ge2$ vertices. Prove that $G$ has at least $n$ edges and that it contains a strongly connected spanning subgraph with at most $2n-2$ edges. A spanning subgraph keeps every vertex.

Lower bound. Every vertex must be able to leave for some other vertex, so $d^+(v)\ge1$. Summing gives $|E|\ge n$. A directed cycle through all $n$ vertices attains the bound (use both opposite arrows when $n=2$). For $n=1$, a vertex alone is strongly connected and can have no edges.

Routes out of a root. Fix $A$. For each $v\ne A$, let $d(v)$ be the shortest directed distance from $A$ to $v$. Choose one incoming edge $p(v)\to v$ for which $d(p(v))=d(v)-1$. Following the chosen predecessors backwards decreases $d$ by one until we reach $A$. Thus these $n-1$ selected edges give a path from $A$ to every vertex.

Two rooted trees on the same four verticesFrom A
Two rooted trees on the same four verticesTo A
Figure 5. Two shortest routes reach $D$ from $A$. On the left we choose $B\to D$ as its last edge. On the right the selected arrows all lead towards $A$. Unselected edges are faint.

Routes into the root. For each $v\ne A$, choose one outgoing edge that decreases the shortest distance from $v$ to $A$ by one. This gives another $n-1$ edges, now letting every vertex reach $A$. Equivalently, perform the first construction in the reversed graph and turn its arrows back.

Join the choices. For any $u,v$, go from $u$ to $A$ using the second tree and from $A$ to $v$ using the first. Their union is strongly connected and spans $V$. A shared edge is counted only once.

$$|E_{\mathrm{union}}|\le(n-1)+(n-1)=2n-2.$$

5. Tournaments and the two-step king

Tournament. Every pair of distinct vertices is joined by exactly one directed edge. Think of a round robin with one game per pair, no draws, and an arrow from winner to loser. A tournament on $n$ players has $\binom{n}{2}$ edges.

A king is a vertex that can reach every other vertex by a directed path of length at most two. It need not beat every opponent directly.

Two-step king theorem (Landau). In a nonempty tournament, every vertex of maximum out-degree is a king.

Proof. Choose a player $A$ with the most wins. Let $S$ be the set of players beaten by $A$, and put $k=|S|=d^+(A)$. Every member of $S$ is already one step away. Consider any $X\notin S\cup\{A\}$. Since $A$ does not beat $X$, we have $X\to A$.

A maximum-out-degree vertex A, its out-neighbour set S, and an outside vertex XA two-step route
A maximum-out-degree vertex A, its out-neighbour set S, and an outside vertex XIf no member of S beats X
Figure 6. The shaded region is the arbitrary set $S$ of vertices beaten by $A$; dots and an ellipsis represent its $k$ members. Left: one edge $B\to X$ gives $A\to B\to X$. Right: if no such $B$ exists, $X$ beats all of $S$ and also $A$.

If some $B\in S$ beats $X$, the route $A\to B\to X$ works. Otherwise the tournament condition forces $X\to B$ for every $B\in S$. Together with $X\to A$, this gives

$$d^+(X)\ge k+1>k=d^+(A),$$

contradicting the choice of $A$. Hence every outside opponent is reachable in two steps. The vertices of $S$ are reachable in one step, and $A$ itself in zero steps, so $A$ is a king.

Reverse all arrows. Apply the theorem in the reversed tournament. Back in the original tournament there is a vertex that every other vertex can reach in at most two steps. In particular, one can choose a vertex of maximum in-degree.

6. Visiting every player once

Hamiltonian path theorem. Every nonempty tournament has a directed path that visits each vertex exactly once. Such a path is called Hamiltonian.

Proof by induction. A single vertex is already a Hamiltonian path. For the induction step, remove $x$ and use the hypothesis to put the remaining vertices on a path $v_1\to\cdots\to v_m$. If $x\to v_1$, put $x$ first. If $v_m\to x$, put $x$ last.

Inserting vertex 9 into a directed path of eight vertices
Figure 7. The remaining case for $x=9$. The arrows $1\to9$ and $9\to8$ prevent insertion at either end. Only the edges needed for the argument are drawn.

Otherwise $v_1\to x$ and $x\to v_m$. Walk along the path and let $j$ be the first index such that $x\to v_j$. The last vertex guarantees that $j$ exists, and the first vertex guarantees that $j\ge2$. Since $x$ does not beat $v_{j-1}$, the tournament rule forces $v_{j-1}\to x$.

Inserting vertex 9 into a directed path of eight vertices
Figure 8. With $j=6$, replace the step $5\to6$ by $5\to9\to6$. The edge $5\to6$ still belongs to the tournament. The green route is the new path.
$$v_1\to\cdots\to v_{j-1}\to x\to v_j\to\cdots\to v_m.$$

Every old vertex still occurs once, and $x$ occurs once. The orientations between $x$ and the old path may switch several times. Finding the first suitable index suffices. Consecutive players on the resulting path are in win order, but a later player may have beaten an earlier one.

Worked example 2. Three examinations

Problem. Several students took examinations in mathematics, physics and computer science. In each examination, all scores were different. Prove that the students can be arranged in a line so that every student, except the last, scored higher than the student immediately following them in at least two examinations.

Build the graph from the table. Use one vertex per student. For each pair, compare the scores subject by subject. Draw an arrow from the student with a higher score in at least two subjects to the other student. Here are five students; larger numbers mean higher scores.

StudentMathematicsPhysicsComputer science
$A$$5$$1$$3$
$B$$4$$3$$2$
$C$$3$$5$$1$
$D$$2$$4$$5$
$E$$1$$2$$4$

First pair: $A$ and $B$. In mathematics, $5>4$; in physics, $1<3$; in computer science, $3>2$. Thus $A$ is higher in two of the three subjects, and we draw

$$A\to B.$$

Next pairs. Comparing $A$ with $C$ gives $A\to C$: mathematics and computer science favour $A$. Comparing $A$ with $D$ gives $D\to A$: physics and computer science favour $D$. These are the first three arrows in Figure 9.

Continue with each of the $\binom52=10$ pairs. For example, $E\to A$, since $E$ wins physics and computer science. The pair $D,E$ gives $D\to E$: $D$ is higher in all three subjects, which also satisfies “at least two”. The row order in the score table does not determine the arrow directions.

From the complete tournament to a line

The ten pairwise comparisons give the following outgoing neighbours. Each arrow is recorded once, in its starting student’s row.

StudentStudents beaten in at least two subjects
$A$$B, C$
$B$$C, E$
$C$$D, E$
$D$$A, B, E$
$E$$A$
Tournament on five students; arrows follow majority comparisons of their three scoresFirst three comparisons
Tournament on five students; arrows follow majority comparisons of their three scoresAll ten arrows; a path in green
Figure 9. Left: the first three comparisons give $A\to B$, $A\to C$ and $D\to A$. Right: all ten comparisons, with the Hamiltonian path $D\to A\to B\to C\to E$ highlighted.

Why the method always works. For any pair of students, the three subjects give three votes with no ties. Exactly one student wins at least two votes. Hence there is exactly one directed edge on each pair, so the graph is a tournament. The Hamiltonian-path theorem therefore supplies a path through every student exactly once.

Read that path as a line of students. Each path edge says that the earlier student has higher scores in at least two subjects than the next. These are precisely the consecutive comparisons required by the problem. In our example one such line is

$$D,\ A,\ B,\ C,\ E.$$

A path need not be a perfect ranking. The unused edge $E\to A$ does not invalidate this line: $E$ and $A$ are not consecutive. In fact $A\to B\to E\to A$ is a directed cycle, so majority comparison is not transitive. The path theorem requires no transitivity.

7. Closing a Hamiltonian cycle

Hamiltonian cycle theorem. A tournament with at least three vertices has a directed Hamiltonian cycle if and only if it is strongly connected. A Hamiltonian cycle visits each vertex once and then returns to its start.

Necessity. Travel forward around a Hamiltonian cycle. From any starting vertex you eventually reach every other vertex, so the tournament is strongly connected.

Start the converse. In a strongly connected tournament choose two distinct vertices. Going from the first to the second and back gives a closed directed walk. A shortest nonempty closed segment between repeated vertices is a directed cycle: it has no internal repetition. The tournament rule excludes a cycle of length two. Choose a cycle $C$ of greatest length.

Can one outsider be inserted?

Let $x$ lie outside $C$. Suppose some cycle vertex beats $x$ and $x$ beats another cycle vertex. Start at a cycle vertex that beats $x$ and walk forward to the first vertex $b$ that $x$ beats. Its predecessor $a$ satisfies $a\to x$. Both new arrows are now known.

Extending a directed cycle at the edge from a to b
Figure 10. Replace the one step $a\to b$ by $a\to x\to b$. The dashed edge marks the old step. Every other cycle edge stays in place.

This produces a cycle one vertex longer. Consequently, for a longest cycle, every outside vertex has a uniform relationship to $C$: it beats all of $C$ or loses to all of $C$. We still have to rule out the possibility that all outsiders are of these two types.

When one outsider does not fit

Assume there is an outside vertex. Let $Y$ consist of outsiders that beat all of $C$, and $Z$ of outsiders that lose to all of $C$. Every outsider belongs to exactly one class. Strong connectivity forces both classes to be nonempty. If only $Y$ existed, no edge could leave $C$. If only $Z$ existed, no edge could enter $C$.

Now start in $Z$ and follow a directed path to $C$, which exists by strong connectivity. Consider its first edge leaving $Z$. Its head cannot lie on $C$, because every edge between $C$ and $Z$ points from $C$ to $Z$. The head must lie in $Y$. Thus we have found particular vertices $z\in Z$ and $y\in Y$ with $z\to y$.

Directions between the cycle and the two outside classes
Figure 11. The arrows $Y\to C$ and $C\to Z$ describe every edge between the indicated groups. The return arrow $Z\to Y$ represents one edge $z\to y$ whose existence we just proved.

Pick any consecutive pair $a\to b$ on $C$. From the definitions of $Z$ and $Y$ we know $a\to z$ and $y\to b$. The edge between the two outsiders supplies the missing middle step.

Extending a directed cycle at the edge from a to b
Figure 12. The detour $a\to z\to y\to b$ inserts two distinct outsiders and preserves the rest of the cycle.

The new cycle is two vertices longer than $C$, contradicting our choice of a longest cycle. Hence there are no outside vertices. The cycle $C$ is Hamiltonian.

8. When do all results agree with a ranking?

List the players from first to last. A backward edge means that a lower-ranked player beat a higher-ranked player. A Hamiltonian path guarantees that consecutive results agree with the list. A ranking free of backward edges requires agreement for every pair, including distant positions.

Ranking theorem. A tournament has a ranking with no backward edges if and only if it is acyclic. When this ranking exists, it is unique.

Why cycles obstruct a ranking. If every edge points forward, the position number increases at each step. A directed cycle would require it to increase and return to its initial value. Three players with $A\to B\to C\to A$ already exhibit the obstruction: $A,B,C$ is a Hamiltonian path, but $C$ beat $A$.

Ranking four players when every result points forward
Figure 13. All arrows into $D$ point towards the last player. After deleting $D$, player $C$ becomes the next sink.

Build an order when there is no cycle. Starting at any vertex, keep following an outgoing arrow. Since no vertex can repeat, this must end at a sink, a vertex with out-degree zero. Every remaining player beat that sink, so its only possible position is last. Delete it and repeat in the smaller tournament, which is still acyclic. Reading the removed vertices backwards gives the ranking.

Why it is unique. Two distinct sinks cannot coexist in a tournament: the edge between them leaves one of them. Thus the last place is forced at every deletion. Equivalently, each pairwise result already prescribes which of the two players must come first.

For Figure 13 the deletion order is $D,C,B,A$, giving the ranking $A,B,C,D$. In a cyclic tournament at least one backward edge is unavoidable in every ranking.

Worked example 3. Count paths of length three

Problem. An acyclic tournament has $n\ge3$ vertices. How many directed paths of length $3$ does it contain? A path of length $3$ has three edges and visits four distinct vertices. Give a formula in $n$.

Use the ranking. By the ranking theorem, the vertices have a unique order $v_1,\ldots,v_n$ in which every edge points forward. A directed path must therefore visit vertices in increasing order of their indices.

Choose the vertices. Select any four indices $i_1<i_2<i_3<i_4$. The tournament contains every forward edge, so they give the path

$$v_{i_1}\to v_{i_2}\to v_{i_3}\to v_{i_4}.$$
A transitive six-vertex tournament and a selected three-edge path
Figure 14. With six vertices, the choice $\{1,3,4,6\}$ gives the green path $1\to3\to4\to6$. The selected vertices need not occupy consecutive places in the ranking.

Conversely, every length-three path has four distinct vertices, and the forward order is the only order in which those four can form a directed path. Thus choosing four vertices and choosing a path are equivalent.

$$\#\text{paths}=\binom n4=\frac{n(n-1)(n-2)(n-3)}{24}.$$

For $n=6$, this gives $\binom64=15$ paths. For $n=3$, the answer is zero. A path may ignore the extra edges between its nonconsecutive vertices; it need not be an induced path.

Follow-up. More generally, a path of length $r$ uses $r+1$ vertices. The same reasoning gives $\binom n{r+1}$ directed paths for $0\le r\le n-1$. There are none when $r\ge n$.