COMP 550 · Final Exam Cheat Sheet

Course Ch. 1–8 except 7 (arrays, graphs, greedy, DP, shortest paths, flows, approximation) · Thu Dec 10, 2026, 4–7 pm, Chapman 211 · 3 h, closed-everything, 8 × 5 = 40 pts · P1–P2 = five 1-pt parts each, answer only · P3–P8 = problem-set style, ≥ 2 identical to PS problems

Writing an answer

Recipe. (1) Algorithm: 1–2 sentences of idea + pseudocode in the lecture idiom (4–12 lines, call listed algorithms by name, a "slight modification" in one clear sentence). (2) Correctness: a named argument (invariant/induction, both directions, exchange, cut/cycle property, reduction correspondence). (3) Running time: iterations × time each; named calls at their notes' bound on the new size; must reach the target. DP: Subproblems (+ what is returned) · Recurrence + base cases + why it holds (= the proof) · Algorithm (fill d) · Time = #subproblems × time each. Approx: feasibility · bound on OPT · ALG in same terms · ratio.
Question typeWhat to producePts
P1–P2 trace (d array, post, edge order, flow value, makespan)final answer only, requested format: [a, b, c], {u, v}, (u, v), "nothing"1 each
P1–P2 True/False on ratiosmin α-approx is β-approx for β ≥ α; max for β ≤ α1 each
P3–P8 designalgorithm, correctness, running time5
P3–P8 "prove α-approx" / "prove or refute"Yes/No first; proof or instance + ALG + OPT + ratio worse than α5

Cover page: no hashing (sets = 0/1 arrays), indices from 1, A[1]..A[n], n = |V|, m = |E|, adjacency lists, ties to smallest label, only boxes graded.

Arrays (course Ch. 1)

Max: m = max(m, A[i]); invariant m = max(A[1:i]); O(n). Binary-Search: m = ⌊(i+j)/2⌋ while i ≤ j; A[m] < t ⇒ i = m+1 else j = m−1; O(log n); [2,4,7,9,12,15,20], t=5 → m = 4, 2, 3, nothing. Selection-Sort round i swaps min of A[i:n] in, n(n+1)/2 = O(n²). Merge-Sort: halves + merge (ties left), T(n) = 2T(n/2) + O(n) = O(n log n).

Two-Sum(A, t):     # sorted A, O(n)
  i, j = 1, n
  while i < j:
    if A[i] + A[j] = t: return (i, j)
    if A[i] + A[j] < t: i += 1
    else: j -= 1
  return nothing   # a solution always stays in A[i:j]

[1,4,5,7], t=5 → (1, 2). Container (PS 1.4): start (1, n), record (j−i)·min, move the shorter wall; O(n); [1,3,2,5,2,2] → (2, 6), 8. PS 1.1 repeated element: sum − n(n+1)/2 or 0/1 array B; 1.2 sorted squares: two ends, fill from back; 1.3 sort A, binary-search each b, O(n log m); 1.5 sort, min consecutive gap.

Graphs (course Ch. 2)

Explore(G, u):     # DFS: for u = 1..n, if unvisited, Explore
  pre[u] = t; t += 1
  for v in G[u]:        # label order
    if pre[v] = inf: Explore(G, v)
  post[u] = t; t += 1   # clock never restarts

BFS: queue; first discovery d[v] = d[u]+1, p[v] = u; ∞ unreachable; O(m+n). [[2,4],[3,5,6],[6],[5,7],[7],[8],[],[5]] → d = [0,1,2,1,2,2,2,3]. DFS [[2,3],[3,4],[],[1,3]] → pre [1,2,3,5], post [8,7,4,6]. Edges: tree/fwd pre u < pre v < post v < post u; back pre v < pre u < post u < post v; cross pre v < post v < pre u < post u. Find-Cycle: cycle ⇔ back edge (both directions). Topo-Sort: decreasing post (push u to left of R at post); post[u] > post[v] ∀(u, v); not increasing pre ([[2],[],[2]]). SCC: DFS on GR, BFS in G from unlabelled vertices by decreasing post (sink SCCs first); [[2],[3,4],[1,5],[5],[]] → c = [3,3,3,2,1]; not callable by name. All O(m+n) (Σ out-deg = m). PS 2: 2.1 diameter = n BFS, O(mn); 2.2/2.5 topo order T, check (T[i], T[i+1]) ∉ E; 2.3 BFS wrapper labels components; 2.4 bipartite = BFS 2-coloring c[v] = 1 − c[u], fail on equal colors.

Greedy (course Ch. 3)

Max k-Sum: sort decreasing, A[1:k], O(n log n); [4,5,1,7,2], k=3 → [7,5,4]. Intervals: sort by end, add if start > last chosen end (closed); O(n log n). Fractional Knapsack: sort v/w decreasing, take what fits, ≤ 1 fractional; ε-exchange; O(n log n); v=[3,4,5], w=[2,2,1], B=4 → x = [0.5, 1, 1] (item order).

Cut Property: lightest edge crossing any cut S is in the MST. Cycle Property: heaviest edge on any cycle is not. (Distinct weights ⇒ unique MST; exchange T + e − f, f on the cycle.)

Prim: S = {1}; n−1 times add lightest edge crossing S; O(mn) = O(m²); order not sorted (square {1,2}5 {1,4}4 {1,3}3 {2,3}2 {3,4}1 → {1,3}, {3,4}, {2,3}). Kruskal: increasing, keep e if F + e acyclic (BFS on F, O(n)); O(m log m) + m·O(n) = O(m²). Reverse-Delete: decreasing, delete e if G − e connected; O(m log m) + m·O(m) = O(m²). PS 3: 3.2 shortest job first; 3.3 sort by deadline; 3.4 MST after w(e) up: remove e, add lightest edge across the cut, O(m+n); 3.5 coins: dimes, nickels, pennies greedily.

Dynamic programming (course Ch. 4)

PatternOPT forFill / return
1 prefixA[1:i]1..n / OPT[n]
2 suffixA[i:n]n..1 / OPT[1]
3 LIS trickA[1:i], ends on A[i]1..n / max(d)
4 intervalA[i:j]i = n..1 / OPT[1][n]
5 budget(A[1:i], j)rows / OPT[n][B]
6 two strings(A[1:i], B[1:j])rows / OPT[m][n]
7–8 treeTu (in/out)children first / root
LIS(A): d = [1]*n                        # O(n^2)
  for i = 2..n: for j = 1..i-1:
    if A[j] < A[i]: d[i] = max(d[i], 1 + d[j])
  return max(d)                          # not d[n]
LPS: d[i][i] = 1; A[i] = A[j]: d[i+1][j-1] + 2
     else max(d[i+1][j], d[i][j-1]); i = n..1   # O(n^2)
Knapsack(v, w, B): d[1][j] = v[1] if w[1] <= j else 0
  for i = 2..n: for j = 0..B:
    d[i][j] = d[i-1][j]
    if w[i] <= j: d[i][j] = max(d[i][j], v[i] + d[i-1][j-w[i]])
  return d[n][B]                         # O(nB), not polynomial
Edit-Distance(A, B): d[i][0] = i; d[0][j] = j
  for i = 1..m: for j = 1..n:  δ = 0 if A[i] = B[j] else 1
    d[i][j] = min(d[i][j-1]+1, d[i-1][j]+1, d[i-1][j-1]+δ)
  return d[m][n]          # left=insert, up=delete; O(mn)

LIS [5,1,2,7,4,6,3] → d = [1,1,2,3,3,4,3], 4. LPS acbcbda → 5; row 1 entry j = LPS of A[1:j]. Knapsack v=[4,2,3,5], w=[3,1,2,2], B=5 → 10; unbounded: d[i][j−w[i]]; one array: j downward. ED(star, water) = 3. MIS in trees: in[u] = w(u) + Σ out[v]; out[u] = Σ max(in[v], out[v]); leaf (w, 0); O(n); lecture → 21. One array: max(Σ OPT[children], w[u] + Σ OPT[grandchildren]). Tree matching: out[u] = Σ OPT[v]; OPT[u] = max(out[u], maxv w(u,v) + out[v] + out[u] − OPT[v]); O(n); lecture → 9. Alice & Bob: OPT[i] = S[i] − min(OPT[i+1..i+3]), S[i] = sum A[i:n]; → 18.

PS 4 (all O(n) but 4.4). 4.1 d[i] = d[i−1] + A[i]. 4.2 d[i] = d[i−1]+1 if A[i] > A[i−1] else 1, max. 4.3 d[i] = A[i] + min(d[i−1], d[i−2]), return min(d[n−1], d[n]). 4.4 grid paths: d[i][j] = d[i−1][j] + d[i][j−1] (0 on obstacle), O(mn). 4.5 stock: d[j] = A[j] − running min, max (= PF 3, Midterm 2 P2). 4.6 d[i] = max(d[i−1], A[i] + d[i−2]). 4.7 d[0] = 1, d[i] = d[i−A] + d[i−B].

Shortest paths (course Ch. 5)

DAG-DP(G, s): d = [inf]*n; d[s] = 0      # O(m+n), any lengths
  for v in topological order: for u in G_R[v]:
    d[v] = min(d[v], d[u] + l(u, v))     # max = longest path
Bellman-Ford: d[s][0] = 0, d[v][0] = inf  # no neg cycle, O(mn)
  for j = 1..n-1: for v:
    d[v][j] = min(d[v][j-1],
                  min over u in G_R[v]: d[u][j-1] + l(u,v))
  return column n-1     # walk ≤ j edges; k columns = O(mk)
Dijkstra(G, s): d = [inf]*n; d[s] = 0     # all l ≥ 0, O(n^2)
  repeat n times: u = vertex not in S with min d; add u to S
    for v in G[u]: d[v] = min(d[v], d[u] + l(u, v))
Floyd-Warshall: d[u][v][0] = 0 / l(u,v) / inf   # O(n^3)
  for r = 1..n: for u: for v:
    d[u][v][r] = min(d[u][v][r-1], d[u][r][r-1] + d[r][v][r-1])

Lecture answers: DAG-DP [0, −6, 10, −4]; BF [0, 2, 1, 5]; Dijkstra (1,2)3 (1,4)10 (2,3)1 (2,4)4 (3,4)2 (3,5)3 (4,5)1 → order 1,2,3,4,5, d = [0,3,4,6,7]. Dijkstra proof: induction, d[u] = δ(u) when u joins S (needs ℓ ≥ 0); heap O(m log n). FW: r = "vertices 1..r may be intermediate". PS 5: 5.1 widest path: DAG-DP with max of min(d[u], w), O(m+n); 5.2 one negative edge (u,v): remove it, Dijkstra from s and v, min(ds[t], ds[u] + ℓ + dv[t]), O(n²); 5.3 even walk: copies u₀, u₁, edges (u₀,v₁), (u₁,v₀), Dijkstra from s₀, d[t₀], O(n²); 5.4 shortest cycle: FW, min d[u][v] + d[v][u], O(n³). PF 5: BF to column 10, O(m).

Flows and cuts (course Ch. 6)

Feasible: 0 ≤ f(e) ≤ c(e), conservation at u ≠ s, t; |f| = fout(s). Cut S ∋ s, ∌ t; c(S) = edges leaving S only. Flow-Cut Lemma: |f| = fout(S) − fin(S) ≤ c(S). Max-Flow = Min-Cut.

Ford-Fulkerson(G, s, t): f = 0           # O(mv), v = max flow
  while G_f has an s-t path P (DFS):
    Δ = min residual capacity on P
    f += Δ on forward edges, f -= Δ on backward edges
# G_f: (u,v) with c-f if f < c; (v,u) with f if f > 0

Main example (1,2)3 (1,3)12 (2,3)8 (2,4)1 (3,4)3 (3,5)5 (4,5)15 → 9, S = {1,2,3}. PF 1a → 7, cut {1,3}. Min cut: S = reachable from s in last Gf, O(mv). Bipartite matching: s→L cap 1, L→R cap n, R→t cap 1; edges with f = 1; O(mn). Bipartite VC: same network, min cut S, C = (L \ S) ∪ (R ∩ S), |C| = c(S), O(mn). PS 6: 6.1 global min cut: fix s, min-cut both ways to every t, O(mn²); 6.2 remove k edges leaving a min cut, O(mn); 6.3 vertex capacity: uin→uout cap c(u), O(mv); 6.4 s-t path through u: flow from u of value 2, unit vertex caps, O(m); 6.5 baseball: game and team nodes, O(n²v).

Approximation (course Ch. 8)

Min (α ≥ 1): OPT ≤ ALG ≤ α·OPT. Max (α ≤ 1): OPT ≥ ALG ≥ α·OPT. Any polynomial time ("an analysis is not even necessary").

AlgorithmRatioOPT bound used
VC: take both ends of any remaining edge, delete them2|OPT| ≥ |M| (picked edges = matching); |S| = 2|M|
Load Balancing: jobs in order, to least-loaded machine2 (2 − 1/m)T* ≥ Σℓ/m, T* ≥ ℓ[k]; T ≤ Σℓ/m + ℓ[k]
Metric k-Center: S = {1}, add farthest point k−1 times2same-cluster points within 2r* (triangle ineq.), pigeonhole
Max Cut: S = ∅, move any vertex that raises w(δ(S))1/2OPT ≤ w(E); α(u) ≥ w(δ(u))/2 ⇒ 2·ALG ≥ w(E)

VC greedy by max degree fails (spider: 4 vs 3). LB [5,1,4,2,3], m=3 → 6, OPT 5; PF 2c [3,1,2,3] → 4. k-Center points 0,2,9,10,15,18, k=3 → 1,6,3. Max Cut ≤ w(E) iterations: not polynomial. PS 8: 8.1 greedy matching 1/2 (= PF 4): charge each e* ∈ M* to an M-edge sharing an endpoint, ≤ 2 each; 8.2 tight LB: m(m−1) unit jobs then one of length m; 8.3 sorted LB 3/2: extra T* ≥ 2ℓ[m+1]; 8.4 Dominating Set via 1.9-approx k-Center (d = 1 adjacent, 2 otherwise).

Which method when

SituationUseCost
pair/window in arraytwo pointersO(n)
unit distances, components, 2-colorBFSO(m+n)
input is a DAGtopological sort / DAG-DPO(m+n)
MST, "is e in MST", updatePrim + cut/cycle propertyO(m²) / O(m)
ℓ ≥ 0, one sourceDijkstraO(n²)
negative ℓ, no neg cycleBellman-FordO(mn)
≤ k edgesk BF columnsO(mk)
all pairs, cyclesFloyd-WarshallO(n³)
parity, one special edge2 copies of G + DijkstraO(n²)
disjoint paths, matching, vertex caps, cut k edgesflow network / min cutO(mv)
no known poly optimal (VC, LB, k-center, max cut)approximation + OPT boundpoly
Needs: Dijkstra ℓ ≥ 0; BF/FW no negative cycle; DAG-DP and topo a DAG; Two-Sum/Binary-Search sorted; MST algorithms connected, undirected, distinct weights; k-Center triangle inequality.

Running times (notes)

O(log n)Binary-Search
O(n)Max, Two-Sum, container, PS 4 (not 4.4), tree DPs, PF 7
O(n log n)Merge-Sort, Max k-Sum, Intervals, Fractional Knapsack
O(n²)Selection-Sort, LIS, LPS, Dijkstra (heap O(m log n))
O(m+n)BFS, DFS, Explore, Find-Cycle, Topo-Sort, SCC, DAG-DP
O(mn)Bellman-Ford, Edit Distance (m, n = lengths), matching, bipartite VC, PS 2.1
O(m²)Prim O(mn); Kruskal O(m log m)+m·O(n); Rev-Delete O(m log m)+m·O(m)
O(nB)Knapsack-DP (pseudo-polynomial)
O(n³)Floyd-Warshall, PS 5.4
O(mv)Ford-Fulkerson, Min-Cut, PS 6.3

Proof templates

Invariant: claim after iteration i · base · step · conclusion at the end. "Return X or nothing": prove both directions. Exchange: OPT ≠ ALG; first difference i; OPT′ = OPT with OPT[i] → ALG[i]; OPT′ feasible and no worse (name the inequality). Cut property: "e is the lightest edge crossing S = (component of u), so e ∈ MST." Cycle property: "e is the heaviest edge on cycle P + e, so e ∉ MST." DP: "the optimal solution for OPT[i] either … or …; in the first case its value is …, in the second …; these cases are exhaustive." Reduction: build G′ · call · translate; every original solution ↔ a G′ solution of equal value (both directions); time = build + call on new size. Approx ratio: feasible · OPT bound · ALG in same terms · chain. Counterexample: "No." · instance · ALG's allowed run and value · OPT · ratio strictly worse than α.

Traps

Algorithm without why it works. One direction only. "OPT′ is better" without feasibility. Recurrence without the why; vague OPT. Dijkstra on a negative edge. n per vertex instead of out-deg (adjacency lists). ∞ for unreachable. DFS clock keeps running. Decreasing post, not increasing pre. SCC: DFS on GR, BFS on G. Prim edges in chosen order. Kruskal cycle test on F. Interval endpoints included. x in item order; sort by ratio. LIS: max(d). LPS from the bottom row. ED last row starts with m. Knapsack reads row above; one array sweeps j down. Trees: children first. "After round j" ≠ column j. c(S) only leaving edges; saturated ≠ cut edge. VC = (L \ S) ∪ (R ∩ S). Max problems flip ≤. Ratio exactly α is not a counterexample. Right trace, wrong format. Write inside the boxes.