Академический Документы
Профессиональный Документы
Культура Документы
(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:
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 .
(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.
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
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
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).
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.
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
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