CSci 280: Algorithms and problem-solving paradigms
Home Syllabus Classwork

printable version

Quiz 3

[1] [2] [3] [4] [5] [6]

Problem Q3.1.

[8 pts] Graham's scan is an algorithm for finding the top half of a convex hull of two-dimensional points. It proceeds as follows.

def grahamScan(points):  # assume points are sorted by x-coordinate
    hull = [points[0], points[1]]
    for i in range(2, len(points)):
        p = points[i]
        while len(hull) >= 2 and isLeftTurn(hull[-2], hull[-1], p):
            hull.pop()
        hull.append(p)
    return hull

Using big-O notation in terms of n, the length of the points parameter, how much time does this algorithm take? Use big-O notation, giving the tightest and simplest bound you can, and justify your answer. You should assume that pop, append, and isLeftTurn take O(1) time.