← Exam 2 home

Official practice exam 2

Posted by the professor on Nov 4 with solutions. All 8 questions are DPV end-of-chapter exercises, word for word. Answers below follow the official solutions, rewritten as steps.

Ch 3 · Q1 (DPV 3.7) bipartite graphs

(a) Give a linear-time algorithm to decide whether an undirected graph is bipartite.

Show answer
  • Run DFS. Color each newly visited vertex the opposite color of its parent (blue/green). Restart with any color in each new tree.
  • Then scan every edge. If some edge has both ends the same color, the graph is not bipartite. Otherwise it is.

Answer: DFS 2-coloring plus an edge check, \(O(|V|+|E|)\).

(b) Prove: an undirected graph is bipartite if and only if it has no odd-length cycle.

Show answer
  • (⇒) Every edge switches sides, so a cycle gets back to its start only after an even number of steps.
  • (⇐) Prove the contrapositive: not bipartite ⇒ odd cycle.
  • Run the DFS coloring. Not bipartite, so some edge \(\{u,v\}\) has both ends the same color.
  • In an undirected DFS every non-tree edge is a back edge, so say u is an ancestor of v.
  • Same color means depth(v) − depth(u) is even, so the tree path from u to v has an even number of edges.
  • That path plus the edge \(\{u,v\}\) is a cycle with an odd number of edges.

(c) At most how many colors does a graph with exactly one odd cycle need?

Show answer
  • At least 3: an odd cycle can't be 2-colored.
  • 3 is enough. Delete one edge \(\{u,v\}\) of the odd cycle. Now there's no odd cycle, so 2-color it by (b).
  • If u and v ended up the same color, recolor v with the third color.

Answer: 3.

Ch 3 · Q2 (DPV 3.8) pouring water as a graph

Containers of 10, 7 and 4 pints. The 7 and 4 start full, the 10 starts empty. A pour stops when the source is empty or the destination is full. (a) Model as a graph problem. (b) Which algorithm?

Show answer
  • Vertex = state \((a,b,c)\) = pints in the (10, 7, 4) containers, with \(a+b+c=11\), \(0\le a\le10\), \(0\le b\le7\), \(0\le c\le4\).
  • Directed edge \(x\to y\) if one pour turns state x into state y.
  • Question: is some vertex with \(b=2\) or \(c=2\) reachable from \((0,7,4)\)?
  • Algorithm: explore (DFS) or BFS from \((0,7,4)\), linear in the size of the graph.
  • It is reachable: (0,7,4) → (4,7,0) → (10,1,0) → (6,1,4) → (6,5,0) → (2,5,4) → (2,7,2).

Answer: reachability in the state graph, solved with DFS or BFS. Yes, 2 pints is possible.

Ch 4 · Q3 (DPV 4.9) negative edges only out of s

Directed graph. The only negative edges leave s; all other edges are positive. Can Dijkstra from s fail? Prove it.

Show answer
  • As stated, yes: there could be a negative cycle through s (an edge into s plus a very negative edge out of s).
  • Say that first, then assume no negative cycle. Now Dijkstra works.
  • Dijkstra's proof needs one fact: when v has the smallest estimate outside the finished set S, the path through S to v is shortest.
  • Suppose some other path to v were shorter. With no negative cycles it can be taken simple, so it never returns to s.
  • It leaves S at some edge \((i,j)\). Its length up to j is at least j's estimate, which is at least v's estimate (v had the smallest).
  • Every edge after j is positive, because only edges out of s are negative. So the path to v is no shorter than v's estimate. Contradiction.

Answer: it can fail only if there's a negative cycle through s. With no negative cycle, Dijkstra is correct.

Ch 4 · Q4 (DPV 4.10) shortest paths with at most k edges

Directed graph, possibly negative edges. Every shortest path has at most k edges. Find the shortest u to v path in \(O(k|E|)\).

Show answer
  • Bellman–Ford, but stop after k rounds instead of \(|V|-1\).
  • Set dist(u) = 0, everything else ∞. Repeat k times: update every edge \((x,y)\): \(\text{dist}(y)=\min(\text{dist}(y),\ \text{dist}(x)+\ell(x,y))\), and record prev.
  • Invariant: after round i, dist is correct for every vertex whose shortest path has at most i edges. So k rounds are enough.
  • Follow prev back from v to get the path.

Answer: k rounds of Bellman–Ford, \(O(k|E|)\).

Ch 5 · Q5 (DPV 5.9 a–d) MST true or false

(a) G has more than \(|V|-1\) edges and a unique heaviest edge. Then that edge is in no MST.

Show answer
  • The heaviest edge can be a bridge, and every spanning tree must use a bridge.
  • Triangle a, b, c with weights 1, 2, 3, plus edge (c, d) of weight 10. \(|E| = 4 > |V|-1 = 3\), and (c, d) is in every MST.

Answer: false.

(b) If G has a cycle with a unique heaviest edge e, then e is in no MST.

Show answer
  • Suppose e = (u, v) is in an MST T. Removing e splits T into two parts.
  • The rest of the cycle goes from u to v, so it has another edge e′ crossing between the parts, and \(w(e') < w(e)\).
  • T − e + e′ is a lighter spanning tree. Contradiction.

Answer: true.

(c) Any minimum-weight edge e is in some MST.

Show answer
  • Run Kruskal and break ties so e comes first. The first edge Kruskal looks at is always added.
  • Or: cut property with a cut that separates e's endpoints. e is a lightest edge across it.

Answer: true.

(d) If the lightest edge is unique, it is in every MST.

Show answer
  • Suppose an MST T doesn't contain e. T + e has a cycle.
  • Every other edge on that cycle is strictly heavier than e. Remove one: the result is a lighter spanning tree. Contradiction.

Answer: true.

Ch 5 · Q6 (DPV 5.10) MST of a subgraph

T is an MST of G and H is a connected subgraph of G. Show that \(T\cap H\) is contained in some MST of H.

Show answer
  • Let \(F = T\cap H\).
  • Take any \(e\in F\). Removing e from T splits V into a cut \((S, V-S)\). Among T's edges, only e crosses it.
  • e is a lightest edge of G across this cut. If a crossing edge f had \(w(f) < w(e)\), then T − e + f would be lighter.
  • H's edges are a subset of G's, so e is still a lightest edge of H across the cut, and no other edge of F crosses it.
  • Add F's edges one at a time. Each step is the cut property in H: X ⊆ some MST of H, e lightest across a cut X doesn't cross ⇒ X ∪ {e} ⊆ some MST of H.

Answer: by induction with the cut property, F is in some MST of H.

Ch 6 · Q7 (DPV 6.14) cutting cloth

Cloth X × Y. Product i needs an \(a_i\times b_i\) piece and sells for \(c_i\). Any number of copies. A cut splits a piece horizontally or vertically. Find the best total return.

Show answer
  • Subproblem: \(P(x,y)\) = best return from an x × y piece, for \(1\le x\le X\), \(1\le y\le Y\).
  • Recurrence: \(P(x,y)\) is the best of three options (an empty max counts as 0):
    • Sell it whole: \(\max c_i\) over products with \(a_i\le x,\ b_i\le y\).
    • Cut the x side at h: \(\max_{1\le h<x} P(h,y)+P(x-h,y)\).
    • Cut the y side at w: \(\max_{1\le w<y} P(x,w)+P(x,y-w)\).
  • If products can be rotated, also allow \(b_i\le x,\ a_i\le y\).
  • Fill in increasing x, then y. Answer is \(P(X,Y)\).
  • XY subproblems, each \(O(X+Y+n)\) work.

Answer: \(O(XY(X+Y+n))\), pseudo-polynomial.

Ch 6 · Q8 (DPV 6.16) garage sales, a TSP-style DP

n garage sales, value \(v_j\) each, travel cost \(d_{ij}\) between sales, \(d_{0j}\) and \(d_{j0}\) to and from home. Find a tour of some subset, home to home, maximizing total value minus travel cost, in \(O(n^2 2^n)\).

Show answer
  • Subproblem: for \(S\subseteq\{1..n\}\) and \(j\in S\), \(C(S,j)\) = best (value − cost) of a path that starts at home, visits exactly the sales in S, and ends at \(g_j\).
  • Base: \(C(\{j\},j)=v_j-d_{0j}\).
  • Recurrence: \(C(S,j)=v_j+\max_{i\in S-\{j\}}\big[C(S-\{j\},i)-d_{ij}\big]\). Fill in order of increasing \(|S|\).
  • Answer: \(\max\{0,\ \max_{S,\,j\in S} C(S,j)-d_{j0}\}\). The 0 is staying home.
  • \(n2^n\) subproblems × \(O(n)\) each.

Answer: subset DP, \(O(n^2 2^n)\).

How Exam 1's practice paper compared to the real Exam 1

Practice Exam 1 askedReal Exam 1 asked insteadSource of the real one
Find the flaw in an induction proofQ1: prove \(F_n \ge 2^{n/2}\) by induction, largest cDPV 0.3
Sort functions by growth; prove/disprove a big-O identityQ2: prove a sum rule for Ω. MCQ 13–14: compare \(2^n\) vs \(2^{n+1}\), \(n^{0.1}\) vs \((\log n)^{10}\)Q2: not in DPV or the course files. MCQs: DPV 0.1
Modular inverses: DPV 1.15, 1.21 (how many invertible mod \(11^3\)), 1.22Q4: how many invertible mod \(p^n\). MCQ 15: inverse of 20 mod 79DPV 1.24. MCQ: DPV 1.20, solved in the Lecture 13 recitation
(nothing)Q3: \((N-1)! \not\equiv -1\) when N is compositeHW1 Problem 7 = DPV 1.35(c)
Is squaring an n-bit integer faster than multiplying?Q5: square a 2×2 matrix with 5 multiplications, find the flawDPV 2.27
Solve \(T(n)=T(\sqrt n)+1\)Q6: recursion tree for \(8T(n/3)+n^{8/3}\). MCQ 17: \(T(n)=T(n-1)+2\)Q6: not in DPV. MCQ: DPV 2.5
Sort an almost-sorted arrayMCQ 16: FFT of (1, 0, 0, 0)DPV 2.8
(nothing on ch 3)Q7: DFS topological sort trace, pre/post, count orderingsDPV 3.3, word for word
(no MCQs)MCQ 8–12: agents, determinism, belief state, search with costs and heuristicsPoole–Mackworth ch 1–3 definitions

So what Exam 2 will probably look like

Practice questionIts neighbors to knowWhere on this site
3.7 bipartite, 3.8 pouring water3.4 SCC trace, 3.9, 3.10, 3.11, 3.12, 3.13, 3.15, 3.16Topic 1, Likely next, HW3
4.9 negative edges out of s4.8 add a constant, 4.11/4.12 shortest cycle, 4.1 Dijkstra traceTopic 2
4.10 at most k edges4.13, 4.14, 4.17, 4.19, 4.20, 4.21 (HW3), 4.2 Bellman–Ford traceTopic 2, Likely next
5.9 (a)–(d) MST true/false5.9 (e)–(j): the same exercise, other parts. Very likely.Topic 3
5.10 MST of a subgraph5.5, 5.6, 5.7, 5.8, 5.22, 5.1/5.2 tracesTopic 3, Likely next
6.14 cutting cloth6.17–6.19 coin change (knapsack family), 6.2 hotels (HW4), 6.3Topic 4, Likely next
6.16 garage sales6.15, 6.13, 6.21, 6.20Likely next

What to take from this