15-451 Homework 7 Solutions
15-451 Homework 7 Solutions
Homework #7
This is an oral presentation assignment. Groups of three, sign-up sheet online (details on
Piazza), etc. same rules as for HW#2.
Problem #B is a bonus problem, worth 1% under the reci/quiz/bonus category. Submissions via gradescope by Apr 28th.
(100/3 pts) 2. Triangle of Maximal Area Given n points in the plane, give an O(n2 ) algorithm to
compute the triangle of maximum area that can be formed by selecting three of these
points.
Solution: Compute the convex hull of the points. Call the points on the convex hull
p0 , p1 , . . . , pm1 in counterclockwise order.
The triangle must contain three points on the convex hull (CH). Indeed, if a, b, c is the
triangle and c is not on the CH, then draw a line parallel to ab passing through c. At least
one point d on the CH must be strictly above this line (else c would be on the CH). And
then a, b, d would have greater area.
For the O(n2 ) algorithm, walk three points i, j, and k around the hull in counterclockwise
order. For each point pi we run the following algorithm.
Starting with j = k = (i + 1) mod m advance k (modulo m) around until the
distance between pk and the line between pi and pj is about to decrease. Then
stop. At this point, triangle(pi , pj , pk ) is the one of maximum area given that i
and j are fixed. Now we advance j by 1 (modulo m) and repeat the process.
We do this until j = i at which point we stop.
This is like the two-sum problem from recitation #15. For the O(n2 log n) solution, for
some fixed a, b try to find the c that is furthest from the line ab using ideas from max-finding
in semi-sorted arrays, also from recitation #15.
(100/3 pts) 3. (Square)
Youre given a set of n points in the plane, each with a weight. The ith point is (xi , yi )
and has weight wi . Youre given a side length s. Consider all squares of side length s
with sides parallel to the coordinate axies. Give an O(n log n) algorithm to compute
the square that contains the set of points with the most total weight.
Solution: Its a sweep-line algorithm that uses segment trees.
We first construct a set of interesting x-coordinates and y-coordinates where something
happens. (E.g., if we think of the square as being specified by its left bottom corner, the
interesting x-coordinates will be xi s (where the sweep-line starts to see (xi , yi )), and xi
(where it stops seeing (xi , yi )). And similarly, the interesting y-coordinates will be yi s
and yi . Build a segment tree for the interesting y-coordinates. The segment tree maintains
a colleciton of variables. It supports increment-range by d. It supports find-max in range.
We do a vertical sweep-line from left to right. When we are at position xi s for some
point i, some set of squares with their left-bottom corners on the sweepline can see
(xi , yi ). These are the squares whose left-bottom corners lie between yi s and yi . So we
increment all the variables in the range yi s, yi by the appropriate weight wi . When we
are at position xi for some i, we decrement the same range by the weight wi . Meanwhile
we keep testing for the maximum point in the entire tree whenever anything changes.
There are 2n segment-tree operations each takes O(log n). So the algorithm is O(n log n).