Graphs
Level 4 (Application — novel problems, no hints) Time limit: 60 minutes Total marks: 60
Instructions: Show all working. State any assumptions. Where an algorithm is named, you may use its standard operations without re-deriving them, but you must show the intermediate states requested.
Question 1 — Shortest paths under a twist (14 marks)
A delivery drone operates on a directed weighted graph with vertices and edges (edge : weight):
(a) Run Dijkstra's algorithm from . Give the final shortest-distance to every vertex and the order in which vertices are finalized (extracted from the priority queue). (6)
(b) Now every edge weight is replaced by (all weights stay positive). Explain precisely why re-running Dijkstra with does not, in general, produce the path with the fewest edges, and give a concrete counterexample path from to from this graph illustrating the point. (4)
(c) Suppose instead we want the path minimising the maximum single edge weight (a "bottleneck" path), using original weights. State the minimum possible bottleneck value and one path achieving it. Briefly describe how to adapt Dijkstra to compute this. (4)
Question 2 — Negative edges and cycles (12 marks)
Consider the directed graph on with edges:
(a) Run Bellman–Ford from source . Process edges in the listed order each pass. Show the distance array after each of passes 1, 2, and 3, and after the extra -th relaxation pass. (7)
(b) Does this graph contain a negative cycle reachable from ? Justify using your pass results and identify the cycle if it exists. (3)
(c) State one graph property that makes Floyd–Warshall preferable to running Bellman–Ford from every source, and give the asymptotic comparison for a graph with vertices and edges. (2)
Question 3 — Structure detection (13 marks)
You are given the directed graph on :
(a) Find all strongly connected components. Show enough of Kosaraju's method (first-pass finish order and second-pass on the transpose) to justify your answer. (7)
(b) Build the condensation (DAG of SCCs) and give one valid topological ordering of it. (3)
(c) Explain in one sentence why the condensation of any directed graph is always acyclic. Then state how many distinct topological orderings the condensation from (b) has. (3)
Question 4 — Cuts, flows and matching (13 marks)
A network has source , sink , and vertices with directed capacities:
(a) Find the maximum – flow value. Show one augmenting-path sequence (Edmonds–Karp style: shortest augmenting paths first) with residual updates. (6)
(b) State a minimum cut (the vertex partition and its capacity) and verify max-flow = min-cut. (3)
(c) A separate bipartite graph has left set , right set with edges –, –, –. Give a maximum matching and state its size. Prove maximality by exhibiting a minimum vertex cover (König's theorem). (4)
Question 5 — MST and Union-Find trace (8 marks)
Undirected weighted graph on with edges:
(a) Run Kruskal's algorithm. List edges in the order considered, mark accept/reject, and give the total MST weight. (5)
(b) Using union-by-rank (rank ties broken by making the first-named root the parent) with path compression, give the parent array (or forest) after all Kruskal unions. (3)
Answer keyMark scheme & solutions
Question 1
(a) Dijkstra from (original weights).
- Init: , others .
- Extract : relax , .
- Extract : relax ; ; .
- Extract : relax .
- Extract : relax .
- Extract : relax (no change); ? , no change.
- Extract .
Final distances: . Finalize order: . (6: distances 4, order 2)
(b) Dijkstra minimises total weight, not edge count. Transforming makes long original edges cheap, so the algorithm favours paths with many short-original edges — but there is no monotone relationship between -cost and number of edges. Concretely: path has 4 edges; -cost . Path has 3 edges; -cost . Under the 3-edge path wins here, but that's coincidental — one can construct a graph where the -minimal path uses more edges than an alternative. The point: minimising = minimising for a -edge path, which mixes edge count and total weight, so it is not equivalent to minimising . To get fewest edges, use BFS (unit weights). (4: correct reasoning 2, concrete numbers 2)
(c) Bottleneck (minimax edge) :
- Path : max edge .
- Path : max .
- Path : max . Minimum bottleneck , achieved by . Adaptation: replace the "distance = sum" relaxation with and extract the vertex of minimum current bottleneck. (4: value 2, path 1, adaptation 1)
Question 2
(a) Bellman–Ford from . Order: . Init: for .
Pass 1:
- :
- :
- :
- :
- : Result: .
Pass 2:
- :
- :
- :
- :
- : Result: .
Pass 3:
- :
- :
- :
- :
- : Result: .
Pass 4 (|V|-th, the extra check pass):
- : (no)
- : ← still decreasing
- ... distances keep dropping. Result shows further relaxation, e.g. . (7: passes 2 each ≈ rounded to 7)
(b) Yes, a negative cycle reachable from exists. Distances continue to decrease on pass 4 (the -th pass), which is the standard Bellman–Ford negative-cycle certificate. The cycle is with weight . (3)
(c) Floyd–Warshall gives all-pairs shortest paths in and handles negative edges (no negative cycle). Running Bellman–Ford from every source costs ; with that is , worse than . So Floyd–Warshall is preferable on dense graphs when all-pairs distances are needed. (2)
Question 3
(a) Edges: (cycle) and , (cycle). Kosaraju pass 1 (DFS, record finish times) from : visits ; a valid finish order (last-finished first) is i.e. stack top . Pass 2 on transpose ():
- Start : in reach (via ? transpose of gives ), , back — collects .
- Next unvisited by finish order: : in reaches (transposes of ) — collects .
SCCs: and . (7)
(b) Condensation: two super-nodes , with single edge (from ). Topological order: . (3)
(c) If the condensation had a cycle, two distinct SCCs would be mutually reachable and hence form one larger SCC — contradiction; so it is acyclic. Number of topological orderings of a 2-node chain = 1. (3)
Question 4
(a) Max flow. Edmonds–Karp (shortest augmenting paths):
- Path (length 2), bottleneck . Flow . Residual: , .
- Path (length 2), bottleneck . Flow . Residual: , .
- Path (length 3), bottleneck . Flow . Residual: , , .
- No more augmenting paths ('s out-edges saturated). Max flow . (6)
(b) Min cut: , . Cut edges , , capacity . Equals max flow ✓. (3)
(c) Bipartite matching: (or ), has only (taken). Better: — size 3 (perfect). Min vertex cover (König): must cover all edges; since matching size , min cover size , e.g. covers every edge ('s edges to ✓; 's ✓; 's edges via ✓). Cover size matching size , so matching is maximum. (4)
Question 5
(a) Kruskal, sorted edges:
| Edge | Weight | Action |
|---|---|---|
| P–Q | 1 | accept |
| Q–R | 2 | accept |
| S–U | 2 | accept |
| P–R | 3 | reject (P,R connected) |
| Q–S | 4 | accept (joins {P,Q,R} with {S,U}) |
| R–S | 5 | reject (cycle) |
| R–U | 6 | reject (cycle) |
MST edges: P–Q, Q–R, S–U, Q–S. Total weight . (5)
(b) Union-by-rank + path compression trace (ranks start 0). Making first-named the root on ties:
- P–Q: root P (rank 1), Q→P.
- Q–R: find(Q)=P; R→P (P rank stays 1 since R rank 0).
- S–U: root S (rank 1), U→S.
- Q–S: find(Q)=P(rank1), find(S)=S(rank1) — equal ranks: P becomes root, S→P, P rank→2.
Final parent array: , , , , (or after path compression on any later find(U)). Single tree rooted at P. (3)
[
{"claim":"Dijkstra Q1 final distances S,A,B,C,D,T",
"code":"import heapq\nadj={'S':[('A',4),('B',2)],'A':[('C',5)],'B':[('A',1),('C',8),('D',7)],'C':[('T',3)],'D':[('C',2),('T',6)],'T':[]}\ndist={k:float('inf') for k in adj}\ndist['S']=0\npq=[(0,'S')]\nwhile pq:\n d,u=heapq.heappop(pq)\n if d>dist[u]:continue\n for v,w in adj[u]:\n if d+w<dist[v]:dist[v]=d+w;heapq.heappush(pq,(dist[v],v))\nresult = dist=={'S':0,'A':3,'B':2,'C':8,'D':9,'T':11}"},
{"claim":"Bellman-Ford Q2 has reachable negative cycle from 1",
"code":"edges=[(1,2,3),(2,3,-2),(3,4,2),(4,2,-2),(1,3,5)]\nV=[1,2,3,4]\nINF=float('inf')\nd={v:INF for v in V};d[1]=0\nfor _ in range(len(V)-1):\n for u,v,w in edges:\n if d[u]!=INF and d[u]+w<d[v]:d[v]=d[u]+w\nneg=False\nfor u,v,w in edges:\n if d[u]!=INF and d[u]+w