def test_insert_into_heap(): heap = Heap([8, 5, 2, 4, 3, 1]) heap.insert(6) expected = [0, 8, 5, 6, 4, 3, 1, 2] output = heap.heap assert output == expected
def test_insert_empty_heap(): heap = Heap() heap.insert(4) expected = [0, 4] output = heap.heap assert output == expected
def test_insert_empty_heap_two_elements(): heap = Heap() heap.insert(1) heap.insert(3) expected = [0, 3, 1] output = heap.heap assert output == expected
def heap_sort(values: List[int]) -> List[int]: heap = Heap() sorted_values = [] for value in values: heap.insert(f'node_{value}', value) while len(sorted_values) < len(values): name, value = heap.pop_min() sorted_values.append(value) return sorted_values
def dijkstra(self, node_key: str, node_destination_key: str) -> List[Tuple[str, int]]: """ Method that returns the shortest path between 2 nodes We will store for each node its parent(previous_node) We will store the distances from source node in a distances registry 1. At first the distance registry is set to infinite except for the source node 2. We will use a heap, in which we temporarily store the nodes to visit 3. The heap will allow us to retrieve the next node with minimal distance from source node 4. From each visited nodes, we will store its neighbors in the heap, and if the distance we found is shorter than the previous one, we update the distance and store it in the heap to be visited. 5. Finally, from the destination key, we will look in the parent registry, and create the path with the distance registry """ heap_instance = Heap() # set the distances registry distances = {} visited = [] previous_node = {} for key in self.nodes.keys(): distances[key] = cmath.inf distances[node_key] = 0 # helper to create path previous_node[node_key] = None heap_instance.insert(node_key, 0) while len(heap_instance.nodes) > 0: heap_node = heap_instance.pop_min() current_key = heap_node[0] visited.append(current_key) for child_key, child_value in self.nodes[current_key].items(): if child_key in visited: continue # At first we have zero, then we take this value and we update it(so we don't deal with inf) new_dist = distances[current_key] + child_value if new_dist < distances[child_key]: distances[child_key] = new_dist heap_instance.insert(child_key, new_dist) previous_node[child_key] = current_key key = node_destination_key path = [] while previous_node[key]: path.insert(0, (key, distances[key])) key = previous_node[key] path.insert(0, (node_key, 0)) return path
def test_problem_1(self): """ You are given as input an unsorted array of n distinct numbers, where n is a power of 2. Give an algorithm that identifies the second-largest number in the array, and that uses at most n+log2n−2 comparisons. Solution: use a hash data structure. """ numbers = [5,1,2,5,1,2,3,54,6,7,1,3,3,5,6,2,3,4,56,6] h = Heap() for number in numbers: h.insert(-number) h.extract_min() actual = -h.extract_min() self.assertEqual(actual, 54, 'found the second largest number')
def test_problem_1(self): """ You are given as input an unsorted array of n distinct numbers, where n is a power of 2. Give an algorithm that identifies the second-largest number in the array, and that uses at most n+log2n−2 comparisons. Solution: use a hash data structure. """ numbers = [ 5, 1, 2, 5, 1, 2, 3, 54, 6, 7, 1, 3, 3, 5, 6, 2, 3, 4, 56, 6 ] h = Heap() for number in numbers: h.insert(-number) h.extract_min() actual = -h.extract_min() self.assertEqual(actual, 54, 'found the second largest number')
def find_rectangle_candidates( cutout: np.array, points: np.array, candidate_limit: int = 20) -> List[Tuple[np.array]]: """Returns a list of rectangles with maximum space overlap with original figure.""" heap = Heap() for quad in tqdm(combinations(points, 4), total=num_combinations(len(points), 4)): zeros = np.zeros_like(cutout) for p in quad: zeros[p[0], p[1]] = 1 chull = convex_hull_image(zeros) space_part = np.sum(chull * cutout) heap.insert((space_part, quad)) best_space, _ = heap.peek() retlist = [] total = 0 while total < candidate_limit and heap.peek()[0] >= 0.9 * best_space: space, cur_points = heap.pop() retlist.append(cur_points) return retlist
def test_insert(self): """ Test that adding an element preserves the heap property. Given the following heap: (4) / \ (4) (8) / \ / \ (9) (4) (12) (9) / \ (11) (13) """ data = [4, 4, 8, 9, 4, 12, 9, 11, 13] h = Heap(data) h.insert(7) self.assertTrue(Heap.is_heap(h.data), 'should still be a heap') h.insert(10) self.assertTrue(Heap.is_heap(h.data), 'should still be a heap') h.insert(5) self.assertTrue(Heap.is_heap(h.data), 'should still be a heap')