Final Practice Sols
Final Practice Sols
1
Problem 2 (Allocating Jobs to Machines).
You are given 3n jobs and have 3 machines named A, B, C. Each job j has “benefits” a j , b j , c j if assigned to
A, B, or C, respectively. Assume these are positive integers.
Design an efficient algorithm to allocate exactly1 n jobs to each machine such that the sum of benefits of
these jobs is maximized.
• Definition. F(i, j, k) be the maximum benefit that can be obtained from allocating the first (i + j + k)
jobs such that exactly i jobs are on machine A, exactly j jobs on machine B, and exactly k jobs on
machine C. We are interested in F(n, n, n).
To clarify, we take the max of all things which “pass” the if-statements.
• Reason. The (i + j + k)th job must be in one of the three machines...and if so, we piggy back to the
residual world. (I am being sketchy here)
• Runtime O(n3 ).
Solution: Ok, this is just asking whether or not there is a walk of length exactly 43 from a to b in G.
To solve this, we can make 43 copies of all vertices; name them V0 ∪ V1 ∪ V2 ∪ · · · ∪ V42 . Each vertex v ∈ V
has 43 copies, with copy vi in the set Vi . For each original (x, y) ∈ E, we add 42 edges, namely, (xi , yi+1 ) for
0 ≤ i ≤ 41. In this new graph, H, we need to figure out if there is a path from a0 to b42 . If so, then we have
a walk from a to b of length 42 obtained by “wiping out the subscripts”.
One can also (equivalently) solve this by dynamic programming, a la LBSW, by having d[v, i] ∈ {0, 1} which
is 1 if and only if there is a walk from a to v of length exactly i. We wish to know if d[b, 42] = 1 or not. Base
case: d[a, 0] = 1 and d[v, 0] = 0 otherwise. d[v, j] = 1 if and only if d[u, j − 1] = 1 for some u ∈ V such that
(u, v) ∈ E. Figuring this out takes deg− (v) time, and so figuring out d[v, j] for all v takes O(m) time. Since
0 ≤ j ≤ 42, we get all total O(m) time.
1
if this constraint wasn’t there, then we would allocate j to whichever machine gave the largest benefit
2
Problem 4 (Bottleneck of a Graph).
You are given a weighted directed graph G = (V, E) with every edge having a unique positive capacity w(e).
Assume m ≥ n. Given a path p in G, the bottleneck edge of the path is the one with minimum capacity, that
is,
bottleneck(p) := min w(e)
e∈p
What is the fastest time algorithm you can design to determine the largest value k such that for any two
vertices x, y ∈ V(G), there is some path p from x to y with bottleneck(p) ≥ k?
Bonus: Solve this problem in O(m) time if G is undirected. Y
Solution: In one of the W7 problems, you solved the problem of bottleneck paths: given any s ∈ V, for
every x ∈ V \ s you could find u(x) which is the bottleneck of the largest bottleneck path from s to x using a
modification of Dijkstra; so this can be done in O(m + n log n) time. Let k1 := min x∈V\s u(x); then, for every
vertex x, there is at least one path from s to x with bottleneck ≥ k1 .
Now, we reverse the graph G and run the same algorithm to get k2 . We would then get that for every x,
there is at least one path from s to x in Greverse , that is, a path from x to s in G with bottleneck ≥ k2 . (This
was the same idea as running 2 DFS for figuring out strong connectivity). The answer we are looking for is
k := min(k1 , k2 ) – for any x and y, there is a bottleneck ≥ k path from x to s and another from s to y. If we
take the concatenation of these paths to get a walk, and find the path from x to y by short-cutting, this path
will have bottleneck ≥ k as well.
Bonus. (Sketch). There are two-three main ideas:
• First, given a k we can check in O(m) time if for every x and y there is a path with bottleneck at
least k: we first take O(m) time and delete all edges of capacity ≤ k getting a graph Gk . Run strong
connectivity on the remainder. If Gk is connected, then the answer we are looking for is ≥ k, and we
recurse on Gk .
• If Gk is not connected, then the answer we are looking for is < k. Cruciall ynote: we can contract, a la
Boruvka, all components in Gk . This is where undirected-ness is being used. Let this resulting graph
be Hk and we need to recurse our problem on Hk .
• So, if we can find a k such that no matter what case we land in, the resulting graph has “half” the
edges, then we would be done. So, we pick k to be the median of all edge weights — this can be done
in O(m) time as well! So, the full algorithm is