You are on page 1of 3

CMPT 405/705 Design and Analysis of Algorithms

Outline Solutions to Exercises on Dynamic Programming and Network Flow


1. Suppose that you are given an n n checkerboard and a checker. You must move the checker from the
bottom edge of the board to the top edge of the board according to the following rule. At each step you
may move the checker to one of three squares:
the square immediately above,
the square that is one up and one to the left (but only if the checker is not already in the leftmost
column),
the square that is one up and one to the right (but only if the checker is not already in the rightmost
column).
Each time you move from square x to square y, you receive p(x, y) dollars. You are given p(x, y) for all
pairs (x, y) for which a move from x to y is legal. Do not assume that p(x, y) is positive.
Give an algorithm that figures out the set of moves that will move the checker from somewhere along the
bottom edge to somewhere along the top edge while gathering as many dollars as possible. Your algorithm
is free to pick any square along the bottom edge as a starting point and any square along the top edge as
a destination in order to maximize the number of dollars gathered along the way. What is the running
time of your algorithm?
Denote each square by the pair (i, j) where i is the row number, j is the column number, and 1 i, j n. Our
goal is to find a most profitable way from any square in row 1 to any square in row n. Once we do so, we can
look up all the most profitable ways to get to any square in row n and pick the best one.
A subproblem is the most profitable way to get from some square in row 1 to a particular square (i, j). We have
optimal substructure as follows. Consider a subproblem for (i, j), where i > j, and consider the most profitable
way to (i, j). Because of how we define legal moves, it must be through square (i 1, j 0 ), where j 0 = j 1,
j, or j + 1. then the way we got to (i 1, j 0 ) within the most profitable way to (i, j) must itself be a most
profitable way to (i 1, j 0 ). The usual cut-and-paste argument applies. Suppose that in our most profitable
way to (i, j), which goes through (i 1, j 0 ), we earn a profit of d dollars to get to (i 1, j 0 ), and then earn
p((i 1, j 0 ), (i, j)) dollars getting from (i 1, j 0 ) to (i, j); thus we earn d + p((i 1, j 0 ), (i, j)) dollars getting
to (i, j). Now suppose that there is a way to (i 1, j 0 ) that earns d0 dollars, where d0 > d. Then we would use
that to get to (i 1, j 0 ) on our way to (i, j), earning d0 + p((i 1, j 0 ), (i, j)) > d + p((i 1, j 0 ), (i, j)) dollars,
and thus contradicting the optimality of our way to (i, j).
We also have overlapping subproblems. We need the most profitable way to (i, j) to find the most profitable
way to (i + 1, j 1), to (i + 1, j), and to (i + 1, j + 1). So we will need to directly refer to the most profitable
way to (i, j) up to three times, and if we were to implement this algorithm recursively, we would be solving
each subproblem many times.
Let d[i, j] be the profit we earn in the most profitable way to (i, j). Then we have that d[1, j] = 0 for all
j = 1, 2, . . . , n. For i = 2, 3, . . . , n we have

d[i 1, j 1] + p((i 1, j 1), (i, j)), if j > 1,
d[i, j] = d[i 1, j] + p((i 1, j), (i, j)), always,
d[i 1, j + 1] + p((i 1, j + 1), (i, j)), if j < n.

To keep track of how we got to (i, j) most profitably, we let w[i, j] be the value of j used to achieve the maximum
value of d[i, j]. These values are defined for 2 i n and 1 j n.
Thus we can run the following procedure:

1
Checkerboard(n, p)

for i = 1 to n do
set d[1, j] := 0
endfor
for i = 2to n do
forj = 1 to n do
set d[i, j] :=
if j > 1 then do
set d[i, j] := d[i 1, j 1] + p((i 1, j 1), (i, j))
set w[i, j] := j 1
endif
if d[i 1, j] + p((i 1, j), (i, j)) > d[i, j] then do
set d[i, j] := d[i 1, j] + p((i 1, j), (i, j))
set w[i, j] := j
endif
if j < n and d[i 1, j + 1] + p((i 1, j + 1), (i, j)) > d[i, j] then do
set d[i, j] := d[i 1, j + 1] + p((i 1, j + 1), (i, j))
set w[i, j] := j + 1
endif
endfor
endfor
return d and w

Once we fill in the d[i, j] table, the profit earned by the most profitable way to any square along the top row is
max1jn {d[n, j]}.
To actually compute the set of moves, we use the usual recursive backtracking method. This procedure prints
the squares visited, from row 1 to row n:

Print-Moves(w, i, j)

if i > 1 then
Print-Moves(w, i 1, w[i, j])
print (i,j)

Letting t = max1jn {d[n, j]}, the initial call is Print-Moves(w, n, t).


The time to run Checkerboard is clearly (n2 ). Once we have computed the d and w tables, Print-Moves runs
in (n) time, which we can see byobserving that i = n in the initial call and i decreases by 1 in each recursive
call.
2. Decide whether the following statement is true or false. If it is true, give a short explanation. If it is false,
give a counterexample.
Let G be an arbitrary flow network, with a source s, a sink t, and a positive integer capacity ce
on every edge e. If f is a maximum flow in G, then f saturates every edge out of s with flow
(i.e. for all edges e out of s, we have f (e) = ce ).
This is false. Consider a graph with nodes s, v, w, t, edges (s, v), (v, w), (w, t), capacities 2 on (s, v) and (w, t),
and a capacity of 1 on (v, w). Then the maximum flow has value 1, and does not saturate the edge out of s.
3. Suppose you are given a directed graph G = (V, E), with positive integer capacity ce on each edge e, a
source s V , and a sink t V . You are also given a maximum flow in G, defined by a flow value fe on

2
each edge e. The flow f is acyclic: There is no cycle in G on which all arcs carry positive flow. The flow f
is also integer valued.
Now suppose we pick a specific arc e E and reduce its capacity by 1 unit. Show how to find a maximum
flow in the resulting capacitated graph in time O(m + n), where m is the number of edges in G and n is
the number of nodes.
We will assume that the flow f is integer-valued. Let e = (v, w). If the edge e is not saturated with flow, then
reducing its capacity by one unit does not cause a problem. So assume it is saturated.
We first reduce the flow on e to satisfy the capacity conditions. We now have to restore the capacity condition.
We construct a path from w to t such that all edges carry flow, and we reduce the flow by one unit on each of
these edges. We then do the same thing from v back to s. Note that because the flow f is acyclic, we do not
encounter any edge twice in this process, so all edges we traverse have their flow reduced by exactly one unit,
and the capacity condition is restored.
Let f 0 be the current flow. We have to decide whether f 0 is a maximum flow, or whether the flow value can be
increased. Since f was a maximum flow, and the value of f 0 is only one unit less than f , we attempt to find a
single augmenting path from s to t in the residual graph Gf 0 . If we fail to find one, then f 0 is maximal. Else, the
flow is augmented to have value at least that of f ; since the current flow network cannot have a larger maximum
flow value than the original one, this is a maximal flow.
4. Let G = (V, E) be a directed graph, with source s V , sink t V , and nonnegative arc capacities {ce }.
Give a polynomial time algorithm to decide whether G has a unique minimum cut (i.e. a cut of capacity
strictly less than that of all other cuts).
We first classify all nodes into three types:
upstream, that is, nodes v such that for all minimum cuts (A, B), we have v A;
downstream, that is, nodes v such that for all minimum cuts (A, B), we have v B;
central, that is, nodes v that neither upstream nor downstream, there is a minimum cut (A, B) such that
v A and a minimum cut (A0 , B 0 ) such that v B 0 .
Clearly, the minimum cut is unique if and only if there are no central nodes.
To check if there are central nodes, consider the cut (A , B ) found by performing breadth-first search on the
residual graph Gf at the end of a maximum flow algorithm. We claim that a node v is upstream if and only
if v A . Clearly, if v is upstream then it must belong to A ; since otherwise, it lies on the sink-side of the
minimum cut (A , B ). Conversely, suppose v A were not upstream. Then there would be a minimum cut
A0 , B 0 with v B 0 . Now since v A there is a path P in Gf from s to v. Since v B 0 , this path must have
an edge (u, w) with u A0 and w B 0 . But this is a contradiction, because no edge in the residual graph can
go from the source side to the sink side of any minimum cut.
A completely symmetric argument shows the following. Let B denote the nodes that can reach t in Gf , and let
A = V B . Then (A , B ) is a minimum cut, and a node w is downstream if and only if w B .
Thus our algorithm is to compute a maximum flow f , build Gf , and use breadth-first search to find the sets A
and B . These are the upstream and downstream nodes, respectively; the remaining nodes are central.

You might also like