def graham(points: List) -> List: """ Graham's scan is a method of finding the convex hull of a finite set of points in the plane with time complexity O(n log n). It is named after Ronald Graham, who published the original algorithm in 1972.The algorithm finds all vertices of the convex hull ordered along its boundary. """ # Find the bottom left point and remove it from the set bottom_left_point = min(points, key=lambda x: (x[1], x[0])) # Put the points on a min heap by polar angle polar_points = [(get_polar_angle(bottom_left_point, x), x) for x in points if x != bottom_left_point] point_heap = BinomialHeap(polar_points) # Push the first 3 elements on a stack stack = Stack() stack.push(bottom_left_point) stack.push(point_heap.pop()[1]) stack.push(point_heap.pop()[1]) for _ in range(len(point_heap)): _, p3 = point_heap.pop() while is_counterclockwise(stack.peek(1), stack.peek(), p3) < 0: stack.pop() stack.push(p3) return stack.to_array()
class TestBinomialHeap(unittest.TestCase): def setUp(self): self.list = [random.randint(1, 20) for _ in range(10)] self.heap = BinomialHeap(self.list) self.list2 = [random.randint(1, 20) for _ in range(10)] self.heap2 = BinomialHeap(self.list2) def test_pop(self): start = 0 while True: try: value = self.heap.pop() assert value >= start assert value in self.list self.list.remove(value) start = value except IndexError: break assert len(self.list) == 0 def test_insert(self): heap = BinomialHeap() heap.insert(4) heap.insert(7) heap.insert(1) heap.insert(2) heap.insert(10) assert heap.pop() == 1 heap.insert(3) assert heap.pop() == 2 assert heap.pop() == 3 assert heap.pop() == 4 assert heap.pop() == 7 assert heap.pop() == 10 def test_merge(self): self.heap.merge(self.heap2) assert len(self.heap) == 20 start = 0 combined_list = self.list + self.list2 while True: try: value = self.heap.pop() assert value >= start assert value in combined_list combined_list.remove(value) start = value except IndexError: break assert len(combined_list) == 0
class TestBinomialHeap(unittest.TestCase): def setUp(self): self.list = [random.randint(1,20) for _ in range(10)] self.heap = BinomialHeap(self.list) self.list2 = [random.randint(1, 20) for _ in range(10)] self.heap2 = BinomialHeap(self.list2) def test_pop(self): start = 0 while True: try: value = self.heap.pop() assert value >= start assert value in self.list self.list.remove(value) start = value except IndexError: break assert len(self.list) == 0 def test_insert(self): heap = BinomialHeap() heap.insert(4) heap.insert(7) heap.insert(1) heap.insert(2) heap.insert(10) assert heap.pop() == 1 heap.insert(3) assert heap.pop() == 2 assert heap.pop() == 3 assert heap.pop() == 4 assert heap.pop() == 7 assert heap.pop() == 10 def test_merge(self): self.heap.merge(self.heap2) assert len(self.heap) == 20 start = 0 combined_list = self.list + self.list2 while True: try: value = self.heap.pop() assert value >= start assert value in combined_list combined_list.remove(value) start = value except IndexError: break assert len(combined_list) == 0
def setUp(self): self.list = [random.randint(1, 20) for _ in range(10)] self.heap = BinomialHeap(self.list) self.list2 = [random.randint(1, 20) for _ in range(10)] self.heap2 = BinomialHeap(self.list2)
def test_insert(self): heap = BinomialHeap() heap.insert(4) heap.insert(7) heap.insert(1) heap.insert(2) heap.insert(10) assert heap.pop() == 1 heap.insert(3) assert heap.pop() == 2 assert heap.pop() == 3 assert heap.pop() == 4 assert heap.pop() == 7 assert heap.pop() == 10
def setUp(self): self.list = [random.randint(1,20) for _ in range(10)] self.heap = BinomialHeap(self.list) self.list2 = [random.randint(1, 20) for _ in range(10)] self.heap2 = BinomialHeap(self.list2)