The min heap uses heapq as well as custom written _siftup and _siftdown methods to allow the heap positions to be tracked by an additional dict keyed by element to position. The smallest element can be popped in O(1) time, new elements can be pushed in O(log n) time, and any element can be removed or updated in O(log n) time. The queue cannot contain duplicate elements and an attempt to push an element already in the queue will have no effect.
MappedQueue complements the heapq package from the python standard library. While MappedQueue is designed for maximum compatibility with heapq, it adds element removal, lookup, and priority update.
The MappedQueue class implements a min-heap with removal and update-priority.
A MappedQueue
can be created empty or optionally given an array of initial elements. Calling :None:None:`push()`
will add an element and calling :None:None:`pop()`
will remove and return the smallest element.
>>> q = MappedQueue([916, 50, 4609, 493, 237])
... q.push(1310) True
>>> [q.pop() for i in range(len(q.heap))] [50, 237, 493, 916, 1310, 4609]
Elements can also be updated or removed from anywhere in the queue.
>>> q = MappedQueue([916, 50, 4609, 493, 237])See :
... q.remove(493)
... q.update(237, 1117)
... [q.pop() for i in range(len(q.heap))] [50, 916, 1117, 4609]
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.utils.mapped_queue.MappedQueue
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them