Вы находитесь на странице: 1из 10

Algorithms: CSE 202 — Homework I

Problem 1: Stable matching with indifference (KT 1.5)


The Stable Matching Problem, as discussed in the text, assumes that all men and women have a
fully ordered list of preferences. In this problem we will consider a version of the problem in which
men and women can be indifferent between certain options. As before we have a set M of n men
and a set W of n women. Assume each man and each woman ranks the members of the opposite
gender, but now we allow ties in the ranking. For example (with n = 4), a woman could say that
m1 is ranked in first place; second place is a tie between m2 and m3 (she has no preference between
them); and m4 is in last place. We will say that w prefers m to m0 if m is ranked higher than m0 on
her preference list (they are not tied).
With indifferences in the rankings, there could be two natural notions for stability. And for each,
we can ask about the existence of stable matchings, as follows.

(a) A strong instability in a perfect matchings S consists of a man m and a woman w, such that
each of m and w prefers the other to their partner in S. Does there always exist a perfect
matching with no strong instability? Either give an example of a set of men and women with
preference lists for which every perfect matching has a strong instability; or give an algorithm
that is guaranteed to find a perfect matching with no strong instability.

(b) A weak instability in a perfect matching S consists of a man m and a woman w, such that their
partners in S are w0 and m0 , respectively, and one of the following holds:

- m prefers w to w0 , and w either prefers m to m0 or is indifferent between these two choices;


or
- w prefers to m to m0 , and m either prefers w to w0 or is indifferent between these two
choices.

In other words, the pairing between m and w is either preferred by both, or preferred by one while
the other is indifferent. Does there always exist a perfect matching with no weak instability? Either
give an example of a set of men and women with preference lists for which every perfect matching
has a weak instability; or give an algorithm that is guaranteed to find a perfect matching with no
weak instability.

Solution.

(a) Yes, there always exists a perfect matching with no strong instability. In this problem, we
don’t have a fully ordered list of preferences because of the indifference. The simplest way to
break this tie is to order the indifferent persons using their subscripts. For example, if man
m is indifferent between women wi and wj , we can rank wi higher than wj if i < j, otherwise,
rank wj higher than wi . We can do it similarly to women’s preference list, that is, if woman
w is indifferent between men mk and ml , we can rank mk higher than ml if k < l, otherwise,

1
rank mk higher than ml . Applying this method to the example, w1 ’s preference list will be
m1 > m2 > m3 > m4 . After this modification, we can get a fully ordered list of preference for
both men and women.
Then we can run the Gale-Shapley algorithm with the fully ordered list of preferences as input
and it will return a stable matching, which is a perfect matching with no strong instability.

(b) No, there exists a set of preferences with indifferences such that every perfect matching has a
weak instability. We provide a counterexample as follows:
We have a set M = {m1 , m2 } of two men, and set W = {w1 , w2 } of two women. Suppose
m1 is indifferent between w1 and w2 , m2 prefers w1 to w2 , and both of w1 and w2 prefers
m1 than m2 . We have two perfect matching in this case which are {(m1 , w1 ), (m2 , w2 )} and
{(m1 , w2 ), (m2 , w1 )}. For each matching, it is the case that w1 and w2 prefer m1 , and m1 is
indifferent between w1 and w2 .

Problem 2: Stable matching considering truthfulness (KT 1.8)


For this problem, we will explore the issue of truthfulness in the Stable Matching Problem and
specifically in the Gale-Shapley algorithm. The basic question is: Can a man or a woman end up
better off by lying about his or her preferences? More concretely, we suppose each participant has a
true preference order. Now consider a woman w. Suppose w prefers man m to m0 , but both m and
m0 are low on her list of preferences. Can it be the case that by switching the order of m and m0 on
her list of preferences (i.e., by falsely claiming that she prefers m0 to m) and running the algorithm
with this false preference list, w will end up with a man m00 that she truly prefers to both m and
m0 ? (We can ask the same question for men, but will focus on the case of women for purpose of
this question.)
Resolve this question by doing one of the following two things:

(a) Give a proof that, for any set of preference lists, switching the order of a pair on the list cannot
improve a woman’s partner in the Gale-Shapley algorithm; or

(b) Give an example of a set of preference lists for which there is a switch that would improve the
partner of a woman who switched preferences.

Solution.
Yes, a woman can end up with a desirable man by falsely claiming her preferences. As discussed
in the textbook, Gale-Shapley algorithm is “unfair”, that is, if men propose, men will end up with
their best possible (among all stable matchings) partner. If women propose, women will end up
with their best possible partner. By falsely claiming preferences, a woman or a man might be able
to improve the outcome.
The following concrete example shows that one can get a better outcome by being untruthful
about their preferences.
Suppose we have a set M = {m1 , m2 , m3 } of three men and a set W = {w1 , w2 , w3 } of three
women. Their preference lists are as follows:
m1 w3 >w1 >w2 w1 m1 >m2 >m3
m2 w3 >w2 >w1 w2 m2 >m1 >m3
m3 w2 >w3 >w1 w3 m3 >m2 >m1

2
In the male version of Gale-Shapley algorithm, assume that m1 will propose to w3 who is ranked
the first. m1 and w3 are engaged. Assume that m2 proposes next. He will first propose to w3 and
since w3 prefer m2 to m1 , w3 accepts the proposal and matches with m2 setting m1 free. m1 then
proposes to w1 , and they form a match. Now, m3 proposes to w2 and w2 accepts. The algorithm
outputs the following stable matching: (m1 , w1 ), (m2 , w3 ), (m3 , w2 ).
Assume now that w3 lies about her preferences and all others state their preferences truthfully
as in the following table.
m1 w3 >w1 >w2 w1 m1 > m2 > m3
m2 w3 >w2 >w1 w2 m2 > m1 > m3
m3 w2 >w3 >w1 w3 m3 >m1 (m2 )>m2 (m1 )
If we run the male version of the Gale-Shapley algorithm again, w3 accepts the proposal from
m1 as before. In the next round, when m2 proposes to w3 , w3 rejects m2 although she actually
prefers m2 to m1 . m2 then goes on to propose to w2 who accepts the proposal. Then m3 will
propose to w2 , but w2 rejects. m3 will then propose to w3 . w3 accepts the proposal forming a match
with m3 who is her first choice. Then m1 becomes free and he will propose to w1 who accepts.
The algorithm outputs the following matching where w3 ends up with a better outcome: (m1 , w1 ),
(m2 , w2 ), (m3 , w3 ).
Through falsely claiming she prefers m1 to m2 , woman w3 successfully engages with m3 , who is
her favorite man. Thus, we conclude that running the Gale-Shapley algorithm with false preference
list, woman will end up with a desirable man.

Problem 3: Coulomb forces (KT 5.4)


You’ve been working with some physicists who need to study, as part of their experimental design,
the interactions among large numbers of very small charged particles. Basically, their setup works
as follows. They have an inert lattice structure, and they use this for placing charged particles at
regular spacing along a straight line. Thus we can model their structure as consisting of the points
{1, 2, 3, . . . , n} on the real line; and at each of these points j, they have a particle with charge qj .
(Each charge can be either positive or negative.)
They want to study the total force on each particle, by measuring it and then comparing it to a
computational prediction. This computational part is where they need your help. The total net
force on particle j, by Coulomb’s Law, is equal to
X Cqi qj X Cqi qj
Fj = − .
(j − i)2 (j − i)2
i<j i>j

They’ve written the following simple program to compute Fj for all j:


It’s not hard to analyze the running time of this program: each invocation of the inner loop,
over i, takes O(n) time, and this inner loop is involved O(n) times total, so the overall running time
is O(n2 ).
The trouble is, for the large values of n they’re working with, the program takes several minutes
to run. On the other hand, their experimental setup is optimized so that they can throw down n
particles, perform the measurements, and be ready to handle n more particles within a few seconds.
So they’d really like it if there were a way to compute all the forces Fj much more quickly, so as to
keep up with the rate of the experiment.
Help them out by designing an algorithm that computes all the forces Fj in O(n log n) time.

3
for j = 1, 2, . . . , n do
Initialize Fj to 0
for i = 1, 2, . . . , n do
if i < j then
Cqi qj
Add (j−i) 2 to Fj

else if i > j then


Cqi qj
Add − (j−i) 2 to Fj

end if
end for
Output Fj
end for

Solution. We solve the problem by reduction to convolution. Remember that the convolution of two
vectors A =P(a0 , a1 , . . . , an−1 ) and B = (b0 , b1 , . . . , bn−1 ) is defined as D = A⊗B = (d0 , d1 , . . . , d2n−2 )
with dj = ji=0 ai bj−i .
Note that convolution is the exact same problem as polynomial multiplication. Multiplying
the two polynomials a0 + a1 x + a2 x2 + · · · + aP n−1 x
n−1 and b + b x + b x2 + · · · + b
0 1 2 n−1 x
n−1 gives
2 2n−2 j
d0 + d1 x + d2 x + · · · + d2n−1 x with dj = i=0 ai bj−i .
As we learned in class, we can solve convolution/polynomial multiplication in time O(n log n)
using Fast Fourier Transform.
In our particular instance, let

A = (q1 , . . . , qn , 0, . . . , 0)
| {z }
n−1
1 1 1 1 1 1
B = (− , . . . , − 2 , − 2 , 0, 2 , 2 , . . . , ).
(n − 1)2 2 1 1 2 (n − 1)2

i.e, (
qi+1 if 0 ≤ i ≤ n − 1
ai =
0 otherwise

0

 if i = n − 1
1
bi = − (n−1−i) 2 if 0 ≤ i < n − 1
 1
if n − 1 < i ≤ 2n − 2

 2
(i−n+1)

Both vectors have 2n − 1 elements. Let D = (d0 , . . . , d4n−4 ) = A ⊗ B. We claim that the final

4
answer is Fj = Cqj dn+j−2 for j = 1, . . . , n. To verify, we see that
n+j−2
X
dn+j−2 = ai bn+j−i−2
i=0
 
j−2 n−1 n+j−2
! !
X X X
= ai bn+j−i−2 + (aj−1 bn−1 ) +  ai bn+j−i−2  + ai bn+j−i−2
i=0 i=j i=n
j−2 n−1
X 1 X 1
= qi+1 2
+0− qi+1 +0
(j − i − 1) (i − j + 1)2
i=0 i=j
j−1 n
X 1 X 1
= qi 2
− qi
(j − i) (i − j)2
i=1 i=j+1

which is exactly what we need.

Problem 4: Local minimum (KT 5.7)


Suppose now that you’re given an n × n grid graph G. (An n × n grid graph is just the adjacency
graph of an n × n chessboard. To be completely precise, it is a graph whose node set is the set of all
ordered pairs of natural numbers (i, j), where 1 ≤ i ≤ n and 1 ≤ j ≤ n; the nodes (i, j) and (k, l)
are joined by an edge if and only if |i − k| + |j − l| = 1.)
Each node v is labeled by a real number xv ; you may assume that all these labels are distinct.
Show how to find a local minimum (A node v of G is a local minimum if the label xv is less than
the label xw for all nodes w that are joined to v by an edge. ) of G using only O(n) probes to the
nodes of G. (Note that G has n2 nodes.)

Solution. Let A be a m × n matrix of reals with indexes (i, j) for 1 ≤ i ≤ m and 1 ≤ j ≤ n.


The neighbors of entry (i, j) are those (i0 , j 0 ) with |i − i0 | + |j − j 0 | = 1. A local minimum is some
(i, j) such that for each neighbor (i0 , j 0 ), Ai,j ≤ Ai0 ,j 0 . An inefficient greedy method to find a local
minimum is to start anywhere and while you have a smaller neighbor, move to that neighbor. This
process must terminate since each move results in a strictly smaller number. While greedy method
may not lead to an efficient algorithm, it is nevertheless useful since it establishes the existence of a
local minimum within a region of the matrix.
We will show how to find a local minimum using O(m + n) queries to A. There are many
solutions to this problem, but there are many appealing non-solutions as well, so be careful.
We will solve a slightly more general problem: we will also be given as input the index c of an
element in A and find some local minimum ≤ Ac . The greedy method starting at c shows such a
local minimum exists. It is necessary to require the algorithm to find a local minimum subject to
certain upperbound to ensure correctness of the algorithm.
If m, n < 3, use exhaustive search. Otherwise, suppose wlog that n ≥ m and let M be the
middle column of A (if n < m, we would use the middle row). More precisely, we will let M to be
the set of indexes corresponding to the middle column. Find an index a in M ∪ {c} such that Aa
is the smallest number among all Ax where x in M ∪ {c}. If a is a local minimum, we are done.
Otherwise, there is some neighbor b ∈ / M ∪ {c} of a with Ab < Aa . M divides A into 2 halves. Let
B be the half of A, not including M , that contains b.
We claim that B contains a local minimum x such that Ax ≤ Ab and that any such local
minimum is a local minimum of A such that it is less than or equal to Ac . To see the first claim,

5
L1
L5
L2 L4

L3

Figure 1: An instance of hidden surface removal with five lines (labeled L1 to L5 in the figure). All
the lines except for L2 are visible.
note that the greedy method starting at b ∈ B cannot cross M since every value in M is strictly
larger than Ab . To see the second claim, let d be a local minimum of B such that Ad ≤ Ab . It is
clear that Ad ≤ Ac since Ab ≤ Ac . To prove that d is a local minimum of A, assume for the sake
of contradiction that d is not a local minimum of A. Then d has a neighbor e ∈ M with Ae < Ad .
But then Ae < Ad ≤ Ab < Aa , contradicting that Aa is a smallest element among all Ax for x in
M ∪ {c}.
Each recursive call uses O(max{m, n}) queries to A and cuts the larger of {m, n} in half, so the
total number of queries is at most a constant times (m + m/2 + m/4 + · · · ) + (n + n/2 + n/4 + · · · ),
which is O(m + n).

Problem 5: Hidden surface removal (KT 5.5)


Hidden surface removal is a problem in computer graphics that scarcely needs an introduction:
when Woody is standing in front of Buzz, you should be able to see Woody but not Buzz; when
Buzz is standing in front of Woody, . . . well, you get the idea.
The magic of hidden surface removal is that you can often compute things faster than your
intuition suggests. Here’s a clean geometric example to illustrate a basic speed-up that can be
achieved. You are given n nonvertical lines in the plane, labeled L1 , L2 , . . . , Ln , with the ith line
specified by the equation y = ai x + bi . We will make the assumption that no three of the lines all
meet at a single point. We say line Li is uppermost at a given x-coordinate x0 if its y-coordinate at
x0 is greater than the y-coordinates of all the other lines at x0 : ai x0 + bi > aj x0 + bj for all j 6= i.
We say line Li is visible if there is some x-coordinate at which it is uppermost – intuitively, some
portion of it can be seen if you look down from “y = ∞”.
Give an algorithm that takes n lines as input and in O(n log n) time returns all the ones that
are visible. Figure 1 gives an example.

Solution.

Solution 1
We use a divide-and-conquer algorithm that is very similar to MergeSort or the Skyline problem.
As a preprocessing step, we sort the input lines by slopes. For the remainder of this solution we
then consider the problem of finding the visible envelope of the lines L1 , . . . , Ln where the lines are
sorted by increasing slope. Then we split the set of input lines into two equal parts (i.e. the “divide”

6
Figure 2: L1 , L3 , L4 , and L5 are visible. L2 is hidden. The intersections points are p1 , p2 , and p3
in the order of x-coordinate. The visible envelope is (L1 , p1 , p2 , p3 , L5 ).
step), and recursively compute the visible envelope for each part (i.e. the “conquer” step). Note
that the subproblems also have the property that the lines are sorted by slope.
The main step is on how to efficiently combine the two visible envelopes into one single visible
envelope of the original input lines (i.e. the “combine” step). Here, we leverage the fact that the
slopes in the second visible envelope are larger than the slopes in the first visible envelope because
we sorted the input lines by slopes. This guarantees that the two visible envelopes meet at exactly
one intersection point. By exploiting the sorted order of slopes, this intersection point can be
determined in linear time.
Full details of the algorithm are as follows.

1. First, sort the input lines by slopes with time complexity O(n log n). Among the lines with
the same slope, keep only the one with largest y-intercept (since the others are hidden by this
line). From now on, we suppose L1 , L2 , ..., Ln are sorted by unique slopes.

2. We design an algorithm that returns the set of visible lines in increasing slopes (Li1 , Li2 , ...,
Lim ), together with the set of intersection points (p1 , p2 , ..., pm−1 ) where pj is the intersection
of Lij and Lij+1 . See Figure 2 for an example.

3. We apply the divide-and-conquer technique. Split the original set L1 , L2 ,..., Ln into two
halves L1 , L2 , ..., Ln/2 and Ln/2+1 , Ln/2+2 , ..., Ln . Recursively call the algorithm on each
half. Note that all lines in the second half have higher slopes than those in the first half.

4. The base cases for n ≤ 3 can be handled trivially in constant time.

5. Calling the algorithm on the first half L1 , L2 , ..., Ln/2 returns the set of visible lines (Li1 , Li2 ,
..., Lis ) and the set of intersection points P = (p1 , p2 , ..., ps−1 ).
Denote the visible envelope Φ = (Li1 , p1 , p2 , ..., ps−1 , Lis ), i.e as a sequence of points with a
ray at either end.

6. Calling the algorithm on the second half Ln/2+1 , Ln/2+2 , ..., Ln returns the set of visible lines
(Lj1 , Lj2 , ..., Ljt ) and the set of intersection points Q = (q1 , q2 , ..., qt−1 ).
Denote the visible envelope Ψ = (Lj1 , q1 , q2 , ..., qt−1 , Ljt ).

7
Figure 3: Combining two visible envelopes Φ and Ψ. Line segments (pk−1 , pk ) and (ql−1 , ql ) intersect
at r∗ .

7. Now we combine the two sets of visible lines and intersection points as shown in Figure 3. Note
that Li1 has the smallest slope in Φ ∪ Ψ and hence is always visible. Also Ljt has the largest
slope in Φ ∪ Ψ and hence is always visible. So the two envelopes Φ and Ψ must intersect at at
least one point (otherwise either Φ or Ψ is completely hidden by the other, which contradicts
the fact that both Li1 in Φ and Ljt in Ψ are visible).
Let (pk−1 , pk ) in Φ and (ql−1 , ql ) in Ψ be two line segments intersecting at point r∗ for some
1 ≤ k ≤ s and 1 ≤ l ≤ t (c.f. Figure 3).
To merge the two visible lines, we need to find the indices k and l as well as r∗ . The merged
visible envelope is then simply the left envelope up to the point r∗ , followed by the right
envelope from point r∗ ∗ onwards.
To find r∗ we scan the two visible envelopes from left to right until we find the two line
segments that intersect. To make notation simpler, we identify the rays at the beginning and
end of the envelope with a segment-like notation (p0 , p1 ) and (ps−1 , ps ) respectively. Start
with k = 0, l = 0, i.e. the leftmost rays. If they intersect, we have found r∗ . If they do not
intersect and the x-coordinate of pk+1 is smaller than the x-coordinate of ql+1 increment k,
otherwise increment l. Repeat these steps until we found segments (pk , pk+1 ) and (ql , ql+1 )
that do intersect. Note that we are guaranteed to find an intersection.

8. Correctness: To argue correctness we need to show that our merging procedure is correct.
In particular we need to show that there is indeed exactly one intersection between Φ and
Ψ. For this, consider Φ and Ψ as piecewise linear functions and consider the function
f (x) = Ψ(x) − Φ(x) (right side minus the left side). This function is again a piecewise linear
function, and the points where the slope of f (x) changes is simply the union of the points
where the slopes of Φ and Ψ changes. It is not hard to see that the roots of f (x) are exactly
the x-coordinates where Φ and Ψ intersect. Since the slope of Li1 is smaller than the slope of
Lj1 we have limx→−∞ f (x) = −∞. Likewise since the slope of Ljt is larger than the slope of
Lis we also have limx→∞ f (x) = ∞. Since the function f (x) is continuous there must be at
least one point x∗ with f (x∗ ) = 0. Furthermore, for every x the slope of Ψ(x) is larger than
the slope of Φ(x), hence f (x) is monotone (always increasing) and has therefore exactly one

8
root. Since our merge procedure goes mimics MergeSort in the way it goes through all points
from left to right, it does in fact test for every segment of f (x) if it contains a root. Hence it
is guaranteed to find the unique root.

9. Complexity analysis:

• In the combining step, merging P and Q takes O(n). So the divide-and-conquer takes
time T (n) = 2T (n/2) + O(n) = O(n log n).
• The initial sorting input lines by slope takes O(n log n).
• Therefore, the total time complexity is O(n log n).

Solution 2
We will represent a visible envelope consisting of k visible lines as a sequence of pairs (xj , uj )
for 1 ≤ j ≤ k, such that line Luj is visible from x-coordinates xj to xj+1 . Note that x1 = −∞ and
xk+1 = ∞.
The idea of this algorithm is to construct a sequence of envelopes P1 , . . . , Pn such that Pi is the
visible envelope of the set of lines {L1 , . . . , Li }. In particular, Pn will be the final output of our
algorithm.
As a base case we set P1 = ((−∞, 1)), which is trivially the correct visible envelope for only one
line.
For the recursion step we use the fact that for every visible envelope, the slopes are increasing
from left to right. In particular this means that any new line Li intersect the visible envelope at 0,
1, or 2 positions. To find the left intersection (if it exists) we can use binary search. We need to find
the j, such that the slope of Luj is smaller that the slope of Li and Li and Luj intersect between
x-coordinates xj and xj+1 , i.e. if the slope of Luk/2 is larger than the slope of Li or the intersection
Luk/2 and Li is smaller than xk/2 , recurse on the left half, otherwise recurse on the right half. We
also need to account for the special case where the slope of Li is smaller than the slope of any line
in the visible envelope, in which case there is no left intersection and Li is the leftmost ray of the
new visible envelope instead. For this case we say that the left intersection is at x-coordinate −∞.
Similarly, we can use binary search to find the right intersection. The new visible envelope Pi
then is equal to Pi−1 if there is neither a left nor a right intersection. Otherwise, Pi consists of Pi−1
up to the left intersection, Li up to the right intersection, and then again Pi−1 for the rest.
Correctness follows from the correctness of the base case (which is trivial) and the correctness of
the binary search to find the intersection points.
For the runtime analysis, we note that we need to do 2n binary searches with time O(log n)
each, such that the total runtime is O(n log n).

Solution 3
This solution is a variant of solution 2. Instead of adding the lines in any order to the visible
envelope we add the lines by increasing slope. This then allows us to add every line in (amortized)
constant time.
To prepocess, we first sort the lines by slope and if there are multiple lines the same slope, we
only keep the higher one (as the lower one can never be part of the visible envelope anyway). From
now on we assume that the lines L1 , . . . , Ln are sorted in increasing order by slope and that no two
lines have the same slope.

9
We again set P1 = ((−∞, 1)). Note that L1 will remain the leftmost ray for all visible envelopes
Pi , as it is the line with the smallest slope.
To compute Pi from Pi−1 , we observe that Li is necessarily the rightmost ray of Pi , as it has the
largest slope among all lines L1 , . . . , Li . The only question is where Li intersects Pi−1 . To find this
intersection we can do a linear scan from right to left. Let (xk , uk ) be the last tuple in Pi−1 and let
x be the x-coordinate of the intersection of Li and Luk . If x > xk , then Li intersects the rightmost
ray of Pi−1 . In this case we simply append (x, i) to the visible envelope to get Pi . Otherwise, Luk is
below Li for the whole interval (xk , ∞), and therefore not part of the visible envelope Pi . In this
case remove (xk , uk ) from the visible envelope and we iterate by comparing the intersection of Li
and Luk−1 with xk−1 .
Correctness follows from how we argued that there is exactly one intersection between Pi−1 and
L1 and that our linear scan finds the correct intersection point.
The prepocessing step requires time O(n log n) since we need to sort the lines. For the rest of
the algorithm, we can do an amortized runtime analysis. Each time we compute the intersection of
two lines Lj and Li with j < i, we either

1. Remove Lj from the visible envelope

2. Add Li to the visible envelope

Since we add every line exactly once to the visible envelope and remove each line at most once
from the envelope, the number of steps can be at most 2n. Hence the whole algorithm without the
prepocessing step takes O(n) time and the algorithm including the prepocessing step takes time
O(n log n).

10

Вам также может понравиться