0% found this document useful (0 votes)
309 views

15-451 Homework 7 Solutions

This homework assignment for an algorithms course includes three problems: 1) Analyzing the Move-Half-Way-To-Front list update algorithm and proving it is 6-competitive. 2) Giving an O(n^2) algorithm to find the triangle of maximum area formed by 3 points among n input points. 3) Giving an O(n log n) algorithm to find the square of a given side length that contains points with maximum total weight, using a sweep line algorithm and segment trees.

Uploaded by

jane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
309 views

15-451 Homework 7 Solutions

This homework assignment for an algorithms course includes three problems: 1) Analyzing the Move-Half-Way-To-Front list update algorithm and proving it is 6-competitive. 2) Giving an O(n^2) algorithm to find the triangle of maximum area formed by 3 points among n input points. 3) Giving an O(n log n) algorithm to find the square of a given side length that contains points with maximum total weight, using a sweep line algorithm and segment trees.

Uploaded by

jane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

15-451 Algorithms, Spring 2016

Homework #7

Due: Apr 2628, 2016

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) 1. (Move-Half-Way-To-Front)


Heres an alternative on-line algorithm for the List Update problem discussed in lecture
23.
Move-Half-Way-To-Front: When an element x is accessed, swap x with
its predecessors until its distance to the front is at most half what it was
before. To be more precise, suppose when we access x there there are k
elements in front of it. After the operation there will be bk/2c elements in
front of it.
Question: Is there a constant c such that this algorithm (MHWTF) is c-competitive?
If the answer to the above question is yes, then find the constant c such that: (1)
MHWTF is c-competitive, and (2) MHWTF is not c0 -competitive for any c0 < c (for
sufficiently large n). Prove both parts.
If the answer to the above question is no, then prove it.
Solution: The answer is 6. A simple modification of the proof presented in lecture works.
The potential is going to be three times the number of inversions between the two lists.
We compare our algorithm MHWTF with some other algorithm B.
Consider a typical access to an element x. Let M be the dk/2e elements that MHWTF
moves x past. Now partition M into S and T where S are those elements of M that are
in front of x in Bs list and T are those that are in back of x in Bs list.
CMHWTF = 1 + |S| + |T | + 2(|S| + |T |) = 1 + 3(|S| + |T |)
= 3(|S| |T |)
AmortizedCMHWTF = 1 + 6|S| < 6(1 + |S|) = 6 CB
Also when B does a swap CB = 1, and the potential could increase by at most 3. Thus
AmortizedCMHWTF 3 < 6 CB .
To prove that the constant 6 is optimal, consider a sequence that always accesses the last
element in the MHWTF list. Each of these accesses costs (3/2)n. The optimal algorithm
will put these n/2 elements at the front of the list and never move them again. The average
cost to this algorithm is therefore n/4, proving the result.

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

You might also like