class TestHashHeap(unittest.TestCase): def setUp(self): self.pq = HashHeap() array = [8, 5, 3, 9, 6, 4, 2, 6] for n in array: self.pq.add(n) # def tearDown(self) def test_of_add(self): for i in range(1, len(self.pq.heap)): p = (i - 1) // 2 self.assertLess( self.pq.heap[p], self.pq.heap[i]) def test_of_pop(self): sorted_array = [] while self.pq: sorted_array.append(self.pq.pop()) for i in range(1, len(sorted_array)): self.assertLessEqual( sorted_array[i-1], sorted_array[i]) def test_of_delete(self): self.pq.delete(5) self.pq.delete(6) self.pq.delete(-1) for i in range(1, len(self.pq.heap)): p = (i - 1) // 2 self.assertLess( self.pq.heap[p], self.pq.heap[i]) self.assertFalse(5 in self.pq) self.assertTrue(6 in self.pq)
def dijkstra(self, graph, source): """Dijkstra This method computes shortest distance and path for each node from the source node, and save results in each node's distance attribute. return None """ reachable = self.preprocessing(graph, source) self.dists[source.id] = 0 self.paths[source.id] = source.id unknown = HashHeap( key=lambda x, y: self.dists[x.id] < self.dists[y.id]) for node_id in reachable: unknown.add(graph[node_id]) # add to unknown marked = [False] * len(graph) while unknown: node = unknown.pop() marked[node.id] = True for edge in node.edges: adj = graph[edge.head] if (not marked[adj.id] and self.dists[node.id] + edge.dist < self.dists[adj.id]): self.dists[adj.id] = self.dists[node.id] + edge.dist unknown.update(adj) # method only in hash heap self.paths[adj.id] = node.id
def test_of_add_and_delete(self): pq = HashHeap('min') array = random.sample(range(100000), 10000) for each in array: pq.add(each) array.sort() for i in range(10000): min = pq.top() self.assertEqual(min, array[i]) pq.delete(min)
def building_outlines(buildings): points = [] for b in buildings: start, end, height = b[0], b[1], b[2] points.append(Point(start, height, True)) points.append(Point(end, height, False)) points.sort() ans = [] heap = HashHeap('max') start = 0 for p in points: if p.is_start: if not heap or heap.top() < p.height: if heap: ans.append([start, p.x, heap.top()]) start = p.x heap.add(p.height) else: height = heap.top() heap.delete(p.height) if not heap or heap.top() < height: ans.append([start, p.x, height]) start = p.x return ans
def setUp(self): self.pq = HashHeap() array = [8, 5, 3, 9, 6, 4, 2, 6] for n in array: self.pq.add(n)
def __init__(self): self.min_pq = HashHeap() self.max_pq = HashHeap(key=lambda x, y: x > y)
class MinMaxHeap: def __init__(self): self.min_pq = HashHeap() self.max_pq = HashHeap(key=lambda x, y: x > y) def __len__(self): return len(self.min_pq) def get_min(self): return self.min_pq.top() def get_max(self): return self.max_pq.top() def add(self, key): self.min_pq.add(key) self.max_pq.add(key) def pop_min(self): ans = self.min_pq.pop() self.max_pq.delete(ans) return ans def pop_max(self): ans = self.max_pq.pop() self.min_pq.delete(ans) return ans
def test_push(self): hh = HashHeap(htype=MAX_HEAP) [hh.push(x, str(x)) for x in xrange(5)] assert hh.size() == 5 hh.push(1,'1') assert hh.size() == 5
def test_peek(self): hh = HashHeap(htype=MAX_HEAP) [hh.push(x, str(x)) for x in xrange(5)] assert hh.peek() == (4,'4') assert [x for x in hh.xpeek(count=2)] == [(4,'4'),(3,'3')]
def test_contains(self): hh = HashHeap(htype=MAX_HEAP) [hh.push(x, str(x)) for x in xrange(5)] assert 1 in hh
def test_pop(self): hh = HashHeap(htype=MAX_HEAP) [hh.push(x, str(x)) for x in xrange(5)] [hh.push(x, str(x)) for x in xrange(5)] assert hh.pop() == (4,'4')