Level 4 — ApplicationGraphs

Graphs

60 minutes60 marksprintable — key stays hidden on paper

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 {S,A,B,C,D,T}\{S, A, B, C, D, T\} and edges (edge : weight):

SA:4,SB:2,BA:1,AC:5,BC:8,BD:7,CT:3,DC:2,DT:6.S\to A:4,\quad S\to B:2,\quad B\to A:1,\quad A\to C:5,\quad B\to C:8,\quad B\to D:7,\quad C\to T:3,\quad D\to C:2,\quad D\to T:6.

(a) Run Dijkstra's algorithm from SS. 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 w(u,v)=10w(u,v)w'(u,v) = 10 - w(u,v) (all weights stay positive). Explain precisely why re-running Dijkstra with ww' does not, in general, produce the path with the fewest edges, and give a concrete counterexample path from SS to TT from this graph illustrating the point. (4)

(c) Suppose instead we want the STS\to T 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 {1,2,3,4}\{1,2,3,4\} with edges:

12:3,23:2,34:2,42:2,13:5.1\to2:3,\quad 2\to3:-2,\quad 3\to4:2,\quad 4\to2:-2,\quad 1\to3:5.

(a) Run Bellman–Ford from source 11. Process edges in the listed order each pass. Show the distance array after each of passes 1, 2, and 3, and after the extra (V)(|V|)-th relaxation pass. (7)

(b) Does this graph contain a negative cycle reachable from 11? 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 VV vertices and EV2/4E \approx V^2/4 edges. (2)


Question 3 — Structure detection (13 marks)

You are given the directed graph on {a,b,c,d,e,f}\{a,b,c,d,e,f\}:

ab, bc, ca, cd, de, ef, fd.a\to b,\ b\to c,\ c\to a,\ c\to d,\ d\to e,\ e\to f,\ f\to d.

(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 ss, sink tt, and vertices {s,u,v,t}\{s, u, v, t\} with directed capacities:

su:10,sv:5,uv:15,ut:5,vt:10.s\to u:10,\quad s\to v:5,\quad u\to v:15,\quad u\to t:5,\quad v\to t:10.

(a) Find the maximum sstt 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 {x1,x2,x3}\{x_1,x_2,x_3\}, right set {y1,y2,y3}\{y_1,y_2,y_3\} with edges x1x_1{y1,y2}\{y_1,y_2\}, x2x_2{y1}\{y_1\}, x3x_3{y2,y3}\{y_2,y_3\}. 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 {P,Q,R,S,U}\{P,Q,R,S,U\} with edges:

P ⁣ ⁣Q:1, P ⁣ ⁣R:3, Q ⁣ ⁣R:2, Q ⁣ ⁣S:4, R ⁣ ⁣S:5, R ⁣ ⁣U:6, S ⁣ ⁣U:2.P\!-\!Q:1,\ P\!-\!R:3,\ Q\!-\!R:2,\ Q\!-\!S:4,\ R\!-\!S:5,\ R\!-\!U:6,\ S\!-\!U:2.

(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 SS (original weights).

  • Init: d(S)=0d(S)=0, others \infty.
  • Extract S(0)S(0): relax A4A\to4, B2B\to2.
  • Extract B(2)B(2): relax A:min(4,2+1)=3A: \min(4, 2+1)=3; C:2+8=10C: 2+8=10; D:2+7=9D: 2+7=9.
  • Extract A(3)A(3): relax C:min(10,3+5)=8C: \min(10, 3+5)=8.
  • Extract C(8)C(8): relax T:8+3=11T: 8+3=11.
  • Extract D(9)D(9): relax C:min(8,9+2)=11C: \min(8, 9+2)=11 (no change); T:min(11,9+6)=11T:\min(11,9+6)=11? 9+6=159+6=15, no change.
  • Extract T(11)T(11).

Final distances: S=0, B=2, A=3, C=8, D=9, T=11S=0,\ B=2,\ A=3,\ C=8,\ D=9,\ T=11. Finalize order: S,B,A,C,D,TS, B, A, C, D, T. (6: distances 4, order 2)

(b) Dijkstra minimises total weight, not edge count. Transforming w=10ww'=10-w makes long original edges cheap, so the algorithm favours paths with many short-original edges — but there is no monotone relationship between ww'-cost and number of edges. Concretely: path SBACTS\to B\to A\to C\to T has 4 edges; ww'-cost =8+9+5+7=29=8+9+5+7=29. Path SBCTS\to B\to C\to T has 3 edges; ww'-cost =8+2+7=17=8+2+7=17. Under ww' the 3-edge path wins here, but that's coincidental — one can construct a graph where the ww'-minimal path uses more edges than an alternative. The point: minimising (10w)\sum(10-w) = minimising (10kw)(10k-\sum w) for a kk-edge path, which mixes edge count kk and total weight, so it is not equivalent to minimising kk. To get fewest edges, use BFS (unit weights). (4: correct reasoning 2, concrete numbers 2)

(c) Bottleneck (minimax edge) STS\to T:

  • Path SBACTS\to B\to A\to C\to T: max edge =max(2,1,5,3)=5=\max(2,1,5,3)=5.
  • Path SBCTS\to B\to C\to T: max =max(2,8,3)=8=\max(2,8,3)=8.
  • Path SBDCTS\to B\to D\to C\to T: max =max(2,7,2,3)=7=\max(2,7,2,3)=7. Minimum bottleneck =5=\mathbf{5}, achieved by SBACTS\to B\to A\to C\to T. Adaptation: replace the "distance = sum" relaxation with bottleneck(v)=min(bottleneck(v), max(bottleneck(u),w(u,v)))\text{bottleneck}(v)=\min\big(\text{bottleneck}(v),\ \max(\text{bottleneck}(u),w(u,v))\big) and extract the vertex of minimum current bottleneck. (4: value 2, path 1, adaptation 1)

Question 2

(a) Bellman–Ford from 11. Order: (1 ⁣ ⁣2:3),(2 ⁣ ⁣3:2),(3 ⁣ ⁣4:2),(4 ⁣ ⁣2:2),(1 ⁣ ⁣3:5)(1\!\to\!2:3),(2\!\to\!3:-2),(3\!\to\!4:2),(4\!\to\!2:-2),(1\!\to\!3:5). Init: [0,,,][0,\infty,\infty,\infty] for (1,2,3,4)(1,2,3,4).

Pass 1:

  • 121\to2: d2=3d_2=3
  • 232\to3: d3=1d_3=1
  • 343\to4: d4=3d_4=3
  • 424\to2: d2=min(3,32)=1d_2=\min(3,3-2)=1
  • 131\to3: d3=min(1,5)=1d_3=\min(1,5)=1 Result: [0,1,1,3][0,1,1,3].

Pass 2:

  • 121\to2: min(1,3)=1\min(1,3)=1
  • 232\to3: min(1,12)=1\min(1,1-2)=-1
  • 343\to4: min(3,1+2)=1\min(3,-1+2)=1
  • 424\to2: min(1,12)=1\min(1,1-2)=-1
  • 131\to3: min(1,5)=1\min(-1,5)=-1 Result: [0,1,1,1][0,-1,-1,1].

Pass 3:

  • 121\to2: min(1,3)=1\min(-1,3)=-1
  • 232\to3: min(1,12)=3\min(-1,-1-2)=-3
  • 343\to4: min(1,3+2)=1\min(1,-3+2)=-1
  • 424\to2: min(1,12)=3\min(-1,-1-2)=-3
  • 131\to3: min(3,5)=3\min(-3,5)=-3 Result: [0,3,3,1][0,-3,-3,-1].

Pass 4 (|V|-th, the extra check pass):

  • 121\to2: 3-3 (no)
  • 232\to3: min(3,32)=5\min(-3,-3-2)=-5still decreasing
  • ... distances keep dropping. Result shows further relaxation, e.g. d3=5d_3=-5. (7: passes 2 each ≈ rounded to 7)

(b) Yes, a negative cycle reachable from 11 exists. Distances continue to decrease on pass 4 (the V|V|-th pass), which is the standard Bellman–Ford negative-cycle certificate. The cycle is 23422\to3\to4\to2 with weight 2+22=2<0-2+2-2=-2<0. (3)

(c) Floyd–Warshall gives all-pairs shortest paths in O(V3)O(V^3) and handles negative edges (no negative cycle). Running Bellman–Ford from every source costs O(VVE)=O(V2E)O(V\cdot VE)=O(V^2E); with EV2/4E\approx V^2/4 that is O(V4/4)O(V^4/4), worse than O(V3)O(V^3). So Floyd–Warshall is preferable on dense graphs when all-pairs distances are needed. (2)


Question 3

(a) Edges: abcaa\to b\to c\to a (cycle) and cdc\to d, defdd\to e\to f\to d (cycle). Kosaraju pass 1 (DFS, record finish times) from aa: visits a,b,c,d,e,fa,b,c,d,e,f; a valid finish order (last-finished first) is a,b,c,d,e,fa,b,c,d,e,f i.e. stack top aa. Pass 2 on transpose (ba,cb,ac,dc,ed,fe,dfb\to a, c\to b, a\to c, d\to c, e\to d, f\to e, d\to f):

  • Start aa: in GTG^T reach cc (via aca\to c? transpose of cac\to a gives aca\to c), bb, back — collects {a,b,c}\{a,b,c\}.
  • Next unvisited by finish order: dd: in GTG^T reaches f,ef,e (transposes of defdd\to e\to f\to d) — collects {d,e,f}\{d,e,f\}.

SCCs: {a,b,c}\{a,b,c\} and {d,e,f}\{d,e,f\}. (7)

(b) Condensation: two super-nodes X={a,b,c}X=\{a,b,c\}, Y={d,e,f}Y=\{d,e,f\} with single edge XYX\to Y (from cdc\to d). Topological order: X,YX, Y. (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 XYX\to Y = 1. (3)


Question 4

(a) Max flow. Edmonds–Karp (shortest augmenting paths):

  • Path suts\to u\to t (length 2), bottleneck min(10,5)=5\min(10,5)=5. Flow =5=5. Residual: su=5s\to u=5, ut=0u\to t=0.
  • Path svts\to v\to t (length 2), bottleneck min(5,10)=5\min(5,10)=5. Flow =10=10. Residual: sv=0s\to v=0, vt=5v\to t=5.
  • Path suvts\to u\to v\to t (length 3), bottleneck min(5,15,5)=5\min(5,15,5)=5. Flow =15=15. Residual: su=0s\to u=0, uv=10u\to v=10, vt=0v\to t=0.
  • No more augmenting paths (ss's out-edges saturated). Max flow =15=15. (6)

(b) Min cut: S={s}S=\{s\}, T={u,v,t}T=\{u,v,t\}. Cut edges su(10)s\to u(10), sv(5)s\to v(5), capacity =15=15. Equals max flow ✓. (3)

(c) Bipartite matching: x1 ⁣ ⁣y1, x3 ⁣ ⁣y2x_1\!-\!y_1,\ x_3\!-\!y_2 (or x3 ⁣ ⁣y3x_3\!-\!y_3), x2x_2 has only y1y_1 (taken). Better: x1 ⁣ ⁣y2, x2 ⁣ ⁣y1, x3 ⁣ ⁣y3x_1\!-\!y_2,\ x_2\!-\!y_1,\ x_3\!-\!y_3size 3 (perfect). Min vertex cover (König): must cover all edges; since matching size =3=3, min cover size =3=3, e.g. {y1,y2,x3}\{y_1, y_2, x_3\} covers every edge (x1x_1's edges to y1,y2y_1,y_2 ✓; x2x_2's y1y_1 ✓; x3x_3's edges via x3x_3 ✓). Cover size == matching size =3=3, 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 =1+2+2+4=9=1+2+2+4=9. (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: PPP\to P, QPQ\to P, RPR\to P, SPS\to P, USU\to S (or UPU\to P 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