def test_modify_key(backend): heap = DHeap(backend) populate_heap(heap, order='REVERSE') for item in reversed([x for x in heap]): heap.modifykey(item, -1) assert heap.findminkey() == -1 assert heap.deletemin() == item
def spanning_tree_edges(graph, edge_weight): components = UnionFind(memory_usage=graph.MemoryUsage) sorted_edges = DHeap(memory_usage=graph.MemoryUsage) for edge in graph.edges(): sorted_edges.insert(edge, key=edge_weight[edge]) while sorted_edges: edge = sorted_edges.deletemin() if components.link(*edge) is not None: yield edge
def test_meld(backend): h1 = DHeap(backend, items = [x for x in xrange(0,300)]) h2 = DHeap(backend, items = [x for x in xrange(300,500)]) h3 = DHeap(backend, items = [x for x in xrange(500,700)]) h4 = DHeap(backend, items = [x for x in xrange(700,1000)]) h1.meld(h2) h3.meld(h4) h1.meld(h3) assert sorted(x for x in h1) == [x for x in xrange(0,1000)] remove_items_from_heap(h1)
def test_multiple_identical_keys(backend): heap = DHeap(backend) items = [x for x in xrange(100)] * 5 populate_heap(heap, items=items) remove_items_from_heap(heap) heap = DHeap(backend) items = [x for x in 'abcdefghijklmnopqrstuvwxy'] key = dict(zip(items, [0,1,2,3,4] * 5)) populate_heap(heap, items=items, key=key) preimages = dict() for i in xrange(5): preimages[i] = set(x for x,y in key.iteritems() if y == i) for i in xrange(len(heap)): item = heap.deletemin() assert item in preimages[i / 5] preimages[i / 5].remove(item)
def test_get_key(backend): heap = DHeap(backend) populate_heap(heap) for item in heap: assert heap.getkey(item) == item