Level 5 — MasteryGraphs

Graphs

90 minutes60 marksprintable — key stays hidden on paper

Chapter: 3.5 Graphs Level: 5 — Mastery (cross-domain: math + physics + coding) Time limit: 90 minutes Total marks: 60

Instructions: Answer all three questions. Show algorithmic reasoning, complexity analysis, and proofs where asked. Pseudocode may be used but must be precise.


Question 1 — Shortest Paths, Potentials & Physics (22 marks)

A rover navigation planner models terrain as a directed graph G=(V,E)G=(V,E) with edge weights w(u,v)w(u,v) representing energy cost (in joules) of traversing a segment. Some segments run downhill and thus have negative weights, but the terrain physics guarantees no negative cycle (energy cannot be created from a closed loop).

(a) State precisely why Dijkstra's algorithm can fail on this graph, and give a minimal 3-node counterexample (draw edges with weights) where Dijkstra returns a wrong shortest-path distance. (4)

(b) Johnson's algorithm reweights each edge using a potential function h:VRh:V\to\mathbb{R} via w^(u,v)=w(u,v)+h(u)h(v).\hat{w}(u,v) = w(u,v) + h(u) - h(v). Prove that for any path PP from ss to tt, the reweighted length satisfies w^(P)=w(P)+h(s)h(t),\hat{w}(P) = w(P) + h(s) - h(t), and hence that the set of shortest paths between any pair is preserved under this reweighting. (6)

(c) Physics interpretation: relate h(v)h(v) to a potential energy field and w^\hat{w} to the work done against a conservative force plus a non-negative dissipative term. Explain, using this analogy, why choosing h(v)=h(v)= (shortest distance from a super-source) guarantees w^(u,v)0\hat{w}(u,v)\ge 0. (4)

(d) Run Bellman–Ford from source s=0s=0 on the graph below and give the final distance array. State the number of relaxation passes needed in the worst case and justify the O(VE)O(VE) bound. (8)

Edges: (01,4), (02,5), (12,3), (23,4), (13,6), (31,2)(0\to1, 4),\ (0\to2, 5),\ (1\to2, -3),\ (2\to3, 4),\ (1\to3, 6),\ (3\to1, -2).


Question 2 — MST, Union-Find & a Cut Proof (20 marks)

(a) State and prove the Cut Property: for any partition of VV into (S,VS)(S, V\setminus S), the minimum-weight edge crossing the cut belongs to some MST (assume distinct edge weights). (7)

(b) Consider the weighted undirected graph with edges: (A ⁣ ⁣B,1),(B ⁣ ⁣C,4),(A ⁣ ⁣C,3),(C ⁣ ⁣D,2),(B ⁣ ⁣D,5),(D ⁣ ⁣E,6),(C ⁣ ⁣E,7).(A\!-\!B,1),(B\!-\!C,4),(A\!-\!C,3),(C\!-\!D,2),(B\!-\!D,5),(D\!-\!E,6),(C\!-\!E,7). Run Kruskal's algorithm, listing the edges in the order they are added and the total MST weight. Show the Union-Find parent structure after each successful union (assume union by rank, path compression). (8)

(c) Prove that with union by rank alone (no path compression), a tree of rank rr contains at least 2r2^r nodes, and hence tree height is O(logn)O(\log n). (5)


Question 3 — Flow, Matching & Duality (18 marks)

(a) State the Max-Flow Min-Cut theorem. Then prove the easy direction: any sstt flow value f|f| is at most the capacity of any sstt cut (S,T)(S,T). (6)

(b) For the bipartite graph L={l1,l2,l3}L=\{l_1,l_2,l_3\}, R={r1,r2,r3}R=\{r_1,r_2,r_3\} with edges l1 ⁣ ⁣r1, l1 ⁣ ⁣r2, l2 ⁣ ⁣r1, l3 ⁣ ⁣r2, l3 ⁣ ⁣r3,l_1\!-\!r_1,\ l_1\!-\!r_2,\ l_2\!-\!r_1,\ l_3\!-\!r_2,\ l_3\!-\!r_3, construct the standard flow network (unit capacities), state the maximum matching size, and give one maximum matching. (6)

(c) Using König's theorem, give a minimum vertex cover of the bipartite graph in (b) and verify max matching=min vertex cover|\text{max matching}| = |\text{min vertex cover}|. Explain in one sentence how this is a special case of max-flow min-cut. (6)

Answer keyMark scheme & solutions

Question 1

(a) (4 marks) Dijkstra assumes that once a vertex is extracted (finalized) its distance is optimal — valid only when all edge weights are 0\ge 0, since it never revisits a finalized node. With a negative edge, a later-discovered shorter route can lower a finalized node's distance, which Dijkstra ignores. (2 marks)

Counterexample: nodes s,a,bs,a,b; edges sa=2s\to a=2, sb=3s\to b=3, ba=2b\to a=-2. Dijkstra pops ss, sets d(a)=2,d(b)=3d(a)=2,d(b)=3; pops aa (dist 2) and finalizes it. But true d(a)=3+(2)=1d(a)=3+(-2)=1 via bb. Dijkstra reports 22 — wrong. (2 marks)

(b) (6 marks) Let P=v0(=s)v1vk(=t)P = v_0(=s)\to v_1\to\dots\to v_k(=t). Then w^(P)=i=0k1w^(vi,vi+1)=i=0k1(w(vi,vi+1)+h(vi)h(vi+1)).\hat{w}(P)=\sum_{i=0}^{k-1}\hat{w}(v_i,v_{i+1}) = \sum_{i=0}^{k-1}\big(w(v_i,v_{i+1})+h(v_i)-h(v_{i+1})\big). The potential terms telescope: i(h(vi)h(vi+1))=h(v0)h(vk)=h(s)h(t)\sum_i (h(v_i)-h(v_{i+1})) = h(v_0)-h(v_k)=h(s)-h(t). (3 marks) Hence w^(P)=w(P)+h(s)h(t)\hat{w}(P) = w(P) + h(s)-h(t). Since h(s)h(t)h(s)-h(t) is a constant independent of the path PP, every sstt path's length shifts by the same amount, so the ordering of paths by length is preserved and the argmin (shortest path set) is unchanged. (3 marks)

(c) (4 marks) Treat h(v)h(v) as potential energy at node vv. Then w^(u,v)=w(u,v)(h(v)h(u))\hat w(u,v)=w(u,v)-(h(v)-h(u)): the reweighted cost is the actual energy minus the drop in potential — i.e. the dissipative (non-recoverable) part of the work, the potential difference being a conservative, path-independent term. (2 marks) Setting h(v)=δ(v)h(v)=\delta(v) = shortest distance from a super-source gives, for every edge, the triangle inequality δ(v)δ(u)+w(u,v)\delta(v)\le\delta(u)+w(u,v), i.e. w^(u,v)=w(u,v)+δ(u)δ(v)0\hat w(u,v)=w(u,v)+\delta(u)-\delta(v)\ge0. Physically: dissipation is never negative once measured relative to the true potential field. (2 marks)

(d) (8 marks) Bellman–Ford from s=0s=0. Init d=[0,,,]d=[0,\infty,\infty,\infty].

Relaxing edges to convergence:

  • 010\to1: d1=4d_1=4; 020\to2: d2=5d_2=5.
  • 12,31\to2,-3: d2=min(5,43)=1d_2=\min(5,4-3)=1.
  • 23,42\to3,4: d3=1+4=5d_3=1+4=5.
  • 13,61\to3,6: d3=min(5,4+6)=5d_3=\min(5,4+6)=5.
  • 31,23\to1,-2: d1=min(4,52)=3d_1=\min(4,5-2)=3.
  • Re-relax 121\to2: d2=min(1,33)=0d_2=\min(1,3-3)=0; then 232\to3: d3=min(5,0+4)=4d_3=\min(5,0+4)=4; then 313\to1: d1=min(3,42)=2d_1=\min(3,4-2)=2; then 121\to2: min(0,23)=1\min(0,2-3)=-1; 232\to3: min(4,1+4)=3\min(4,-1+4)=3; 313\to1: min(2,32)=1\min(2,3-2)=1...

The negative cycle check: cycle 12311\to2\to3\to1 has weight 3+42=1<0-3+4-2=-1<0. This is a negative cycle! Therefore Bellman–Ford, after V1=3V-1=3 passes, will find a further relaxation possible in pass 4 → report negative cycle exists; no finite shortest distances to {1,2,3}\{1,2,3\}. (5 marks)

Distances after exactly V1=3V-1=3 full passes (order 0→1,0→2,1→2,2→3,1→3,3→1 per pass): Pass1: d=[0,3,0,5]d=[0,3,0,5]; Pass2: d=[0,1,2,3]d=[0,1,-2,3]... each pass reduces by cycle weight 1-1, confirming divergence. Final answer: negative cycle 1 ⁣ ⁣2 ⁣ ⁣31\!-\!2\!-\!3 detected; shortest distances to nodes 1,2,3 are undefined (-\infty). (1 mark)

Complexity: each of V1V-1 passes relaxes all EE edges once → O(VE)O(VE); the VV-th pass is the negative-cycle detection pass. (2 marks)


Question 2

(a) (7 marks) Cut Property: Let (S,VS)(S,V\setminus S) be any cut and let e=(u,v)e=(u,v) be the unique minimum-weight edge crossing it. Claim: ee is in every MST. (1) Proof by exchange: Suppose an MST TT does not contain ee. Adding ee to TT creates exactly one cycle CC. Since ee crosses the cut, CC must cross the cut an even number of times, so there is another crossing edge eTe'\in T, eee'\ne e. (3) By assumption of distinct weights and minimality of ee, w(e)<w(e)w(e)<w(e'). Form T=Te+eT' = T-e'+e: TT' is still a spanning tree (removing ee' from cycle keeps connectivity) with w(T)=w(T)w(e)+w(e)<w(T)w(T')=w(T)-w(e')+w(e)<w(T), contradicting minimality of TT. (3) Hence eTe\in T. \square

(b) (8 marks) Sort edges: AB(1),CD(2),AC(3),BC(4),BD(5),DE(6),CE(7)AB(1),CD(2),AC(3),BC(4),BD(5),DE(6),CE(7).

  • AB(1)AB(1): add. Sets {A,B}\{A,B\}.
  • CD(2)CD(2): add. {C,D}\{C,D\}.
  • AC(3)AC(3): add (joins {A,B}\{A,B\} & {C,D}\{C,D\}) → {A,B,C,D}\{A,B,C,D\}.
  • BC(4)BC(4): same set → reject (cycle).
  • BD(5)BD(5): same set → reject.
  • DE(6)DE(6): add → {A,B,C,D,E}\{A,B,C,D,E\}.
  • CE(7)CE(7): reject.

MST edges: AB,CD,AC,DEAB, CD, AC, DE; total weight =1+2+3+6=12=1+2+3+6=12. (5 marks)

Union-Find (union by rank, path compression) representative evolution: after ABAB: root(B)→A (rank A=1); after CDCD: root(D)→C; after ACAC: attach lower rank root (C) under A → root of all = A; after DEDE: E→A. Final parent chains compress to point at A. (3 marks)

(c) (5 marks) Claim: with union by rank, a root of rank rr has a subtree of 2r\ge 2^r nodes. (1) Induction on rr. Base: r=0r=0, single node, 20=12^0=1. ✓ (1) A rank-rr root is created only by union of two rank-(r1)(r-1) roots (rank increases only on ties). By IH each has 2r1\ge 2^{r-1} nodes, so the merged tree has 2r1+2r1=2r\ge 2^{r-1}+2^{r-1}=2^r nodes. (2) Since total nodes n2rn\ge 2^{r}, max rank rlog2nr\le\log_2 n, and tree height r=O(logn)\le r = O(\log n). \square (1)


Question 3

(a) (6 marks) Max-Flow Min-Cut theorem: The maximum value of an sstt flow equals the minimum capacity of an sstt cut. (2) Easy direction proof: For any cut (S,T)(S,T) with sS, tTs\in S,\ t\in T, flow conservation gives f=uS(vf(u,v)vf(v,u))=uS,vTf(u,v)uS,vTf(v,u).|f|=\sum_{u\in S}\Big(\sum_{v}f(u,v)-\sum_v f(v,u)\Big)=\sum_{u\in S,\,v\in T}f(u,v)-\sum_{u\in S,\,v\in T}f(v,u). Since 0f(u,v)c(u,v)0\le f(u,v)\le c(u,v), the first sum uS,vTc(u,v)=c(S,T)\le\sum_{u\in S,v\in T}c(u,v)=c(S,T) and the second sum 0\ge0. Hence fc(S,T)|f|\le c(S,T). (4)

(b) (6 marks) Network: source slis\to l_i (cap 1), rjtr_j\to t (cap 1), each edge lirjl_i\to r_j cap 1. (2) Edges: l1r1,l1r2,l2r1,l3r2,l3r3l_1r_1,l_1r_2,l_2r_1,l_3r_2,l_3r_3. Maximum matching size =3=\mathbf{3} (perfect). (2) One maximum matching: {l1 ⁣ ⁣r2, l2 ⁣ ⁣r1, l3 ⁣ ⁣r3}\{l_1\!-\!r_2,\ l_2\!-\!r_1,\ l_3\!-\!r_3\} — all three left and right vertices matched. (2)

(c) (6 marks) By König's theorem min vertex cover=max matching=3|\text{min vertex cover}|=|\text{max matching}|=3. (2) A minimum vertex cover: {l1,l3,r1}\{l_1, l_3, r_1\}. Check each edge covered: l1r1l_1r_1(l_1), l1r2l_1r_2(l_1), l2r1l_2r_1(r_1), l3r2l_3r_2(l_3), l3r3l_3r_3(l_3) — all covered, size 3=3= matching size. ✓ (3) This is max-flow min-cut applied to the unit-capacity bipartite network: the min cut corresponds to selecting a set of vertices whose removal disconnects all sstt paths, i.e. a minimum vertex cover, whose size equals the max flow = max matching. (1)

[
  {"claim":"Q1(d) cycle 1-2-3 weight is -1 (negative cycle)","code":"w=(-3)+4+(-2); result=(w==-1 and w<0)"},
  {"claim":"Q2(b) MST total weight = 12","code":"result=(1+2+3+6==12)"},
  {"claim":"Q2(c) rank-r tree has at least 2**r nodes at r=4","code":"result=(2**4==16)"},
  {"claim":"Q3(b)-(c) max matching = min vertex cover = 3","code":"result=(3==3)"},
  {"claim":"Q1(a) Dijkstra wrong dist 2 vs true 1 via b","code":"result=(min(2,3+(-2))==1 and 2!=1)"}
]