CS331 Advanced Data Structures – Spring 2016 Due: May 4

advertisement
CS331 Advanced Data Structures – Spring 2016
Homework 10 – 30 points
Due: May 4
1. (15 points) Consider the following set of points:
(4, 1), (2, 3), (−8, 0), (2, 0), (2, 5), (−4, −1), (−1, −2),
(0, 4), (−3, 2), (6, −1), (−4, 1), (5, 4), (−1, 3), (3, 1)
(a) Order the above points using their polar angle with respect to the point with lowest y
coordinate.
(b) Determine the convex hull for these points using Graham’s scan. Show the contents of
the stack after the processing of each point.
(c) Determine the area of the convex hull for these points. Show your work.
2. (5 points) The code I outlined in class for determining the closest pair of points in the plane
was the following:
double closest(point px[], point py[], int n)
{
if (n <= 3)
<return closest pair>
else {
split px into pxL and pxR, and determine the dividing line x=l
split py into pyL and pyR
dl = closest(pxL, pyL, n/2);
dr = closest(pxR, pyR, n/2);
d = min(dl, dr);
int m=0, point y[n];
for(int i=0; i<n; i++) {
if (py[i].x in [l-d, l+d])
y[m++] = py[i];
}
for(int i=0; i<m-7; i++) {
p = y[i];
for(int j=i+1; j<=i+7; j++) {
d2 = dist(p, y[j]);
if (d2 < d)
d = d2;
}
}
return d;
}
}
There’s a major error in this code! Determine what it is and fix it.
3. (10 points) Write code to implement the minimum cost triangularization algorithm described
in class. Your code should first read in an integer n indicating the number of points in the polygon, and then read in n pairs of xy coordinates. You may assume the polygon is convex and
that the vertices are given in clockwise order. Have your code output the min-cost table which
stores the Ci,s values for i = 0, 1, . . . n − 1 and s = 2, 3, . . . 4. Show the results for the polygon
described by the following points: (14, 10), (16, 5), (13, 1), (7, 0), (4, 1), (0, 7), (0, 12), (2, 14).
Download