You are on page 1of 15

Algorithms: CSE 202 — Homework II

Solve problems 2, 3, 4, 6, and 7.


Problem 1: The tramp steamer problem (DPV)
You are the owner of a steamship that can ply between a group of port cities V . You make money
at each port: a visit to city i earns you a profit of pi dollars. Meanwhile, the transportation cost
from port i to port j is cij > 1. You want to find a cyclic route in which the ratio of profit to cost
is maximized.
To this end, consider a directed graph G = (V, E) whose nodes are ports, and which has edges
between each pair of ports. For any cycle C in this graph, the profit-to-cost ratio is
P
(i,j)∈C pj
r(C) = P
(i,j)∈C cij

Let r∗ be the maximum ratio achievable by a simple cycle. One way to determine r∗ is by binary
search: by first guessing some ratio r, and then testing whether it is too large or too small.
Consider any positive r > 0. Give each edge (i, j) a weight of wij = rcij − pj .
(a) Show that if there is a cycle of negative weight, then r < r∗ .

(b) Show that if all cycles in the graph have strictly positive weight, then r > r∗ .

(c) Give an efficient algorithm that takes as input a desired accuracy  > 0 and returns a simple
cycle C for which r(C) ≥ r∗ − . Justify the correctness of your algorithm and analyze its
running time in terms of |V |, , and R = max(i,j)∈E (pj /cij ).

Solution.
(a) For a given r, let Gr be the graph with edge weights wij = rcij − pj . Assume there is a negative
weight cycle in Gr . Let Cneg be a simple cycle of negative weight. The weight of Cneg is:
X
weight(Cneg ) = wij
(i,j)∈Cneg
X
= (rcij − pj ) < 0
(i,j)∈Cneg

Thus,
P
(i,j)∈Cneg pj
r<P = r(Cneg )( by definition)
(i,j)∈Cneg cij

Since r∗ is the maximum ratio achievable for any simple cycle, r < r(Cneg ) ≤ r∗ . Therefore
r < r∗ .

1
(b) Let Cmax be the simple cycle in G with the maximum profit-to-cost ratio, that is, r∗ = r(Cmax ).
Assume that there are no negative weight cycles in Gr . The weight of Cmax in the graph Gr is:
X
weight(Cmax ) = wij
(i,j)∈Cmax
X
= (rcij − pj )
(i,j)∈Cmax

Since all cycles in the graph have a weight strictly greater than zero,

weight(Cmax ) > 0

Thus,
P
(i,j)∈Cmax pj
r>P = r(Cmax )(by definition)
(i,j)∈Cmax cij

Therefore r > r(Cmax ) = r∗ .

(c) Algorithm description: In this problem, our goal is to find a cycle with a ratio r which falls
in the interval [r∗ − , r∗ ]. We can achieve this by using binary search and the conclusions
in (a) and (b). Firstly, it’s easy to prove that max(i,j)∈E (pj /cij ) is an upperbound and 0 is
a lowerbound for r∗ . We select an r which is a mid-point between these two values and
test for the existence of negative weight cycles in Gr . If there is a negative cycle in Gr
(which implies that r < r∗), let r be the new lowerbound. Otherwise, let r be the new
upperbound. The binary search procedure terminates when the length of the interval(the
distance between lowerbound and upperbound) is less than .

Algorithm 1: Maximum Ratio


Require: Graph G = {V, E}
Ensure: Cycle C
1: Initialize low ← 0, high ← max(i,j)∈E (pj /cij )
2: while (high − low) >  do
3: r ← (high + low)/2
4: wij = rcij − pj for all (i, j) ∈ E
5: if G has negative cycle then
6: low ← r
7: else
8: high ← r
9: end if
10: end while
11: Use low to update the weights in G, run Bellman-Ford on G to find the negative cycle C
12: return C

We use Bellman-Ford to check if there exists a negative cycle. In order to get the path of
the cycle, we need to keep track of the predecessor of each node in the Relax step.
Correctness proof:
(a) The algorithm will terminate

2
Algorithm 2: Bellman-Ford
Require: Graph G = {V, E}, w, s
Ensure: Bool b
1: Initialize d[v] ← +∞, predecessor[v] ← v for all v ∈ V , d[s] ← 0
2: for i = 1 → |V | − 1 do
3: for each edge (u, v) ∈ E do
4: if (d[v] > d[u] + w(u, v)) then
5: d[v] = d[u] + w(u, v), predecessor[v] = u
6: end if
7: end for
8: end for
9: for each edge (u, v) ∈ E do
10: if (d[v] > d[u] + w(u, v)) then
11: predecessor[v] = u
12: return false
13: end if
14: end for
15: return true

Proof. (high − low) will be halved during each iteration. We terminte when the
differene is less than .
(b) We can get the path of the negative weight cycle using the predecessor information
Proof. At the end of Bellman-Ford algorithm, the algorithm will return false when the
shortest path of a node v changes. Actually, this is the |V |th interation which means
the shortest path from source to v consists of |V | edges. This indicates that there must
be a negative weight cycle in this path. According to our algorithm, the predecessor of
a node indicates its predecessor on the shortest path. Therefore, starting from v and
by chasing the predecessor pointers, we can find the cycle (if a node appears twice on
the predecessor chain, then all the nodes between them are in the cycle).
(c) The profit-to-cost ratio of the output cycle is greater than r∗ − 
Proof. According to our algorithm, the upperbound ru is always greater or equal to
r∗ , that is, ru ≥ r∗ . The algorithm will terminate only when the difference between
the upperbound ru and the lowerbound rl is less than or equal to , that is, ru − rl ≤ .
Thus, r∗ ≤ ru ≤ (rl + ), that is, rl ≥ r∗ − . At the end of the algorithm, we run
Bellman-Ford on Grl to get a negative weight cycle C. According to the analysis in
(a), we know that r(C) > rl . Therefore, r(C) > r∗ − .
Time complexity: The initial search range is R, in each iteration this range is halved.
Suppose that after x iterations, the algorithm will terminate. Then the range will be 2Rx .
By solving 2Rx = , we can easily get that x = log R . In each iteration, we use Bellman-Ford
to check the existence of negative cycle which takes O(|V 3 |). Therefore the total time
complexity is O(|V 3 | log R )

Problem 2: Changing road conditions (KT 4.18)


Your friends are planning an expedition to a small town deep in the Canadian north next winter

3
break. They’ve researched all the travel options and have drawn up a directed graph whose nodes
represent intermediate destinations and edges represent the roads between them.
In the course of this, they’ve also learned that extreme weather causes roads in this part of
the world to become quite slow in the winter and may cause large travel delays. They’ve found
an excellent travel Web site that can accurately predict how fast they’ll be able to travel along
the roads; however, the speed of travel depends on the time of year. More precisely, the Web site
answers queries of the following form: given an edge e = (v, w) connecting two sites v and w, and
given a proposed starting time t from location v, the site will return a value fe (t), the predicted
arrival time at w. The Web site guarantees that fe (t) ≥ t for all edges e and all times t (you can’t
travel backward in time), and that fe (t) is a monotone increasing function of t (that is, you do not
arrive earlier by starting later). Other than that, the functions fe (t) may be arbitrary. For example,
in areas where the travel time does not vary with the season, we would have fe (t) = t + `e , where `e
is the time needed to travel from the beginning to the end of edge e.
Your friends want to use the Web site to determine the fastest way to travel through the
directed graph from their starting point to their intended destination. (You should assume that
they start at time 0, and that all predictions made by the Web site are completely correct.) Give a
polynomial-time algorithm to do this, where we treat a single query to the Web site (based on a
specific edge e and a time t) as taking a single computational step.

Solution.
Algorithm description: We leverage the idea from Dijkstra Algorithm to solve this problem. The
algorithm maintains a set S of vertices u for which we have found a path with the shortest
arrival time t(u) from the starting point s. Initially S = {s}, and t(s) = 0, t(v) = ∞ for all
v 6= s. Next, the algorithm repeatedly selects the node u ∈ V − S with the earliest arrival
time t(u), adds u to S and then relaxes all edges e leaving u. Relaxation updates t(v) by
t(v) = min(t(v), f (t(u))). The algorithm terminates when S − V = ∅. Algorithm 3 shows the
detail.
To speed up the algorithm, we maintain a min-priority queue Q of nodes v ∈ V − S with its
t(v) as key.

Algorithm 3: Path
1: t(s) = 0, t(v) = ∞ for v 6= s.
2: S = ∅.
3: Q = V .
4: while Q 6= ∅ do
5: Extract u such that t(u) is the minimum from Q.
6: S = S ∪ {u}.
7: for each node v such that e(u, v) ∈ E do
8: if fe(u,v) (t(u)) > t(v) then
9: t(v) = fe(u,v) (t(u))
10: end if
11: end for
12: end while

Correctness proof: Firstly, let’s define S-path as a path from s to a vertex v such that all the
intermediate vertices are in S

4
The following two invariants hold after each iteration:

a) For each vertex u in S, the time t(u) is the earlist time we can arrive at u.
b) For each vertex u not in S, the time t(u) is the earliest time we can arrive at u when
restricted to S-paths.

If the first invariant holds when the algorithm terminates, the algorithm works correctly
because all the vertices will be in the known region S.

Proof. We prove it by the induction on |S|.


For base case, |S| = 1. S = {s}. S contains only s and we have indeed t(s) = 0 which is the
earliest arrival time we can achieve for node s, so the invariant a) holds.
All other vertices u outside S have either t(u) = f(s,u) (0) if they are neighbours of s or
t(u) = ∞ otherwise, so the invariant b) holds as well.
Suppose the claim holds for |S| = k, k ≥ 1, we need to show that for |S| = k + 1 by adding a
new node v into S, Pv is the path with the earliest arrival time.

Figure 1: Proof of correctness.

According to the algorithm, Pv is the path with earlist arrival time when restricted to S-path.
So the only way to fail invariant a) is that there exists another path P which is not a S-path.
We will show that the arrival time of P will not be earlier than Pv . The path P leaves S for
the first time at the vertex y 6= v, therefore can be written as P = P1 P2 with P1 the subpath
from s to y and P2 the subpath from y to v. Figure 1 shows this situation. We use T (P ) to
denote the arrival time of v following path P , then we have:

T (P ) ≥ T (P1 )(f(t) is monotone increasing and f(t) > t)


≥ t(y)(invariant b))
≥ t(v)(we choose the node with earliest arrival time each iteration)
= T (Pv )
(1)

Therefore, as soon as the path P leaves region S, the arrival time is already later than the
S-path discovered to v. Therefore the invariant a) holds.
Let’s show that invariant b) holds at the end of this iteration. the addition of v to S creates
new S-path. We have to check that we update the distance of the vetices that can be reached
by these new S-path.

5
If v is the last vertex of the S-path inside S, before leaving S, then the distance of the last
vertex is updated since it is the neighbour of v.
If v is not the last vertex, but it is a vertex x ∈ S, then there are some shorter S-paths to x
than the one going through v, so no distance needs to be updated for taht case.
Therefore the invariant b) holds as well.

Time analysis: The time complexity is exactly the same as the Dijkstra Algorithm. The
complexity is |E| log |V | if implemented by a heap based min-priority queue.

Problem 3: Timing circuits (KT 4.24)


Timing circuits are a crucial component of VLSI chips. Here’s a simple model of such a timing
circuit. Consider a complete balanced binary tree with n leaves, where n is a power of two. Each
edge e of the tree has an associated length `e , which is a positive number. The distance from the
root to a given leaf is the sum of the lengths of all the edges on the path from the root to the leaf.
The root generates a clock signal which is propagated along the edges to the leaves. We’ll assume
that the time it takes for the signal to reach a given leaf is proportional to the distance from the
root to the leaf.
Now, if all leaves do not have the same distance from the root, then the signal will not reach the
leaves at the same time, and this is a big problem. We want the leaves to be completely synchronized,
and all to receive the signal at the same time. To make this happen, we will have to increase the
lengths of certain edges, so that all root-to-leaf paths have the same length (we’re not able to shrink
edge lengths). If we achieve this, then the tree (with its new edge lengths) will be said to have zero
skew. Our goal is to achieve zero skew in a way that keeps the sum of all the edge lengths as small
as possible.
Give an algorithm that increases the lengths of certain edges so that the resulting tree has zero
skew and the total edge length is as small as possible.
Example. Consider the tree in Figure 2, in which letters name the nodes and numbers indicate
the edge lengths.
The unique optimal solution for this instance would be to take the three length-1 edges and
increase each of their lengths to 2. The resulting tree has zero skew, and the total edge length is 12,
the smallest possible.

Figure 2: An instance of the zero-skew problem, described in Exercise 23.

Solution.

6
Definitions
In the the following, the word tree stands for a complete balanced binary tree. We assume that trees
are rooted. For a non-leaf node u of T , we define the left edge of u to be the edge that connects u
to its left child, which will be denoted by eL (u). Similarly, we define the right edge of u and denote
it by eR (u). We use TL and TR to denote the left and right subtrees of T , respectively.
A function that assigns a real number to each edge of a tree T is called a length function of T .
We use the notation lT to denote a length function and lT (e) to denote the length of an edge e in T .
If T 0 is subtree of T , lT 0 denotes the restriction of the length function lT to TP 0.

Let w(lT ) denote the sum of the edge lengths assigned by lT , that is, w(lT ) = e is an edge of T lT (e).
Let M (lT ) denote the maximum root-to-leaf path length of lT , that is, the maximum root-to-leaf
path length in T where the edge lengths are given by the length function lT . Simularly, let m(lT )
denote the minimum root-to-leaf path length of lT . The skew of a length function lT is defined as
M (lT ) − m(lT ).
For a zero-skew length function lT , we define its delay d(lT ) to be the length of a root-to-leaf
path. The definition is unambiguous since all root-to-leaf paths of a zero-skew length function have
the same length.
For two length functions lT and lT0 of a tree T , we say that lT0 dominates lT if, if lT0 (e) ≥ lT (e)
for all edges e in T .
Given a tree T and a length function lT , our goal is to find a zero-skew length function lT0 that
dominates lT such that among all such length functions lT0 has the minimum sum of edge lengths.
We call such length functions optimal length functions for lT .
It turns out that the notion of minimal domination will prove to be quite useful. For two length
functions lT and lT0 of T , we say that lT0 minimally dominates lT if lT0 dominates lT and for all nodes
in T either lT0 (eL (u)) = lT (eL (u)) or lT0 (eR (u)) = lT (eR (u)). In other words, lT0 increases the length
of at most one of the edges (left or right edge) of a non-leaf node.

Properties of Dominating and Zero-Skew Length Functions


We now establish some properties of dominating length functions and zero-skew length functions.
Claim 0.1. Let lT be a length function of a tree T . If lT has zero skew, then for every subtree T 0
of T lT 0 has zero skew.
Claim 0.2. Let lT and lT0 be length functions of a tree T . If lT0 dominates lT , then for any subtree
T 0 , lT0 0 dominates lT 0 . Analogous property holds for minimal domination.
Claim 0.3. Let T be a tree and lT a length function for T . If lT0 is an optimal length function, then
lT0 minimally dominates lT .
Claim 0.4. Let T be a tree and lT a length function for T . If a length function lT0 has zero skew
and minimally dominates lT , then d(lT0 ) = M (lT ).
Claim 0.5. Let T be a tree and lT a length function for T . If a length function lT0 minimally
dominates lT and has zero skew, then lT0 is optimal for lT .
Proof: We will prove the claim by induction on the height of the tree. For a tree of height 0, the
theorem is trivially true.
Let k ≥ 1 be an arbitrary integer. Assume that the theorem is true for any tree of height less
than k and any length function for it.
Consider a tree T of height k. Let u be its root and TL and TR respectively be its left and right
subtrees. Let lT be any length function of T . Let lT0 be a zero-skew length function that minimally

7
dominate lT . Our goal is to prove that lT0 is optimal for lT . For the sake of contradiction, assume
that lT0 is not optimal. In other words, there is a length function lT00 which is optimal for lT and that
w(lT00 ) < w(lT0 ).
By Claim 0.3, we know that lT00 minimally dominates lT since it is optimal. It is given that
lT0 minimally dominates lT . We then get from Claim 0.4 that d(lT0 ) = d(lT00 ) = M (lT ). Since lT0 0
and lT00 0 also minimally dominate lT 0 for any subtree T 0 of T , we get d(lT0 L ) = d(lT00L ) = M (TL ) and
d(lT0 R ) = d(lT00R ) = M (TR ).
By Claim 0.4, we get d(lT0 ) = lT0 (eL (u)) + d(lT0 L ) = lT0 (eR (u)) + d(lT0 R ) and d(lT00 ) = lT00 (eL (u)) +
d(lTL ) = lT00 (eR (u)) + d(lT00R ) since lT0 and lT00 have zero skew.
00

Putting all these equalities together, we get 2d(lT0 ) = 2M (lT ) = lT0 (eL (u)) + M (lTL ) + lT0 (eR (u)) +
M (lTR ) and 2d(lT00 ) = 2MlT = lT00 (eL (u)) + M (lTL ) + lT00 (eR (u)) + M (lTR ). This implies lT0 (eL (u)) +
lT0 (eR (u)) = lT00 (eL (u)) + lT00 (eR (u)).
On the other hand, w(lT00 ) = lT00 (eL (u)) + w(lT00L ) + lT00 (eR (u)) + w(lT00R ) < w(lT0 ) = lT0 (eL (u)) +
w(lTL ) + lT0 (eR (u)) + w(lT0 R ).
0

By inductive hypothesis, we get that lT0 L is optimal for lTL . This implies w(lT0 L ) ≤ w(lT00L ).
Similarly we have w(lT0 L ) ≤ w(lT00L ). These inequalities imply that lT00 (eL (u))+lT00 (eR (u)) < lT0 (eL (u))+
lT0 (eR (u)), contradicting the previously established equality of these quantities.

Algorithm G
We will design an algorithm (called Algorithm G) which takes a tree T and a length function lT as
inputs and outputs a zero-skew length function lT0 which minimally dominates lT . lT0 is defined in a
bottom-up fashion starting with the subtrees of height 1. In the following, we provide a detailed
description of G.
Let u be the root of T . Let TL and TR respectively be its left and right subtrees. We apply
the algorithm recursively to TL (TR ) with lT restricted to TL (TR ) to obtain a zero-skew minimally
dominating length function lT0 L (lT0 R ) for lTL (lTR ). For each node v in TL , we set lT0 (v) = lT0 L (v).
Similarly for each node v in TR , we set lT0 (v) = lT0 R (v). We compare lT (eL (u)) + d(lT0 L ) and
lT (eR (u)) + d(lT0 R ). If the quantities are equal, we set lT0 (eL (u)) = lT (eL (u)) and lT0 (eR (u)) =
lT (eR (u)). If lT (eL (u)) + d(lT0 L ) < lT (eR (u)) + d(lT0 R ) we set lT0 (eL (u)) = lT (eR (u)) + d(lT0 R ) − d(lT0 L )
and we set lT0 (eR (u)) = lT (eR (u)). The remaining case is handled similarly.
If the input tree T has height 0, the algorithm will output lT without any change.

Correctnes of Algorithm G
We will prove that the function lT0 output by the algorithm has zero skew and that it minimally
dominates lT . It then follows from Claim 0.5 that lT0 has the minimum sum of edge lengths among
all zero-skew functions that dominate lT .
Theorem 0.6. The length function lT0 output by Algorithm G has zero skew and it minimally
dominates lT .
Proof: The theorem is proved by a routine proof by induction. The reader is asked to fill in the
details.

Time Complexity
The time complexity of the Algorithm G is linear in the number of nodes of T . To see this, observe
that it only takes a constant amount of time to extend the definition of lT0 to the left and right

8
edges of a node after it is defined for the left and right subtrees of the node. Hence the total time
required is linear in the number of the nodes in the tree.

Problem 4: Time-varying Minimum Spanning Tree (KT 4.26)


One of the first things you learn in calculus is how to minimize a differentiable function like
y = ax2 + bx + c, where a > 0. The Minimum Spanning Tree Problem, on the other hand, is a
minimization problem of a very different flavor: there are now just a finite number of possibilities
for how the minimum might be achieved–rather than a continuum of possibilities–and we are
interested in how to perform the computation without having to exhaust this (huge) finite number
of possibilities.
One can ask what happens when these two minimization issues are brought together, and the
following question is an example of this. Suppose we have a connected graph G = (V, E). Each
edge e now has a time-varying edge cost given by a function fe : R → R. Thus, at time t, it has cost
fe (t). We’ll assume that all these functions are positive over their entire range. Observe that the set
of edges constituting the minimum spanning tree of G may change over time. Also, of course, the
cost of the minimum spanning tree of G becomes a function of the time t; we’ll denote this function
cG (t). A natural problem then becomes: Find a value of t at which cG (t) is minimized.
Suppose each function fe is a polynomial of degree 2: fe (t) = ae t2 + be t + ce ; where ae > 0. Give
an algorithm that takes the graph G and the values {(ae , be , ce ) : e ∈ E} and returns a value of the
time t at which the minimum spanning tree has minimum cost. Your algorithm should run in time
polynomial in the number of nodes and edges of the graph G. You may assume that arithmetic
operations on the numbers {(ae , be , ce )} can be done in constant time per operation.

Solution.

• Connected graph G = (V, E).

• Each edge e ∈ E has a time-varying edge cost fe = ae t2 + be t + ce such that fe > 0 for all t.

• Denote n = |V | and m = |E|.

Given the graph G and the values (ae , be , ce ) : e ∈ E, we want to find the time t at which the
minimum spanning tree has minimum cost.
We first give intuition into the problem. By reviewing the Kruskal’s algorithm to construct a
minimum spanning tree of the graph G, we note that the algorithm output only depends on the
sorted order of edge cost values in E. So even though the edge cost values change with time, the
minimum spanning tree will not change if the sorted order of edge costs does not change. Hence,
our key idea is to find the time instants at which the relative order of edge cost values may change.
For that purpose, we rely on the parabola shape of the quadratic edge cost functions. In particular,
we note that two distinct parabolas have at most two intersections, and the relative order of the
two parabolas can only change at those crossing points.
We describe the details of our algorithm as follows.

1. Since edge costs vary with time, the set of edges constituting the minimum spanning tree also
changes with time. Nevertheless, we can find a finite number of such minimum spanning trees
as follows.

9
2. Consider the Kruskal’s algorithm to construct minimum spanning tree. We sort the edges in
E by costs, and successively iterate through E in order of increasing cost. Then an edge e is
inserted into the minimum spanning tree only if it does not create a cycle; otherwise it is just
discarded.
We observe that if the sorted order of E does not change, then the Kruskal’s algorithm will
not change the set of edges constituting the minimum spanning tree. This fact holds true even
though the edge costs vary with time.

3. Therefore, the next step is to investigate how to enumerate all possible sorted orders of E.
Consider two edges e1 and e2 in E. If ae1 = ae2 , be1 = be2 , ce1 = ce2 , then fe1 ≡ fe2 for all
time t. Otherwise, by simple geometric characteristics of parabolas, fe1 and fe2 vary over
time as two separate parabolas and have at most two intersections. In particular, we have the
following three cases.

• fe1 and fe2 have no intersection: either fe1 > fe2 or fe2 > fe1 for all time t.
• fe1 and fe2 have one intersection t0 : the relative order of fe1 and fe2 does not change on
the two time intervals (−∞, t0 ] and [t0 , ∞).
• fe1 and fe2 have two intersections t1 , t2 : the relative order of fe1 and fe2 does not change
on the three time intervals (−∞, t1 ], [t1 , t2 ], and [t2 , ∞).

In other words, the intersections of fe1 and fe2 determine the time instants when the edge
cost order of e1 and e2 can be swapped.
 m(m−1)
4. With m = |E| edges, we have m 2 = 2 edge pairs.
We enumerate all edge pairs, ignore any pair consisting of two edges with the same cost
function, and find all intersections of the remaining pairs. Since each pair with distinct cost
functions has at most two intersections, we have at most 2 · m(m−1)
2 = m(m − 1) intersection
instants. We sort all such intersection instants, denoted as t1 < t2 < ... < tm(m−1) .

5. Now we observe that the sorted order of edge costs does not change over time on each interval
t ∈ [ti , ti+1 ], 0 ≤ i ≤ m(m − 1) , where t0 = −∞ and tm(m−1)+1 = ∞.
So we apply Kruskal’s algorithm on each time interval [ti , ti+1 ] to get a minimum spanning
tree Ti with cost function cTi (t), t ∈ [ti , ti+1 ]. Being the sum cost of edges on the minimum
spanning tree Ti , cTi (t) is also a quadratic function. Therefore, by simple calculus, we can
find the minimum value of cTi (t) on t ∈ [ti , ti+1 ], say c∗i at t∗i .

6. Finally, by comparing all values c∗i , 0 ≤ i ≤ m(m − 1), we get the value of time at which the
minimum spanning tree has minimum cost.

7. Correctness proof: by the geometric fact of parabola intersection, the sorted order of edge
costs does not change on each time interval [ti , ti+1 ]. Then by the correctness of the Kruskal’s
algorithm, the set of edges constituting the minimum spanning tree also does not change on
that time interval. So by enumerating and comparing the minimum spanning trees over time,
we can derive the one with minimum cost.

8. Time complexity:

• Enumerating all edge pairs and sorting intersection instants: O(m2 log m) = O(m2 log n),
since m = O(n2 ).

10
• Kruskal’s algorithm on each time interval: O(m log n).
With m(m − 1) + 1 time intervals, we get O(m3 log n).
• Therefore, total time complexity is O(m3 log n).

Problem 5: Untangling signal superposition (KT 6.19)


You’re consulting for a group of people (who would prefer not to be mentioned here by name)
whose jobs consist of monitoring and analyzing electronic signals coming from ships in coastal
Atlantic waters. They want a fast algorithm for a basic primitive that arises frequently: “untangling”
a superposition of two known signals. Specifically, they’re picturing a situation in which each of two
ships is emitting a short sequence of 0s and 1s over and over, and they want to make sure that the
signal they’re hearing is simply an interleaving of these two emissions, with nothing extra added in.
This describes the whole problem; we can make it a little more explicit as follows. Given a string
x consisting of 0s and 1s, we write xk to denote k copies of x concatenated together. We say that
a string x0 is a repetition of x if it is a prefix of xk for some number k. So x0 = 10110110110 is a
repetition of x = 101.
We say that a string s is an interleaving of x and y if its symbols can be partitioned into two (not
necessarily contiguous) subsequences s0 and s00 , so that s0 is a repetition of x and s00 is a repetition of
y. (So each symbol in s must belong to exactly one of s0 or s00 .) For example, if x = 101 and y = 00,
then s = 100010101 is an interleaving of x and y, since characters 1, 2, 5, 7, 8, 9 form 101101–a
repetition of x–and the remaining characters 3, 4, 6 form 000–a repetition of y.
In terms of our application, x and y are the repeating sequences from the two ships, and s is the
signal we’re listening to: We want to make sure s “unravels” into simple repetitions of x and y. Give
an efficient algorithm that takes strings s, x, and y and decides if s is an interleaving of x and y.

Solution.

0.1 Solution 1 (sketch)


Let x0 and y 0 be the repetitions of x and y up to n digits respectively.
We observe that s[1 . . . k] is an interleaving of x0 [1 . . . i] and y 0 [1 . . . j] where k = i + j, denoted
s[1 . . . k] = x0 [1 . . . i] ⊗ y 0 [1 . . . j], iff either of the following two cases is true:

• s[k] = x0 [i] and s[1 . . . k − 1] = x0 [1 . . . i − 1] ⊗ y 0 [1 . . . j]

• s[k] = y 0 [j] and s[1 . . . k − 1] = x0 [1 . . . i] ⊗ y 0 [1 . . . j − 1]

Let T be a Boolean table so that T (i, j) = true iff s[1 . . . i + j] = x0 [1 . . . i] ⊗ y 0 [1 . . . j]. Then we
have the following recurrence equation for 1 ≤ i, j ≤ n:

T (i, j) := (s[i + j] = x0 [i]) ∧ T (i − 1, j) ∨ (s[i + j] = y 0 [i]) ∧ T (i, j − 1)


 

In the base cases, T (i, 0) = true iff s[1 . . . i] ≡ x0 [1 . . . i]; and T (0, j) = true iff s[1 . . . j] ≡
y 0 [1 . . . j]. W
Finally, we decide that s is an interleaving of x and y if 0≤i≤n T (i, n − i) = true. It takes
O(n2 ) time complexity to fill in the table T .

11
0.2 Solution 2 (sketch)
Let us represent each natural number n as the set of its predecessors {0, . . . , n − 1}. Let x =
x0 · · · xm−1 , y = y0 · · · yn−1 , s = s0 · · · sl−1 . We construct an NFA A that recognizes the language of
all interleavings of x and y as follows: A has state set m × n, begins in state (0, 0), on input a and
in state (i, j) nondeterministically transitions either to (i + 1 mod m, j) if xi = a, or to (i, j + 1
mod n) if yj = a, or rejects if neither xi = a nor yj = a. All states are accepting.
We can simulate A running on s in time O(lmn) by keeping a marker on each state A could be
in at time i and updating the markers l times. Initially there is just a marker on (0, 0). To update
the markers once takes time O(mn).

Problem 6: Shortest wireless path sequence (KT 6.14)


A large collection of mobile wireless devices can naturally form a network in which the devices are
the nodes, and two devices x and y are connected by an edge if they are able to directly communicate
with each other (e.g., by a short-range radio link). Such a network of wireless devices is a highly
dynamic object, in which edges can appear and disappear over time as the devices move around.
For instance, an edge (x, y) might disappear as x and y move far apart from each other and lose the
ability to communicate directly.
In a network that changes over time, it is natural to look for efficient ways of maintaining a
path between certain designated nodes. There are two opposing concerns in maintaining such a
path: we want paths that are short, but we also do not want to have to change the path frequently
as the network structure changes. (That is, we’d like a single path to continue working, if possible,
even as the network gains and loses edges.) Here is a way we might model this problem.
Suppose we have a set of mobile nodes V , and at a particular point in time there is a set E0 of
edges among these nodes. As the nodes move, the set of edges changes from E0 to E1 , then to E2 ,
then to E3 , and so on, to an edge set Eb . For i = 0, 1, 2, . . . , b, let Gi denote the graph (V, Ei ). So
if we were to watch the structure of the network on the nodes V as a “time lapse”, it would look
precisely like the sequence of graphs G0 , G1 , G2 , . . . , Gb−1 , Gb . We will assume that each of these
graphs Gi is connected.
Now consider two particular nodes s, t ∈ V . For an s-t path P in one of the graphs Gi , we define
the length of P to be simply the number of edges in P , and we denote this `(P ). Our goal is to
produce a sequence of paths P0 , P1 , . . . , Pb so that for each i, Pi is an s-t path in Gi . We want the
paths to be relatively short. We also do not want there to be too many changes–points at which
the identity of the path switches. Formally, we define changes(P0 , P1 , . . . , Pb ) to be the number of
indices i (0 ≤ i ≤ b − 1) for which Pi 6= Pi+1 .
Fix a constant K > 0. We define the cost of the sequence of paths P0 , P1 , . . . , Pb to be
b
X
cost(P0 , P1 , . . . , Pb ) = `(Pi ) + K · changes(P0 , P1 , . . . , Pb ).
i=0

(a) Suppose it is possible to choose a single path P that is an s-t path in each of the graphs
G0 , G1 , . . . , Gb . Give a polynomial-time algorithm to find the shortest such path.

(b) Give a polynomial-time algorithm to find a sequence of paths P0 , P1 , . . . , Pb of minimum cost,


where Pi is an s-t path in Gi for i = 0, 1, . . . , b.

Solution.

12
a. Let Gi = (V, Ei ), i = 0, . . . , b, be a sequence of graphs and let s, t ∈ V . If there is a single
path P from s to t in each of the Gi , then to find a shortest one we can perform breadth first
search (BFS) on G = (V, ∩bi=0 Ei ). Computing G can be done in time O((|V | + |Ei |) and
P
BFS takesPtime O(|E| + |V |) where |E| = ∩bi=0 Ei . Hence the total runtime is bounded by
O((|V | + |Ei |) = O(b · n2 )
b. To find a min cost sequence P0 , . . . , Pb , notice that in any optimal sequence, if i is the last
breakpoint, i.e. the last index where the path changes, then Pb (with no further breakpoints) is
the shortest path for the graph sequence Gi , . . . , Gb ; and P0 , . . . , Pi−1 is the optimal sequence
of paths for G0 , . . . , Gi−1 (with potentially additional breakpoints). We turn this into an
algorithm below.
Let Di,j = length of a shortest path from s to t in (V, ∩jk=i Ek ). We can compute Di,j for each
i, j as follows.
for i ← 0, . . . , b do
E 0 ← Ei // E 0 will store ∩jk=i Ek
Di,i ← BFS distance from s to t in (V, E 0 )
for j ← i + 1, . . . , b do
E 00 ← ∅
for each e ∈ Ej do
if e ∈ E 0 , add e to E 00
end for
E 0 ← E 00
Di,j ← BFS distance from s to t in (V, E 0 )
end for
end for
This takes time O(b2 (|Ei | + |V |)) = O(b2 n3 ).
P

Next, for 0 ≤ i ≤ b, let Ci be the cost of an optimal sequence for G0 , . . . , Gi . Let Bi be the
index position of the latest breakpoint. The cost of the optimal sequence of paths Cj is then
either the cost of the of shortest path with no breakpoints D0,j or the cost of the shortest
sequence of paths up to index Bj , followed by a path with no more breakpoints.
More formally, for j ∈ [0, b],
Cj = min Ci−1 + (j − i + 1)Di,j + K.
i∈[0,j]

Bj = arg min Ci−1 + (j − i + 1)Di,j + K.


i∈[0,j]

In the base case, C0 = D0,0 and B0 = 0. We define C−1 = −K such that Cj = j ∗ D0,j if
Bj = 0, i.e. in the case that the optimal sequence of paths does not contain a breakpoint.
Correctness follows immediately from our assertion that the optimal sequence of paths either
contains no breakpoints, in which case we pick the shortest path in the union of the graphs, or
there must be a last breakpoint Bj . Then the cost of the shortest sequence of paths consists of
the cost of the shortest sequence of paths from indices 0 to Bj−1 , plus the cost of the shortest
path from with no breakpoints from indices Bj to j times the number of indices in that range,
plus the cost of one breakpoint.
Finally, the total runtime consists of computing first all values Di,j in time O(b2 n3 ) and then
computing all values Cj . Since Cj is computed from C0 , . . . , Cj−1 in time O(b), the total time
of the algorithm is dominated by the time to compute all values Di,j , i.e. O(b2 n3 ).

13
Problem 7: Selling shares of stock (KT 6.25)
Consider the problem faced by a stockbroker trying to sell a large number of shares of stock in a
company whose stock price has been steadily falling in value. It is always hard to predict the right
moment to sell stock, but owning a lot of shares in a single company adds an extra complication:
the mere act of selling many shares in a single day will have an adverse effect on the price.
Since future market prices, and the effect of large sales on these prices, are very hard to predict,
brokerage firms use models of the market to help them make such decisions. In this problem, we
will consider the following simple model. Suppose we need to sell x shares of stock in a company,
and suppose that we have an accurate model of the market: it predicts that the stock price will
take the values p1 , p2 , . . . , pn over the next n days. Moreover, there is a function f (·) that predicts
the effect of large sales: if we sell y shares on a single day, it will permanently decrease the price
by f (y) from that day onward. So, if we sell y1 shares on day 1, we obtain a price per share of
p1 − f (y1 ), for a total income of y1 · (p1 − f (y1 )). Having sold y1 shares on day 1, we can then sell
y2 shares on day 2 for a price per share of p2 − f (y1 ) − f (y2 ); this yields an additional income of
y2 · (p2 − f (y1 ) − f (y2 )). This process continues over all n days. (Note, as in our calculation for day
2, that the decreases from earlier days are absorbed into the prices for all later days.)
Design an efficient algorithm that takes the prices p1 , . . . , pn and the function f (·) (written as a
list of values f (1), f (2), . . . , f (x)) and determines the best way to sell x shares by day n. In other
words, find natural numbers y1 , y2 , . . . , yn so that x = y1 + . . . + yn , and selling yi shares on day i
for i = 1, 2, . . . , n maximizes the total income achievable. You should assume that the share value pi
is monotone decreasing, and f (·) is monotone increasing; that is, selling a larger number of shares
causes a larger drop in the price. Your algorithms running time can have a polynomial dependence
on n (the number of days), x (the number of shares), and p1 (the peak price of the stock).
Example Consider the case when n = 3; the prices for the three days are 90, 80, 40; and
f (y) = 1 for y ≤ 40, 000 and f (y) = 20 for y > 40, 000. Assume you start with x = 100, 000 shares.
Selling all of them on day 1 would yield a price of 70 per share, for a total income of 7,000,000.
On the other hand, selling 40,000 shares on day 1 yields a price of 89 per share, and selling the
remaining 60,000 shares on day 2 results in a price of 59 per share, for a total income of 7,100,000.

Solution. Let Tx0 ,i for 0 ≤ x0 ≤ x and 1 ≤ i ≤ n denote the profit of the most efficient strategy to
sell x0 shares starting at day i. The final answer that we are interested in is Tx,1 .
The base case is Tx0 ,n = pn (x0 ) − x0 · f (x0 ) for all x0 , i.e. on the last day we have to sell all our
remaining stocks with the full discount.
For the inductive case, we use the fact that if we sell yi stocks on day i, the price of every
stock we sell now or afterwards is decreased by f (yi ), independent on the exact strategy we use.
Furthermore, on day i we need to decide how many stocks we sell. Hence we get for all i ≤ n − 1
and for all x0
Tx0 ,i = max pi (y) + Tx0 −y,i+1 − x0 · f (y)
y∈[0,x0 ]

The equation has three terms: The profit we make on day i, the profit we would make in the
remaining days if there were no discounts, minus the discounts for all the unsold stocks.
We can compute all values Tx0 ,i in decreasing order of i in a standard dynamic programming
fashion.
Correctness follows from the property that the optimal strategy for the any remaining amount
of stocks and days does not depend on the discounts already given, as the discounts are applied to
all sales regardless. The rest is then an immediate consequence of considering all possible values for
yi and taking the maximum.

14
Since there are x · n many values Tx0 ,i and there are at most x values for y each, the total runtime
is bounded by O(x2 n).

15

You might also like