Sie sind auf Seite 1von 5

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 ).

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
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.

1
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.

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 1, 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 1: An instance of the zero-skew problem, described in Exercise 23.

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.

2
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.

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.

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 ,

3
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.

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.

4
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.

Das könnte Ihnen auch gefallen