Version: 0.6.22
Problem Set#
- Objectives:
Practice the concepts introduced in the Graphs module
This problem set is optional practice. Work on it wherever you like, alone or in groups, and with any resources you find useful. Unless a problem says otherwise, answer with pen and paper or in pseudocode; implementation tasks can be done in any programming language.
Problems are roughly ordered by difficulty, from ★ (warm-up) to ★★★★ (challenging). The last problem introduces a topic that is not covered in the lectures: It starts with a short background that gives you everything you need.
Every problem comes with a solution, hidden in a dropdown below it. Give the problem a real try before you open it: Most of the learning happens while you are stuck.
The set closes with three case studies. These are not exercises with a single right answer, but open-ended explorations of famous graph problems: Easy to state, sometimes very hard (or even impossible, as far as anyone knows) to solve. Go as far as your curiosity takes you. Instead of a solution, each one comes with a dropdown of resources and ideas to help you dig further.
Core Problems#
Exercise 1 (Handshakes ★)
Seven people meet at a party, and some of them shake hands. We model the party as a simple graph, where vertices are people and an edge means that two people shook hands, as shown on Fig. 26. Recall from Lecture 6.1 that the degree of a vertex is the number of edges incident to it.
Fig. 26 Who shook hands with whom#
Give the degree of every vertex. Add them up, and compare the sum with the number of edges. Explain why, in any simple graph \(G = (V, E)\), we have
\[\sum_{v \in V} \mathrm{degree}(v) = 2 \cdot |E|\]Deduce that every graph has an even number of vertices with an odd degree. Check it on the party. Could seven people each shake hands with exactly three others?
At the next party, everyone shakes hands with everyone else exactly once. Which graph from the lecture’s jargon is this? How many handshakes happen with seven people? With \(n\) people?
Now consider a directed graph, for instance the food web of Fig. 1.31. What becomes of the formula from Question 1 when we count in-degrees and out-degrees?
Show that at any party with at least two people, two people shook the same number of hands.
Hint: With \(n\) people, which degrees are possible? Can someone who shook no hands be at the same party as someone who shook everyone’s hand?
Solution to Exercise 1 (Handshakes ★)
The degrees are:
Vertex
A
B
C
D
E
F
G
Degree
3
2
3
2
3
2
1
They add up to 16, twice the 8 edges. This holds in general because every edge \(\{u, v\}\) has exactly two ends: It counts once in the degree of \(u\) and once in the degree of \(v\). Summing all the degrees counts every edge exactly twice. This result is known as the handshake lemma.
Split the sum of the degrees into the vertices of even degree and those of odd degree. The total is even, and so is the sum over the even degrees. The sum over the odd degrees must therefore be even too, and a sum of odd numbers is even only if there is an even number of them. At the party, A, C, E, and G have an odd degree: Four of them.
Seven people with three handshakes each would give a sum of degrees of \(7 \cdot 3 = 21\), which is odd. This is impossible: It would require 10.5 handshakes.
Every pair of vertices is adjacent: This is the complete graph, often written \(K_n\). Each of the \(n\) people shakes \(n - 1\) hands, so the sum of the degrees is \(n(n-1)\) and there are \(n(n-1)/2\) handshakes, that is 21 for seven people. This is also the largest number of edges a simple graph with \(n\) vertices can have.
Every arc \((u, v)\) adds one to the out-degree of \(u\) and one to the in-degree of \(v\). Therefore:
\[\sum_{v \in V} \mathrm{in}(v) = \sum_{v \in V} \mathrm{out}(v) = |E|\]In the food web, there are 9 arcs, so both sums equal 9.
With \(n\) people, the possible degrees are \(0, 1, \ldots, n - 1\): That is \(n\) values for \(n\) people, which is not enough to force a repetition yet. But a person with degree 0 and a person with degree \(n - 1\) cannot both be at the party: The second one shook everyone’s hand, including the first one’s. So at most \(n - 1\) values are actually used by \(n\) people, and by the pigeonhole principle, two people share the same degree.
Common mistakes
Counting each edge once, and concluding that the sum of the degrees equals \(|E|\).
Answering \(n^2\) or \(n(n-1)\) handshakes in Question 3, forgetting that “A shakes B’s hand” and “B shakes A’s hand” are the same handshake.
In Question 5, stopping at “\(n\) possible values for \(n\) people”, which does not force a repetition on its own.
Exercise 2 (Reading the Matrix ★)
Lecture 6.1 describes a graph by its adjacency matrix: The cell \(a_{ij}\) holds the number of edges from vertex \(i\) to vertex \(j\). Consider the following two matrices, where the vertices are ordered alphabetically, \((A, B, C, D, E)\) and \((P, Q, R, S)\):
Draw the graph of \(A\). Without drawing anything, how can you tell from the matrix alone that it is undirected, and that it has no loop? How do you read the degree of a vertex from the matrix?
Draw the graph of \(M\). What do the sum of a row and the sum of a column tell you? Which features of \(M\) prevent it from describing a simple graph? What would an entry equal to 2 mean?
Compute the matrix product \(A^2 = A \times A\). What does the entry in row B, column D count? What about the entries on the diagonal?
Hint: Write down the formula for the entry \((i, j)\) of the product, \(\sum_k a_{ik} \cdot a_{kj}\), and ask when a term \(a_{ik} \cdot a_{kj}\) equals 1.
Generalise: What does the entry \((i, j)\) of \(A^k\) count? Use \(A^3\) to count the triangles (three vertices that are all adjacent to each other) in the graph of \(A\).
Hint: You only need the diagonal of \(A^3\). How many times does each triangle show up there?
Solution to Exercise 2 (Reading the Matrix ★)
The graph of \(A\) has six edges: \(\{A,B\}\), \(\{A,E\}\), \(\{B,C\}\), \(\{B,E\}\), \(\{C,D\}\), and \(\{D,E\}\).
Fig. 27 The graph of matrix \(A\)#
The matrix is symmetric (\(a_{ij} = a_{ji}\)): Whenever there is an edge from \(i\) to \(j\), there is one from \(j\) to \(i\), so we can read every pair as an undirected edge. Its diagonal holds only zeros, so no vertex is adjacent to itself. The degree of a vertex is the sum of its row (or column): A, C, and D have degree 2, B and E have degree 3. The total is 12, twice the 6 edges, as Exercise 1 predicts.
The graph of \(M\) has seven arcs: \(P \to Q\), \(P \to R\), \(Q \to R\), \(R \to P\), \(R \to R\), \(R \to S\), and \(S \to Q\).
Fig. 28 The graph of matrix \(M\), with a loop on R#
The sum of a row gives the out-degree of its vertex (P: 2, Q: 1, R: 3, S: 1), and the sum of a column its in-degree (P: 1, Q: 2, R: 3, S: 1). Both total 7, the number of arcs.
\(M\) is not symmetric (there is an arc \(Q \to R\) but none \(R \to Q\)), so the graph is directed. The 1 on the diagonal is a loop on R. An entry equal to 2 would mean two parallel edges between the same vertices: The graph would be a multigraph.
The product gives:
\[\begin{split}A^2 = \begin{bmatrix} 2 & 1 & 1 & 1 & 1 \\ 1 & 3 & 0 & 2 & 1 \\ 1 & 0 & 2 & 0 & 2 \\ 1 & 2 & 0 & 2 & 0 \\ 1 & 1 & 2 & 0 & 3 \end{bmatrix}\end{split}\]A term \(a_{ik} \cdot a_{kj}\) is 1 exactly when there is an edge from \(i\) to \(k\) and one from \(k\) to \(j\). The entry \((i, j)\) of \(A^2\) therefore counts the walks of length 2 from \(i\) to \(j\), that is, in an undirected graph, the common neighbours of \(i\) and \(j\). The entry (B, D) is 2 because of the walks B–C–D and B–E–D. On the diagonal, a walk of length 2 from \(i\) back to \(i\) goes to a neighbour and returns, so the diagonal holds the degrees.
By the same argument, applied \(k - 1\) times, the entry \((i, j)\) of \(A^k\) counts the walks of length \(k\) from \(i\) to \(j\).
In a simple graph, a walk of length 3 from \(i\) back to \(i\) must visit two other vertices that are adjacent to \(i\) and to each other: It goes around a triangle. Each triangle appears 6 times on the diagonal: Once for each of its three vertices, in each of the two directions. The diagonal of \(A^3\) is \((2, 2, 0, 0, 2)\), so the graph has \(6 / 6 = 1\) triangle, namely A, B, E.
Common mistakes
Reading a non-symmetric matrix as an undirected graph, and drawing a single line for \(R \to P\) and \(P \to R\) but also one for \(Q \to R\) alone.
Mixing up rows and columns, which reverses every arc of \(M\). With the lecture’s convention, row \(i\) lists the arcs leaving \(i\).
Thinking that \(A^2\) counts paths of length 2. Walks may repeat vertices: The diagonal entries are walks such as A–B–A.
Dividing the trace of \(A^3\) by 3 instead of 6, forgetting that each triangle can be walked in two directions.
Exercise 3 (Mind the Map ★)
In 1933, Harry Beck redrew the map of the London Underground. The new map ignored geography: Stations sit on a grid, lines only run horizontally, vertically, or at 45°, and the distances between stations are all roughly equal. It was a huge success, and most subway maps today, including the one of Oslo shown on Fig. 1.29, follow the same idea. A subway map is really a drawing of a graph.
What information does such a map keep, and what does it throw away? Give at least two advantages and two drawbacks of drawing a transit network as a graph rather than as a geographic map.
Consider now the fictional network shown on Fig. 29, with three lines: Red (A–B–C–D–E), Green (A–F–J), and Blue (G–F–E–H). Riding between two neighbouring stations takes 2 minutes, and changing lines at a transfer station (double circles) takes 5 minutes.
Fig. 29 A fictional subway with three lines: Red (solid), Green (dashed), and Blue (dotted)#
Model the network as a simple graph whose vertices are the stations. Which algorithm finds the route from A to E with the fewest stops? Which route does it find, and how long does that trip really take? Is there a faster one?
The station graph forgets which line an edge belongs to, so it cannot account for transfers. Design a better graph, where every vertex is a platform, that is, a pair (station, line). Which edges does it need, and with which weights? Use it to find the quickest trip from A to E, and from A to H. Which algorithm do you use?
Hint: You may start your trip at A on any line, without paying for a transfer. How can the graph capture that?
Real networks raise more modelling questions. For each of the following situations, explain how you would change the graph:
a circle line that runs in one direction only;
two lines that share the same track between two stations;
two stations that are far apart on the map, but a two-minute walk apart in the street;
waiting for the next train, which depends on how often each line runs.
Solution to Exercise 3 (Mind the Map ★)
The map keeps the topology of the network: Which stations exist, in which order they come along each line, and where lines meet. It throws away the geometry: Real distances, directions, and the shape of the city.
Advantages:
It is much easier to read. Stations in the crowded centre get as much room as those in the suburbs.
Planning a trip becomes a graph problem: Follow the lines and count the stops, or look for a path between two vertices.
The map stays valid when the city changes, as long as the network does not.
Drawbacks:
Distances on the map do not match distances in reality. Two stations can look far apart and be a short walk away. In London, Leicester Square and Covent Garden are only a few hundred metres apart, yet many tourists ride the train between them.
Travellers trust the map more than they should. A study of London trips (Guo, 2011) found that the distance on the map influences the route people choose about twice as much as the real travel time.
It hides everything that is not part of the graph: Walking connections, travel times, how long a transfer takes, or how often trains run.
The station graph has 9 vertices and 9 edges: A–B, B–C, C–D, D–E, A–F, F–J, G–F, F–E, and E–H. Every edge counts the same, so a breadth-first search from A finds the route with the fewest stops: A–F–E, with 2 stops. But it changes lines at F (Green to Blue), so the trip takes \(2 + 5 + 2 = 9\) minutes. Staying on the Red line, A–B–C–D–E takes \(4 \cdot 2 = 8\) minutes: It has more stops, but no transfer.
The platform graph has one vertex per pair (station, line): A·R, A·G, B·R, C·R, D·R, E·R, E·B, F·G, F·B, G·B, H·B, and J·G. It has two kinds of edges:
ride edges, of weight 2, between consecutive platforms of the same line (e.g., A·R–B·R);
transfer edges, of weight 5, between the platforms of a same station (A·R–A·G, F·G–F·B, and E·R–E·B).
Starting the trip at A·R or at A·G is free, so we add a vertex A* linked to both with edges of weight 0 (and the same for the destination). Without it, running the search from A·G would charge a transfer to any trip on the Red line.
Fig. 30 The platform graph. Dashed edges are transfers, and dotted edges of weight 0 connect the entry vertex A* and the exit vertex E*.#
All weights are non-negative, so Dijkstra’s algorithm applies. From A to E, it finds the Red line (8 minutes) rather than the route through F (9 minutes). From A to H, it finds Green to F, then Blue to H: \(2 + 5 + 2 + 2 = 11\) minutes, against \(8 + 5 + 2 = 15\) minutes on the Red line with a transfer at E.
Some possible answers:
A one-way circle line needs directed edges, at least for that line.
Two lines on the same track give two edges between the same stations, one per line (a multigraph). In the platform graph, this is no problem: Each line has its own platforms, so there are two separate edges.
A short walk is an extra edge between the two stations, weighted by the walking time. It does not belong to any line.
The waiting time depends on the line you board, not on the station. In the platform graph, we can add it to the edges that enter a line: The weight-0 entry edges and the transfer edges. Frequent lines then get cheaper transfers.
Common mistakes
Assuming that the route with the fewest stops is the fastest one. BFS minimises the number of edges, not the travel time.
In the platform graph, starting from a single platform of A, which either forbids or overcharges half of the trips.
Reading the drawbacks of the map as “the map is wrong”. The map is a faithful drawing of the graph: The graph itself is what leaves out the geography.
Exercise 4 (Lost in Traversal ★★)
Consider the undirected graph shown on Fig. 31.
Fig. 31 An undirected graph with ten vertices#
Run the depth-first and the breadth-first traversals from Lecture 6.1, starting from A, and give the order in which they visit the vertices. When several neighbours are pending, visit them in alphabetical order (for the depth-first traversal, push them on the stack in reverse alphabetical order, as in the lecture’s example).
For each traversal, draw its traversal tree: The tree whose edges are the ones through which each vertex was first reached. Add the other edges of the graph as dashed lines.
Look at the dashed edges of each tree. In the breadth-first tree, how far apart can the two ends of a dashed edge be, in levels? In the depth-first tree, is one end of a dashed edge always an ancestor of the other? Explain why.
Neither traversal reaches H, I, or J. Write a procedure
components(graph)that returns the number of connected components of an undirected graph, in \(O(|V| + |E|)\) time.A classmate suggests a way to detect cycles: “Run a traversal, and as soon as we meet a vertex that is already visited, there is a cycle”. Run their idea on the component {H, I, J}, which is a tree. What goes wrong? Write a procedure
hasCycle(graph)that works for any undirected graph, connected or not.Does your procedure still work on a directed graph? Find a small directed graph with no cycle on which it reports one.
Solution to Exercise 4 (Lost in Traversal ★★)
The breadth-first traversal visits A, B, C, D, E, F, G. The depth-first traversal visits A, B, C, D, F, E, G: From A it goes to B, then to C (the first unvisited neighbour of B), then to D, F, and E. At E, every neighbour is visited, so it backtracks to F and visits G.
Fig. 32 The breadth-first tree (left) and the depth-first tree (right) from A. Dashed lines are the edges outside the tree.#
In the breadth-first tree, the dashed edges are B–C (both on level 1), C–D (levels 1 and 2), and E–F (levels 2 and 3): Their ends are on the same level or on adjacent levels. This always holds. Assume an edge \(\{u, v\}\) with \(u\) on level \(k\). When the traversal processes \(u\), it adds \(v\) to the pending vertices, so \(v\) ends up on level \(k + 1\) at the latest. It cannot be on a level smaller than \(k - 1\) either, or the same argument applied to \(v\) would put \(u\) on a level smaller than \(k\).
In the depth-first tree, the dashed edges are A–C, B–D, and C–E: Each one connects a vertex with one of its ancestors. This also always holds in an undirected graph. Assume an edge \(\{u, v\}\), where \(u\) is visited first. The traversal does not leave \(u\) for good before it has explored everything it can reach from \(u\) through unvisited vertices, and \(v\) is one of them. So \(v\) gets visited while \(u\) is still “open”, that is, as a descendant of \(u\).
We start a new traversal from every vertex that is still unvisited. Each start discovers exactly one new component:
components(graph): visited = empty set count = 0 for each vertex v in graph: if v not in visited: count = count + 1 explore(graph, v, visited) return count explore(graph, v, visited): add v to visited for each neighbour w of v: if w not in visited: explore(graph, w, visited)The graph has 2 components. Every vertex is explored once, and every edge is looked at twice (once from each end), so the total is \(O(|V| + |E|)\), even though there is a loop around the traversal.
Starting from H, the traversal visits H, then I. From I, it sees its neighbour H, which is already visited, and wrongly reports a cycle. In an undirected graph, every edge can be followed both ways: The edge we just came through always leads back to a visited vertex. We must ignore the vertex we came from:
hasCycle(graph): visited = empty set for each vertex v in graph: if v not in visited: if cycleFrom(graph, v, null, visited): return true return false cycleFrom(graph, v, parent, visited): add v to visited for each neighbour w of v: if w not in visited: if cycleFrom(graph, w, v, visited): return true else if w ≠ parent: return true return falseOn the component {A, …, G}, it returns true when C sees A (C was reached from B). On {H, I, J}, it returns false.
It does not. Consider \(A \to B\), \(A \to C\), and \(B \to C\). There is no directed cycle, but the traversal from A visits B, then C, and when it comes back to A it sees C again, already visited, and reports a cycle. In a directed graph, reaching a visited vertex only means that two paths lead to it. A real cycle needs an arc back to a vertex that is still open, that is, on the current path of the depth-first traversal. The usual fix gives every vertex three states: Unvisited, open, and closed (all descendants explored), and reports a cycle only when an arc leads to an open vertex.
Common mistakes
Pushing neighbours in alphabetical order on the stack, which makes the depth-first traversal visit them in reverse order (A, C, E, F, G, D, B).
Drawing the breadth-first tree with one edge per pending vertex, instead of one edge per first visit. Each vertex has exactly one parent.
Forgetting the outer loop over all vertices, so that
componentsalways returns 1 andhasCyclemisses the cycles of the other components.Ignoring all visited neighbours in
hasCycle, instead of only the parent, which never finds any cycle.
Exercise 5 (Pick Your Path ★★)
Lecture 6.2 presents four shortest-path algorithms: Breadth-first search (BFS), Dijkstra, Bellman-Ford, and Floyd-Warshall. This problem is about choosing between them, not about running them.
For each of the following situations, pick the algorithm you would use, and justify your choice. Estimate the number of basic operations it needs.
A social network with \(10^8\) users and \(10^{10}\) friendships wants to tell you how many “handshakes” separate you from another user.
A car navigation app looks for the quickest route from your position to a destination, on a road network with \(10^6\) junctions and \(2.5 \cdot 10^6\) road segments, weighted by their travel times.
A ferry company serves 15 ports and prints, in its timetable, the quickest connection between every pair of ports.
An electric car spends energy driving uphill, but recovers some when it drives downhill (regenerative braking). We weigh each road segment by the energy it costs, which is negative downhill, and look for the route from Trondheim to Bergen that drains the battery the least.
In the subway of Exercise 3, a traveller does not care about time, but wants as few transfers as possible.
An online game wants the number of hops between every pair of its 500 servers, which are connected by 2000 links.
All four algorithms rely on relaxation, but they relax the edges in different orders. For each algorithm, explain in one or two sentences in which order it relaxes the edges, and why this order is enough to find the shortest paths.
Run Dijkstra’s algorithm on a graph where every edge has weight 1. Show that its priority queue then behaves exactly like the plain queue of BFS: Vertices come out in the same order.
Hint: Show that, at any time, the priorities in the queue differ by at most 1, and that new vertices always enter with the largest priority.
A classmate argues: “Bellman-Ford handles everything Dijkstra handles, and more. Floyd-Warshall even gives all the pairs at once. Why not always use Floyd-Warshall?” Answer them, using Situation b as an example. Assume a computer that relaxes \(10^8\) edges per second.
Solution to Exercise 5 (Pick Your Path ★★)
Possible answers:
BFS. The graph is unweighted, and we need a single source. BFS takes \(O(|V| + |E|)\), about \(10^{10}\) operations in the worst case. In practice, we can stop as soon as we reach the other user, and running two searches, one from each end, until they meet (bidirectional search) explores far fewer vertices.
Dijkstra. Travel times are positive, and we need a single source. With a binary heap, it takes \(O((|V| + |E|) \log |V|)\), about \(3.5 \cdot 10^6 \cdot 20 = 7 \cdot 10^7\) operations, under a second. We can stop as soon as the destination leaves the queue.
Floyd-Warshall. We need all the pairs, and the graph is tiny: \(15^3 = 3375\) operations. Running Dijkstra from every port would work too, but Floyd-Warshall is simpler.
Bellman-Ford. Some weights are negative, so Dijkstra is not safe. There is no negative cycle, though: Driving around a loop and back to where you started cannot recharge the battery, because some energy is always lost. With about \(10^6\) junctions in Norway, \(O(|V| \cdot |E|)\) is large, about \(2.5 \cdot 10^{12}\), but stopping early when a round changes nothing usually helps a lot.
Dijkstra, on the platform graph of Exercise 3, with weight 1 on transfer edges and 0 on rides. The weights are not all equal, so plain BFS does not apply, but they are non-negative. (A variant of BFS that uses a double-ended queue, called 0-1 BFS, handles weights 0 and 1 in linear time.)
BFS from every server. The graph is unweighted and sparse. 500 runs of BFS take about \(500 \cdot (500 + 2000) \approx 1.25 \cdot 10^6\) operations, against \(500^3 = 1.25 \cdot 10^8\) for Floyd-Warshall. Both are fast here, but the gap grows with the size of the network.
BFS processes the vertices in order of their number of edges from the source, and relaxes the edges of each one as it leaves the queue. With equal weights, a vertex reached through fewer edges is always closer.
Dijkstra processes the vertices in order of their distance, always taking the closest one that is not finished. With non-negative weights, no later vertex can lead to a shorter path, so each vertex is final when it leaves the queue, and its edges are relaxed once.
Bellman-Ford does not choose: It relaxes every edge, in any order, \(|V| - 1\) times. After round \(k\), all shortest paths with at most \(k\) edges are correct, and a shortest path never needs more than \(|V| - 1\) edges.
Floyd-Warshall relaxes all pairs \((i, j)\) through one intermediate vertex \(k\) at a time. After step \(k\), the distances are correct for all paths whose intermediate vertices are among the first \(k\).
With weights 1, a vertex enters the queue (or gets a new priority) with \(d + 1\), where \(d\) is the priority of the vertex being processed. Assume that, before an extraction, all priorities in the queue lie between \(d\) and \(d + 1\), where \(d\) is the smallest one. We extract a vertex with priority \(d\), and its neighbours get priority \(d + 1\), the largest value in the queue. The property therefore still holds after the extraction. (A vertex already in the queue never improves: It has priority \(d\) or \(d + 1\) already.) Since new vertices always enter with the largest priority, “smallest priority first” is the same as “first in, first out”. BFS is Dijkstra, with a cheaper queue.
On Situation b, Bellman-Ford needs about \(|V| \cdot |E| = 2.5 \cdot 10^{12}\) relaxations, that is about 7 hours, against less than a second for Dijkstra. Floyd-Warshall needs \(|V|^3 = 10^{18}\) relaxations, more than 300 years. It also needs a matrix of \(|V|^2 = 10^{12}\) distances, that is terabytes of memory. A more general algorithm is not a better one: Pick the cheapest algorithm whose assumptions hold.
Common mistakes
Choosing Dijkstra for Situation d “because Dijkstra handles weighted graphs”, without checking the sign of the weights.
Choosing Bellman-Ford for Situation d “because it detects negative cycles”: Here, the point is that the weights are negative, and there are no negative cycles.
In Question 2, describing what each algorithm computes rather than the order in which it relaxes the edges.
Ignoring the size of the graph. \(O(|V|^3)\) sounds harmless until \(|V| = 10^6\).
Exercise 6 (When Dijkstra Lies ★★)
Lecture 6.2 warns that Dijkstra’s algorithm fails with negative weights, and that negative cycles break the very notion of shortest path. Let us look at both warnings more closely.
Assume that, once Dijkstra’s algorithm extracts a vertex from the queue, its distance is final and never changes again. Find a directed graph with as few vertices as possible on which the algorithm returns a wrong distance. Which step of the correctness proof in the lecture breaks?
The pseudocode of the lecture keeps relaxing edges towards vertices that are already extracted, so it may still fix your example. Find a graph on which even that version returns a wrong distance.
Consider the graph shown on Fig. 33. For each vertex, give its shortest distance from S, which is either a number, \(-\infty\), or \(+\infty\) (unreachable). What does the Bellman-Ford algorithm of the lecture report from S?
Fig. 33 A directed graph with negative weights#
Describe how to extend Bellman-Ford so that it computes the three kinds of answers, for every vertex, instead of stopping at “negative cycle detected”.
A classmate wants to find shortest paths with negative weights in an undirected road network, by replacing every undirected edge with two arcs, one in each direction, and running Bellman-Ford. Why is this hopeless as soon as a single edge has a negative weight?
Another classmate wants to get rid of negative weights altogether: “Add the same constant to every edge, large enough to make all the weights non-negative, then run Dijkstra”. Show, with a small example, that this does not work. Why does it fail?
Here is a trick that does work. Give every vertex \(v\) a number \(h(v)\), and replace the weight of each arc \((u, v)\) with
\[w'(u, v) = w(u, v) + h(u) - h(v)\]Show that, for any two vertices \(s\) and \(t\), every path from \(s\) to \(t\) changes by the same amount. Deduce that the shortest paths stay the same.
Hint: Write the new length of a path \(s = v_0, v_1, \ldots, v_k = t\), and look at what happens to the \(h\) terms.
Solution to Exercise 6 (When Dijkstra Lies ★★)
Three vertices are enough: \(S \to A\) (2), \(S \to B\) (3), and \(B \to A\) (−2). The algorithm extracts S, sets \(\mathrm{dist}[A] = 2\) and \(\mathrm{dist}[B] = 3\), then extracts A, which is closer, and finalises it at 2. When it finally extracts B, the path \(S \to B \to A\) of length 1 comes too late. Two vertices are not enough: With only S and A, the arc \(S \to A\) is the only path.
Fig. 34 (a) A is finalised at 2, but \(S \to B \to A\) has length 1. (b) Even if \(\mathrm{dist}[A]\) is corrected to 1, T keeps the stale value 3 instead of 2.#
The proof breaks at the inequality “\(d(v) + (\text{path from } v \text{ to } u) \geq d(v)\)”: It assumes that extending a path can only make it longer. Here, the path through B is longer than 2 until it takes the negative arc.
If the algorithm keeps updating the distances of extracted vertices, example (a) gets fixed when B relaxes \(B \to A\). But A has already been processed with its old distance, so everything after A keeps a stale value. Adding an arc \(A \to T\) (1) breaks it: T gets 3 from \(A\), and is never updated, while the true distance is 2.
A, B, and C lie on the cycle \(A \to B \to C \to A\), of weight \(1 - 4 + 2 = -1\), which S can reach. Every extra turn around the cycle shortens the path, so their distance is \(-\infty\), and so is the distance of F, which the cycle reaches. D and E are not reachable from the cycle: Their distances are 3 and 1. G cannot be reached from S at all, so its distance is \(+\infty\).
Vertex
S
A
B
C
D
E
F
G
Distance
0
\(-\infty\)
\(-\infty\)
\(-\infty\)
3
1
\(-\infty\)
\(+\infty\)
The lecture’s Bellman-Ford reports “negative cycle detected”, and nothing else: The correct distances of D and E get lost.
To get all three answers, run the \(|V| - 1\) rounds as usual, then one more round, and mark every vertex whose distance still decreases. Each marked vertex can be reached from a negative cycle, and each negative cycle reachable from S has at least one marked vertex. Then run a traversal (see Exercise 4) from all the marked vertices: Everything it reaches gets \(-\infty\). The other vertices keep their distance, which is either correct or still \(+\infty\).
An undirected edge \(\{u, v\}\) of weight \(w < 0\) becomes two arcs \(u \to v\) and \(v \to u\), which form a cycle of weight \(2w < 0\). Every vertex that can reach \(u\) then has distance \(-\infty\): We can walk back and forth along the edge forever. In an undirected graph, a negative edge is a negative cycle. Negative weights only make sense for shortest paths in directed graphs.
Take \(S \to T\) (1), \(S \to A\) (−2), and \(A \to T\) (2). The shortest path from S to T goes through A, with length 0. Adding 2 to every edge gives \(S \to T\) (3), \(S \to A\) (0), and \(A \to T\) (4). Now the direct arc (3) beats the path through A (4), and Dijkstra returns the wrong path. Adding a constant \(c\) to every edge adds \(k \cdot c\) to a path with \(k\) edges: Paths with many edges are penalised more than short ones, which changes the order between them.
The new length of a path \(s = v_0, v_1, \ldots, v_k = t\) is:
\[\sum_{i=1}^{k} \big( w(v_{i-1}, v_i) + h(v_{i-1}) - h(v_i) \big) = \sum_{i=1}^{k} w(v_{i-1}, v_i) + h(s) - h(t)\]The \(h\) terms telescope: Each intermediate vertex appears once with a plus and once with a minus. Every path from \(s\) to \(t\) thus changes by the same amount \(h(s) - h(t)\), whatever its number of edges, and the shortest path stays the shortest.
To make every new weight non-negative, we can add a vertex \(q\) with an arc of weight 0 to every other vertex, run Bellman-Ford from \(q\) once, and take \(h(v) = \mathrm{dist}(q, v)\). Since \(h(v) \leq h(u) + w(u, v)\) for every arc, all \(w'(u, v) \geq 0\), and we can then run Dijkstra from every vertex. This is Johnson’s algorithm, which computes all pairs of shortest paths faster than Floyd-Warshall on sparse graphs.
Common mistakes
Concluding from Question 1 that Dijkstra always fails with negative weights. It may fail; on many graphs it still returns the right answer.
Giving \(-\infty\) to every vertex once a negative cycle exists. Only the vertices that can be reached from the cycle are affected.
Confusing \(+\infty\) (no path) with \(-\infty\) (paths as short as we want).
In Question 5, assuming that \(h(s) - h(t)\) must be 0. It does not matter: It is the same for all paths between the same two vertices.
Exercise 7 (Prim and Kruskal by Hand ★★)
Consider the weighted graph shown on Fig. 35.
Fig. 35 A weighted graph with six vertices#
Trace Prim’s algorithm, starting from A. List the edges in the order in which the algorithm adds them to the tree.
Trace Kruskal’s algorithm. List the edges in the order in which it adds them, and say which ones it skips.
Do the two algorithms produce the same tree? Why, or why not?
Solution to Exercise 7 (Prim and Kruskal by Hand ★★)
Starting from A, the edges leaving the tree are AB (6) and AC (4), so Prim’s algorithm takes AC. It then takes the cheapest edge leaving {A, C}, which is CD (2), then BD (3), then DE (5), and finally EF (7). The edges are added in this order:
AC (4), CD (2), BD (3), DE (5), EF (7).
Kruskal’s algorithm goes through the edges in increasing order of weight. It adds CD (2), BD (3), AC (4) and DE (5). It then skips AB (6), because A and B are already connected, through C and D: adding it would create a cycle. It adds EF (7), which gives the five edges we need for six vertices. The two remaining edges, CF (8) and BE (9), would both be skipped.
Yes, they produce the same tree, which weighs 21 and is shown on Fig. 36. They only differ in the order in which they add the edges. This is no coincidence: all the weights in this graph are different, and then there is only one minimum spanning tree (see the last problem). Both algorithms must find it.
Fig. 36 The minimum spanning tree (in bold), with a total weight of 21#
Exercise 8 (Cheapest Cables ★★★)
A company wants to connect six offices with network cables. The possible cables and their costs are shown on Fig. 37. The cheapest network that connects all the offices is a minimum spanning tree (MST), as explained in Lecture 6.3.
Fig. 37 Possible cables between six offices, with their costs#
Run Prim’s algorithm from A, then Kruskal’s algorithm. Give the edges in the order in which each algorithm adds them, and the total cost. Do they find the same tree? How many different MSTs does this graph have?
The tree connects A and D. Is the path between them in the tree a shortest path from A to D in the graph? What does this tell you about the difference between an MST and the shortest paths of Lecture 6.2?
Show that, when all the weights are different, the graph has exactly one MST.
Hint: Assume two different MSTs \(T_1\) and \(T_2\), and consider the lightest edge \(e\) that belongs to one of them but not to the other. What happens if you add \(e\) to the other tree?
The lecture states the cycle property: The heaviest edge of a cycle cannot belong to an MST (if it is the only heaviest one). This suggests a third algorithm, sometimes called reverse-delete: Go through the edges from the heaviest to the lightest, and delete each one, unless deleting it would disconnect the graph. Run it on the offices. Explain why the result is an MST, and give its time complexity. How does it compare with Kruskal’s?
The network is built. A supplier now offers a new cable between two offices \(u\) and \(v\), at cost \(w\). Explain how to update the MST in \(O(|V|)\) time, without running Kruskal or Prim again. Apply it to a new cable A–D at cost 4, then to a new cable A–E at cost 5.
Solution to Exercise 8 (Cheapest Cables ★★★)
Prim, from A, adds A–C (3), B–C (3), B–D (2), then faces a tie between C–E and D–E (both 6), and finally adds E–F (1). Kruskal adds E–F (1), B–D (2), A–C (3), B–C (3), skips A–B (4) and C–D (5), which would close cycles, adds C–E or D–E (6), whichever comes first, and skips the other one and D–F (7). Both cost \(3 + 3 + 2 + 6 + 1 = 15\).
Fig. 38 The two MSTs of the offices. They only differ in the bold edge.#
The trees are the same when both algorithms break the tie the same way. There are exactly two MSTs: One with C–E and one with D–E. The other edges are forced: Each one is the only cheapest way out of some group of vertices.
No. In the tree, A and D are connected through A–C–B–D, of length \(3 + 3 + 2 = 8\). In the graph, A–B–D has length \(4 + 2 = 6\). An MST minimises the total weight of the network, while a shortest path minimises the length of the route between two given vertices. The cheapest network to build is usually not the one with the shortest trips.
Assume two different MSTs \(T_1\) and \(T_2\). Among the edges that belong to exactly one of them, take the lightest, \(e\), and say it belongs to \(T_1\). Adding \(e\) to \(T_2\) creates a cycle. This cycle contains an edge \(f\) that is not in \(T_1\) (otherwise \(T_1\) would contain the whole cycle). So \(f\) belongs to \(T_2\) only, and since \(e\) is the lightest such edge and all weights differ, \(w(f) > w(e)\). Replacing \(f\) with \(e\) in \(T_2\) gives a spanning tree that is strictly lighter than \(T_2\), which contradicts that \(T_2\) is minimum.
Going from the heaviest edge: D–F (7) is deleted (F stays connected through E–F). Then C–E and D–E (6) come: We can delete one of them, but not both, since the second one would then be the only connection between {E, F} and the rest. C–D (5) and A–B (4) are deleted. The other edges are all needed. We obtain one of the two MSTs again.
Each deleted edge lies on a cycle of the current graph, since the graph stays connected without it. All the heavier edges still on that cycle were examined earlier and kept, so they were bridges at the time. But a bridge stays a bridge when we delete other edges, so it cannot lie on this cycle. The deleted edge is therefore a heaviest edge of the cycle, and the cycle property says that we lose nothing by dropping it. At the end, the graph is still connected, and has no cycle left (the heaviest edge of any cycle would have been deleted): It is a spanning tree, and an MST.
Each deletion requires checking connectivity, for example with a traversal in \(O(|V| + |E|)\). Over all edges, this gives \(O(|E| \cdot (|V| + |E|))\), much slower than the \(O(|E| \log |E|)\) of Kruskal. Reverse-delete is Kruskal backwards: Kruskal keeps the light edges that do not close a cycle, reverse-delete removes the heavy edges that sit on one.
Adding the new edge \(\{u, v\}\) to the tree creates exactly one cycle: The path from \(u\) to \(v\) in the tree, plus the new edge. Find this path with a traversal of the tree from \(u\), in \(O(|V|)\) since the tree has \(|V| - 1\) edges. If the heaviest edge on the path weighs more than \(w\), replace it with the new edge. Otherwise, keep the tree as it is.
For A–D at cost 4, the path in the tree is A–C–B–D, whose heaviest edge costs 3: The new cable is not worth it. For A–E at cost 5, with the tree that uses C–E, the path is A–C–E, whose heaviest edge is C–E (6). We swap it for A–E, and the cost drops to 14. (With the tree that uses D–E, the path is A–C–B–D–E and we swap D–E, which also gives 14.)
Common mistakes
Claiming that the MST is unique “because the algorithm is deterministic”. Ties can be broken in different ways.
In Question 3, picking the lightest edge of \(T_1\) instead of the lightest edge in which the two trees differ.
Using the MST to answer shortest-path questions, as in Question 2.
In Question 5, comparing \(w\) with the edges incident to \(u\) and \(v\) only, instead of the whole path between them.
Exercise 9 (Network Design ★★★)
A city wants to connect eight neighborhoods, A to H, with fiber optic cables. The table below gives the cost (in millions) of laying a cable directly between two neighborhoods. A dash means that no cable can be laid between them.
A |
B |
C |
D |
E |
F |
G |
H |
|
|---|---|---|---|---|---|---|---|---|
A |
· |
13 |
8 |
26 |
27 |
14 |
– |
21 |
B |
13 |
· |
– |
– |
– |
3 |
2 |
28 |
C |
8 |
– |
· |
– |
– |
– |
25 |
18 |
D |
26 |
– |
– |
· |
19 |
– |
29 |
– |
E |
27 |
– |
– |
19 |
· |
– |
4 |
– |
F |
14 |
3 |
– |
– |
– |
· |
– |
– |
G |
– |
2 |
25 |
29 |
4 |
– |
· |
– |
H |
21 |
28 |
18 |
– |
– |
– |
– |
· |
Find a minimum spanning tree using Prim’s algorithm, starting from A, and then using Kruskal’s algorithm. What is the minimum total cost?
Solution to Exercise 9 (Network Design ★★★)
Prim’s algorithm, starting from A, adds the edges in this order:
AC (8), AB (13), BG (2), BF (3), EG (4), CH (18), DE (19).
For instance, the first step takes AC (8), the cheapest of the six cables leaving A. Once B is in the tree, the cheap edges BG (2) and BF (3) become available, and so does EG (4) after that.
Kruskal’s algorithm goes through the cables from the cheapest to the most expensive. It adds BG (2), BF (3), EG (4), AC (8) and AB (13). It skips AF (14), because A and F are already connected through B. It adds CH (18) and DE (19), which gives the seven edges we need for eight neighborhoods.
Both algorithms find the same seven cables: AB, AC, BF, BG, CH, DE and EG. The minimum total cost is
Exercise 10 (Why Is the Cheapest Tree Unique? ★★★)
If all the edge weights in a connected graph are different, then there is only one minimum spanning tree. Explain, in your own words, why this is the case.
Hint: Suppose there are two different minimum spanning trees, and think about swapping one edge for another.
Solution to Exercise 10 (Why Is the Cheapest Tree Unique? ★★★)
Suppose there are two different minimum spanning trees, \(T_1\) and \(T_2\). Since they are different, one of them has an edge \(e\) that the other does not. Say \(e\) is in \(T_1\).
If we remove \(e\) from \(T_1\), the tree falls apart in two groups of vertices. But \(T_2\) also connects the two ends of \(e\), through a path. This path has to go from one group to the other somewhere, through some edge \(f\) of \(T_2\). The edge \(f\) is not \(e\), since \(T_2\) does not have \(e\).
Now compare the weights of \(e\) and \(f\). They are different, since all the weights are.
If \(e\) is cheaper than \(f\), we can swap \(f\) for \(e\) in \(T_2\). We still have a spanning tree, but a cheaper one: \(T_2\) was not a minimum spanning tree.
If \(f\) is cheaper than \(e\), we can swap \(e\) for \(f\) in \(T_1\), and \(T_1\) was not a minimum spanning tree.
Either way, one of the two trees was not minimal, which contradicts our assumption. So there cannot be two different minimum spanning trees.
Exercise 11 (Who’s in Charge? ★★★)
Kruskal’s algorithm needs to know whether two vertices already
belong to the same component. Lecture 6.3 introduces the disjoint sets abstract data
type for this, with two operations: find(x) returns a
representative of the set that contains \(x\), and
union(x, y) merges the sets of \(x\) and \(y\). Let us
implement it on the elements \(1, \ldots, n\).
The simplest idea, called quick-find, stores in an array the
label of the set of every element: find reads the label, and
union relabels every element of one set with the label of the
other.
What are the time complexities of
findandunionwith quick-find? What does this give for Kruskal’s algorithm?
A better idea is a forest: Each set is a tree, whose root is the representative, and each element only stores its parent. A root is its own parent.
find(x):
while parent[x] ≠ x:
x = parent[x]
return x
union(x, y):
rx = find(x)
ry = find(y)
if rx ≠ ry:
parent[rx] = ry
Start with eight singletons, and run
union(1, 2),union(1, 3),union(1, 4), and so on, up tounion(1, 8). Draw the forest at the end. How many parent links does each call tofind(1)follow? What is the total cost of these unions, for \(n\) elements?Union by size changes one thing: Each root keeps the size of its tree, and
unionalways attaches the root of the smaller tree under the root of the larger one (on a tie,rxgoes underry). Run the same unions again, and draw the forest. Then find a sequence of unions on eight elements for which union by size still builds a tree of height 3.Show that, with union by size, a tree of \(n\) elements never has a height larger than \(\log_2 n\).
Hint: When does the depth of an element increase? What happens to the size of its tree at that moment?
Path compression is another improvement: After
find(x)has found the root, it makes every element it went through point directly to the root. Writefindwith path compression, and runfind(1)on the forest of Question 2. Draw the result.With both improvements, a sequence of \(m\) operations on \(n\) elements takes \(O(m \cdot \alpha(n))\) time, where \(\alpha\) grows so slowly that \(\alpha(n) \leq 4\) for any \(n\) you will ever meet (Tarjan, 1975). We do not ask you to prove it! But if Kruskal’s algorithm has to sort the edges in \(O(|E| \log |E|)\) anyway, does the choice of implementation matter at all? Describe a situation where it does.
Finally, explain how to use disjoint sets to detect a cycle in an undirected graph, as in Exercise 4.
Solution to Exercise 11 (Who's in Charge? ★★★)
findreads one cell: \(O(1)\).unionmust scan the whole array to relabel one set: \(O(n)\). Kruskal’s algorithm callsfindtwice per edge, \(O(|E|)\) in total, and makes exactly \(|V| - 1\) successful unions, \(O(|V|^2)\) in total. With the sorting, Kruskal then takes \(O(|E| \log |E| + |V|^2)\), which is fine for dense graphs, but poor for sparse ones.union(1, 2)makes 2 the parent of 1.union(1, 3)finds root 2 for element 1 and makes 3 its parent, and so on: Each union puts the old root under the new element. We end up with a chain \(1 \to 2 \to \cdots \to 8\), shown as (a) below. The callfind(1)insideunion(1, k)follows \(k - 2\) links, so the total is \(0 + 1 + \cdots + (n - 2) = (n-1)(n-2)/2\), that is 21 links for \(n = 8\), and \(\Theta(n^2)\) in general.Fig. 39 Arrows go from an element to its parent. (a) The naive unions build a chain. (b) Union by size builds a star. (c) Union by size on pairs of equal sizes reaches height 3. (d) The chain (a) after
find(1)with path compression.#With union by size,
union(1, 2)puts 1 under 2 (a tie). Then every new element is a singleton, smaller than the tree of 2, and goes under 2: We get a star of height 1, (b) above, and everyfindfollows at most one link.To reach height 3, always merge trees of equal sizes:
union(1, 2),union(3, 4),union(5, 6),union(7, 8), thenunion(1, 3),union(5, 7), and finallyunion(1, 5), which gives (c).The depth of an element \(x\) only increases when the root of its tree goes under another root. With union by size, this happens only when the other tree is at least as large, so the tree that contains \(x\) at least doubles in size. It starts with size 1 and never exceeds \(n\), so it can double at most \(\log_2 n\) times: The depth of \(x\) never exceeds \(\log_2 n\). Every
findthen costs \(O(\log n)\).A recursive version makes every element on the way point to the root, on the way back:
find(x): if parent[x] ≠ x: parent[x] = find(parent[x]) return parent[x]On the chain (a),
find(1)follows 7 links once, and every element from 1 to 7 then points directly to 8, as in (d). The nextfindon any of them follows a single link.With union by size alone, Kruskal makes \(O(|E|)\) calls to
findat \(O(\log |V|)\) each, which is no worse than sorting, so the sorting dominates and both improvements bring little more. The implementation matters when the sorting is cheap or already done: The edges may come sorted, or the weights may be small integers that can be sorted in linear time (see counting sort in Lecture 3.6). Disjoint sets also serve many problems without any sorting, for example tracking the friend groups of a social network as friendships appear, where millions of unions and finds happen online.To detect a cycle, start with one set per vertex and go through the edges. For each edge \(\{u, v\}\), if
find(u) = find(v), then \(u\) and \(v\) are already connected, and the edge closes a cycle. Otherwise, callunion(u, v).
Common mistakes
Linking \(x\) itself under \(y\), as in
parent[x] = y, instead of linking the roots. This loses the rest of the tree of \(x\).Comparing the heights of the trees instead of their sizes, and then forgetting to update them. (Union by rank, which uses heights, works too, but needs care with path compression.)
Thinking that path compression changes the result of
find. It only changes the shape of the tree, never which root an element leads to.
Going Further#
This problem goes beyond the lectures. The assignment problem itself is fair game: It has come up on an earlier exam, and modelling it as a graph (Questions 1 and 2) uses nothing but this module. The Hungarian algorithm, however, is not part of the syllabus, and you are not expected to know it by heart. The background below gives you every step, and the exercise simply takes you through them, as a guided tour of how a classic algorithm on a weighted bipartite graph works and why it is correct.
Exercise 12 (Mentor Matchmaking ★★★★)
Background. Four students, Ada, Bo, Cy, and Dee, each need a mentor, and four mentors, Kari, Lars, Mia, and Nils, can each take one student. Every student ranks the mentors from 1 (favourite) to 4:
Kari |
Lars |
Mia |
Nils |
|
|---|---|---|---|---|
Ada |
1 |
2 |
3 |
4 |
Bo |
4 |
3 |
1 |
2 |
Cy |
1 |
3 |
2 |
4 |
Dee |
1 |
2 |
3 |
4 |
We look for an assignment, which gives each student a different mentor, and we want the sum of the ranks to be as small as possible. This is the assignment problem. The Hungarian algorithm (Kuhn, 1955; Munkres, 1957) solves it on an \(n \times n\) matrix of costs, here the ranks. There is no need to memorise it; just follow the steps:
Subtract from every row its smallest entry.
Subtract from every column its smallest entry.
Cover all the zeros of the matrix with as few lines (whole rows or whole columns) as possible. If \(n\) lines are needed, we can pick \(n\) zeros, no two in the same row or column: This is an optimal assignment, and we stop.
Otherwise, let \(m\) be the smallest entry that no line covers. Subtract \(m\) from every uncovered entry, add \(m\) to every entry covered by two lines, and go back to Step 3.
Step 3 relies on a theorem by Dénes Kőnig (1931), which gave the algorithm its name: The smallest number of lines that cover all the zeros equals the largest number of zeros we can pick with no two in the same row or column. In graph terms, take the bipartite graph whose edges are the zeros: Picking zeros is a matching, and covering them with lines is a vertex cover.
Problem.
Model the situation as a weighted graph. What are the vertices, the edges, and the weights? What does an assignment correspond to? How many different assignments are there for 4 students? For 20? How long would it take to try them all at \(10^9\) assignments per second?
Two natural strategies:
First come, first served: The students choose in alphabetical order, each one taking their favourite mentor among those still free.
Happiest first: Repeatedly assign the pair with the smallest rank among the students and mentors still free (break ties any way you like).
Run both strategies. Then find the best assignment, by reasoning rather than by trying all of them.
Hint: Three students rank Kari first. How many of them can have a rank of 1?
Run the Hungarian algorithm on the table. Give the matrix after Steps 1 and 2, then after every round of Step 4, together with the lines you used. Give the final assignment and its total.
Let us see why the algorithm is correct, in four steps.
Show that subtracting the same number from every entry of a row (or of a column) does not change which assignments are optimal.
Show that, if all entries are non-negative, an assignment that only uses zeros is optimal.
Show that Step 4 has the same effect as subtracting \(m\) from every uncovered row and adding \(m\) to every covered column. Deduce that Step 4 never makes an entry negative, and that it does not change the optimal assignments either.
Keep track of the total \(L\) of everything subtracted so far (rows and columns, minus what was added back). Show that \(L\) is a lower bound on the cost of any assignment, and that every round of Step 4 increases it. Deduce that the algorithm stops when the costs are integers.
Hint for d: With \(r\) covered rows and \(k\) covered columns, we have \(r + k < n\). How many uncovered rows are there?
Real life is messier. Explain how to adapt the method to each of the following situations:
Each mentor can take two students, and there are eight students.
There are five students but only four mentors, so one student gets none.
Three students rank the mentors exactly in the same order (Kari, Lars, Mia, Nils), and Dee ranks them Kari 2, Lars 4, Mia 1, Nils 3. Compute the best assignment. Is it fair? What would you minimise instead, and how?
Solution to Exercise 12 (Mentor Matchmaking ★★★★)
The vertices are the students on one side and the mentors on the other. Every student is linked to every mentor, with the rank as the weight: This is a complete bipartite graph, \(K_{4,4}\). An assignment is a set of edges that touches every vertex exactly once, called a perfect matching, and we look for one of minimum total weight.
The first student has \(n\) choices, the second \(n - 1\), and so on: There are \(n!\) assignments. That is 24 for 4 students, but \(20! \approx 2.4 \cdot 10^{18}\) for 20 students, which takes about \(2.4 \cdot 10^9\) seconds at \(10^9\) per second, that is about 77 years.
First come, first served: Ada takes Kari (1), Bo takes Mia (1), Cy finds Kari and Mia taken and takes Lars (3), and Dee is left with Nils (4). The total is 9.
Happiest first: The pairs of rank 1 are Ada, Cy, and Dee with Kari, and Bo with Mia. Only one of the three gets Kari, and Bo gets Mia. Among the pairs of rank 2 still available, one of the two others (Ada or Dee) gets Lars. The last student is left with Nils, rank 4. Whatever the ties, the total is 8.
The best assignment costs 7. Only one of Ada, Cy, and Dee can get Kari, so the two others get a rank of at least 2, and the total is at least \(1 + 1 + 2 + 2 = 6\). A total of 6 would need Bo with Mia, and two of the three with a rank of 2. But their rank 2 is Lars for Ada and Dee, and Mia (taken by Bo) for Cy: Only one of them can have it. So the total is at least 7, and Ada–Kari, Dee–Lars, Cy–Mia, Bo–Nils reaches it (as does the same with Ada and Dee swapped). Both greedy strategies go wrong by giving Bo their favourite, Mia, which Cy needed more.
In the order (Ada, Bo, Cy, Dee) for rows and (Kari, Lars, Mia, Nils) for columns, every row has minimum 1. After Step 1, the columns have minima 0, 1, 0, 1, and Step 2 gives:
\[\begin{split}\begin{bmatrix} 0 & 1 & 2 & 3 \\ 3 & 2 & 0 & 1 \\ 0 & 2 & 1 & 3 \\ 0 & 1 & 2 & 3 \end{bmatrix} \xrightarrow{\text{Step 2}} \begin{bmatrix} 0 & 0 & 2 & 2 \\ 3 & 1 & 0 & 0 \\ 0 & 1 & 1 & 2 \\ 0 & 0 & 2 & 2 \end{bmatrix}\end{split}\]The zeros of Ada, Cy, and Dee all sit in the columns of Kari and Lars: At most two of them can be picked, so 3 lines are enough, namely the columns Kari and Lars, and the row Bo. The smallest uncovered entry is \(m = 1\) (Cy–Mia). Step 4 gives:
\[\begin{split}\begin{bmatrix} 0 & 0 & 1 & 1 \\ 4 & 2 & 0 & 0 \\ 0 & 1 & 0 & 1 \\ 0 & 0 & 1 & 1 \end{bmatrix}\end{split}\]Now 4 lines are needed, and we can pick Cy–Mia, Bo–Nils, then Ada–Kari and Dee–Lars (or Ada–Lars and Dee–Kari). The total is 7, as found in Question 2.
Every assignment uses exactly one entry of each row. Subtracting \(c\) from a row therefore lowers the cost of every assignment by exactly \(c\). Their order does not change, so the optimal ones stay optimal. The same holds for columns.
If all entries are non-negative, every assignment costs at least 0. An assignment that only uses zeros costs 0, so none can beat it.
Compare the two descriptions entry by entry. An entry in an uncovered row and an uncovered column gets \(-m\) in both. An entry in an uncovered row and a covered column gets \(-m + m = 0\). An entry in a covered row and an uncovered column is untouched in both. An entry in a covered row and a covered column gets \(+m\). Only uncovered entries decrease, by their minimum \(m\), so none becomes negative. And Step 4 is now a series of row and column operations, so by (a), the optimal assignments do not change.
Every assignment uses one entry per row and one per column, so its cost in the current matrix is its original cost minus \(L\). Since all current entries are non-negative, the original cost is at least \(L\). With \(r\) covered rows and \(k\) covered columns, there are \(n - r\) uncovered rows, and \(r + k < n\) gives \(n - r > k\). A round of Step 4 subtracts \(m\) from \(n - r\) rows and adds it back on \(k\) columns: \(L\) increases by \(m(n - r - k) \geq m > 0\). With integer costs, \(L\) grows by at least 1 per round, and it can never exceed the cost of an optimal assignment, so the algorithm stops.
On the mentors, \(L = 4 + 2 = 6\) after Steps 1 and 2, and the round of Step 4 adds \(1 \cdot (3 - 2) = 1\). The final assignment costs 7 = \(L\): The lower bound itself proves that it is optimal.
As described here, with the lines found by hand, the algorithm takes \(O(n^4)\) time. Careful implementations bring it down to \(O(n^3)\), which handles thousands of students easily.
Duplicate every mentor: Kari 1 and Kari 2 get the same column of ranks. The matrix is \(8 \times 8\), and any assignment gives each mentor at most two students.
Add a fifth, imaginary mentor, “nobody”, to make the matrix square. The cost of “nobody” is a penalty, for example 5 for every student (worse than any real mentor). The algorithm decides who goes without a mentor, at the lowest total cost.
The best total is 8: Dee gets Mia (1), and the three others share Kari, Lars, and Nils, with ranks 1, 2, and 4. Someone gets their last choice, even though giving Dee Nils (3) and the others Kari, Lars, and Mia would cap everyone at rank 3, for a total of 9. The sum of ranks only cares about the average. To protect the unluckiest student, we can minimise the worst rank: Try \(r = 1, 2, \ldots\), keep only the pairs with a rank of at most \(r\), and stop at the first \(r\) for which a perfect matching exists (for example, with the Hungarian algorithm on costs 0 and 1). Here \(r = 3\). Another option is to make bad ranks more expensive, for example by using the square or the cube of the rank as the cost. Here the squares are not enough (22 against 23), but the cubes are (74 against 63).
Common mistakes
Stopping the Hungarian algorithm as soon as every row and every column contains a zero. What matters is whether we can pick \(n\) zeros in different rows and columns.
In Step 4, adding \(m\) to every covered entry instead of only those covered twice.
Covering the zeros with more lines than necessary. Too many lines leave fewer uncovered entries and can hide the next step.
In Question 4a, arguing about one assignment only. The point is that all assignments change by the same amount.
Case Studies#
The problems above all have a solution. The three case studies below are different: Each one is a famous graph problem that is easy to explain at a dinner party, and surprisingly hard to solve. The first questions are within reach; the last ones open onto problems that keep researchers busy today. There is no solution to look up. Instead, each case study comes with a dropdown of ideas and resources to help you go further. Pick the one that tickles your curiosity.
Exercise (Exam Timetabling)
Every semester, NTNU schedules thousands of exams. Two exams that share at least one student cannot take place at the same time. We can draw this as a conflict graph: Courses are the vertices, and an edge connects two courses that share a student. Giving each course a time slot such that adjacent courses get different slots is called colouring the graph, with slots as colours. The smallest number of colours a graph needs is its chromatic number, written \(\chi(G)\). Graph colouring appears on the list of graph problems in Lecture 6.1, among the “difficult” ones.
Fig. 40 The conflict graph of eight courses#
Find a timetable for the eight courses of Fig. 40 with as few slots as you can. A friend says: “I cannot find four courses that all conflict with each other, so three slots must be enough”. Are they right?
The greedy strategy goes through the courses in some order, and gives each one the first slot that none of its scheduled neighbours uses. Try it with a few different orders. Show that it never needs more than \(\Delta + 1\) slots, where \(\Delta\) is the largest degree. Show also that, for every graph, some order makes greedy optimal. Why does this not solve the problem?
When are two slots enough? Design an algorithm that decides it in \(O(|V| + |E|)\) time.
For three slots, nobody knows an efficient algorithm, and most researchers believe that none exists. How many timetables would a brute-force search try for 300 courses and 3 slots?
A Sudoku is a colouring problem in disguise. Describe its graph: What are the vertices, the edges, and the colours? How many edges are there? Write a program that colours a graph by backtracking (try a colour, go on, undo it when you get stuck), and use it to solve a Sudoku.
Colour a map of the counties of Norway, so that neighbouring counties get different colours. How many colours do you need? The four colour theorem says that four colours are enough for any map. Why does a map never need five colours, while some graphs do?
Open-ended: Find real conflict data, or one of the benchmark graphs used by researchers (see the resources), and compare greedy with a smarter order, such as largest degree first or DSatur. How close to the best known answer do you get?
Resources & Ideas
Ideas
Question 1. Four slots are enough (for example DB, PHY, and WEB in slot 1; MAT, NET, and SEC in slot 2; STA in slot 3; ALG in slot 4). Your friend is wrong: A group of courses that all conflict (a clique) gives a lower bound, but not always the right one. Look at the five courses around ALG: They form a cycle of odd length. Can two slots alternate around it?
Fig. 41 A timetable with four slots#
Question 2. When greedy reaches a course, at most \(\Delta\) of its neighbours already have a slot. For the second part, start from an optimal colouring and list the courses slot by slot. Then try greedy on the “crown” below, in the order u1, v1, u2, v2, …: Two colours suffice, yet greedy uses four. With \(n\) pairs, it uses \(n\).
Fig. 42 A crown graph. Each u is linked to every v except the one below it.#
Question 3. Two slots are enough exactly when the graph has no cycle of odd length: It is bipartite. Run a breadth-first traversal (see Exercise 4), give even levels one slot and odd levels the other, and check every edge. For three slots, brute force tries \(3^{300} \approx 10^{143}\) timetables. The problem is NP-complete (Karp, 1972): If you find an efficient algorithm, you also solve thousands of other problems, and win a million dollars.
Question 4. There are 81 cells and 9 colours. Each cell conflicts with 8 cells in its row, 8 in its column, and 4 more in its box, so there are \(81 \cdot 20 / 2 = 810\) edges. The given digits are pre-coloured vertices.
Question 5. A map gives a planar graph: It can be drawn without crossing edges. Five counties that all touch each other would give \(K_5\), which cannot be drawn without a crossing (try it!). This alone does not prove the theorem, which resisted mathematicians for more than a century.
Question 6. DSatur always colours next the vertex whose neighbours already use the most different colours.
Resources
Graph coloring and Four color theorem on Wikipedia. The theorem was first proved in 1976 by Appel and Haken, using a computer to check almost 2000 cases. It was the first major theorem proved this way, and it caused quite a debate.
G. Gonthier (2008). Formal Proof—The Four-Color Theorem. Notices of the AMS, 55(11), 1382–1393. How the proof was checked entirely by a computer, in the Coq proof assistant.
R. M. Karp (1972). Reducibility Among Combinatorial Problems. The paper that showed that graph colouring, and 20 other problems, are NP-complete.
D. Brélaz (1979). New Methods to Color the Vertices of a Graph. Communications of the ACM, 22(4), 251–256. The DSatur heuristic.
COLOR02/03/04: Benchmark graphs for colouring, many from real timetabling and scheduling problems, with the best known answers.
Exercise (Your Friends Are More Popular)
In 1991, the sociologist Scott Feld published a paper with a provocative title: Why Your Friends Have More Friends Than You Do. Feld showed that, on average, your friends really do have more friends than you, and this has nothing to do with your personality. It is a property of graphs. Let us check it on the friendship graph of Lecture 6.1, reproduced on Fig. 43.
Fig. 43 The friendship graph from Lecture 6.1#
What is the average number of friends per person? For each person, compute the average number of friends of their friends. How many people have fewer friends than their friends do on average?
Here is another way to measure “the friends of my friends”: Pick a friendship (an edge) at random, then one of its two ends at random, and count that person’s friends. Show that the average result is \(\sum_v d(v)^2 / \sum_v d(v)\), where \(d(v)\) is the degree of \(v\), and that it is always at least the average degree. When are they equal? Compute it on the graph.
The average from Question 1 (per person first, then over everyone) is a different number. Is it also always at least the average degree?
Feld’s title suggests something stronger: That most people have fewer friends than their friends do. Find a graph where the friends are more popular on average, in the sense of Question 2, but where fewer than half of the people have fewer friends than their friends.
Download a real social network, such as the Facebook graph from SNAP (see the resources), and compute the three numbers: The average degree, the average of Question 2, and the fraction of people who have fewer friends than their friends. How strong is the paradox in real life?
Open-ended: The paradox has practical uses. During an epidemic, who gets infected first, a random person or a random person’s friend? If you could vaccinate only 1% of a population whose graph you do not know, whom would you pick? Does the paradox hold for followers on a directed network, such as Instagram or Bluesky?
Resources & Ideas
Ideas
Question 1. The degrees are Denis 2, Erik 1, Frank 3, John 2, Lisa 2, Mary 3, Olive 3, Peter 1, and Thomas 1: An average of 2. The averages of their friends range from 1.67 (Frank) to 3, with a mean of about 2.52. Six of the nine people have fewer friends than their friends.
Question 2. By the handshake lemma (see Exercise 1), there are \(\sum_v d(v)\) edge ends, and person \(v\) owns \(d(v)\) of them: Popular people get picked more often. To compare with the average degree, you need \(n \sum d^2 \geq (\sum d)^2\). Think of the variance of the degrees, which is never negative. On the graph, you should get \(42 / 18 \approx 2.33\).
Question 3. Yes. Write the sum over all people as a sum over all edges \(\{u, v\}\): Each edge contributes \(d(u)/d(v) + d(v)/d(u)\). What is the smallest possible value of \(x/y + y/x\) for positive \(x\) and \(y\)?
Question 4. Try small, simple graphs first: Paths, stars, cycles. Very few vertices are enough.
Question 5. Count degrees with a hash map while reading the edge list; no fancy library needed.
Question 6. In 2009, researchers followed the flu at Harvard among random students and among friends they named. The friends got sick about two weeks earlier. For vaccination, look up acquaintance immunization: Vaccinate a random friend of a random person.
Resources
S. L. Feld (1991). Why Your Friends Have More Friends Than You Do. American Journal of Sociology, 96(6), 1464–1477.
N. A. Christakis and J. H. Fowler (2010). Social Network Sensors for Early Detection of Contagious Outbreaks. PLoS ONE, 5(9), e12948.
R. Cohen, S. Havlin, and D. ben-Avraham (2003). Efficient Immunization Strategies for Computer Networks and Populations. Physical Review Letters, 91, 247901.
J. Ugander, B. Karrer, L. Backstrom, and C. Marlow (2011). The Anatomy of the Facebook Social Graph. The paradox, measured on the whole of Facebook.
ego-Facebook, from the Stanford Network Analysis Project (SNAP): 4039 people and 88234 friendships, as a plain list of edges.
Friendship paradox on Wikipedia.
Exercise (The Party Problem)
Here is a game for two players, called Sim. Draw six dots on a sheet of paper. Each player has a pen of their own colour, say red and blue. Taking turns, each player draws a line between two dots that are not yet connected. The first player to complete a triangle of their own colour, with three dots as corners, loses. Lines of the other colour do not count.
Play a few games of Sim with a friend. Did any game end in a draw, with all 15 lines drawn and no single-coloured triangle? Try to colour all the lines on purpose, with no single-coloured triangle.
Show that Sim can never end in a draw. Said differently: At any party of six people, there are always three people who all know each other, or three who are all strangers.
Hint: Pick one dot. It has five lines. What can you say about their colours?
Would a game of Sim with five dots always have a loser?
Look at the friendship graph of Fig. 43. Are there three people who all know each other? Mathematicians know that, among any nine people, there are always three mutual friends or four mutual strangers. Find the four strangers. In the vocabulary of Lecture 6.1, what are three mutual friends and four mutual strangers?
Programming challenge: Among any 18 people, there are always four mutual friends or four mutual strangers. But 17 people are not enough: Find a friendship graph on 17 people with neither. Write a program that checks a candidate graph, then one that looks for one, for instance by starting from a random graph and flipping the edges that take part in the most bad groups of four.
The smallest number of people that guarantees \(k\) mutual friends or \(k\) mutual strangers is the Ramsey number \(R(k, k)\). We know that \(R(3,3) = 6\) (Question 2) and \(R(4,4) = 18\) (Question 5). For \(R(5,5)\), nobody knows: It lies somewhere between 43 and 46. How many ways are there to colour the lines between 43 dots in red and blue? Why can’t we just check them all?
Resources & Ideas
Ideas
Questions 1 and 2. Among the five lines of a dot, at least three have the same colour, say red. Now look at the three dots at the other ends of these red lines, and the lines between them.
Question 3. No: Colour the outline of a pentagon in one colour and the five diagonals (a star) in the other.
Fig. 44 Five people with no three mutual friends (solid) and no three mutual strangers (dashed)#
Question 4. There is no triangle in this graph. Three mutual friends form a clique (a complete subgraph), and mutual strangers form an independent set. Any graph with \(n\) vertices is a colouring of the lines of \(K_n\): Edges are red, missing edges are blue.
Question 5. Checking a candidate means looking at all \(\binom{17}{4} = 2380\) groups of four, which is fast. If your search gets stuck, look up the Paley graph of order 17: Number the people 0 to 16, and make \(i\) and \(j\) friends when \(i - j\) is a square modulo 17, that is, one of 1, 2, 4, 8, 9, 13, 15, 16.
Question 6. \(K_{43}\) has \(43 \cdot 42 / 2 = 903\) lines, so there are \(2^{903} \approx 10^{272}\) colourings, far more than the number of atoms in the universe. Paul Erdős is said to have told this story, retold by Joel Spencer: If aliens threatened to destroy Earth unless we told them \(R(5,5)\), we should gather all our computers and mathematicians and try. If they asked for \(R(6,6)\), we should try to destroy the aliens instead.
For the mathematically curious. Pick one vertex, and split the others by the colour of their line to it. This shows that \(R(s, t) \leq R(s-1, t) + R(s, t-1)\), and therefore that \(R(k, k)\) exists for every \(k\) and is at most about \(4^k\) (Erdős and Szekeres, 1935). Improving the 4 was open for almost 90 years.
Resources
Sim (game) on Wikipedia. Invented by Gustavus Simmons in 1969. A computer search showed in 1974 that the second player can always win, with a strategy too complicated to memorise.
S. Radziszowski. Small Ramsey Numbers. Electronic Journal of Combinatorics, Dynamic Survey DS1. The reference table of all the Ramsey numbers we know, updated every few years.
V. Angeltveit and B. D. McKay (2024). R(5,5) ≤ 46. The latest upper bound, obtained with a large computer search. The lower bound of 43 comes from a colouring of \(K_{42}\) found by Exoo in 1989.
M. Campos, S. Griffiths, R. Morris, and J. Sahasrabudhe (2023). An Exponential Improvement for Diagonal Ramsey. The first improvement of the \(4^k\) bound since 1935.
