H = OrderedList(key=lambda x: x.priority) It can handle tuples like (priority, value) by default but you can also customize it like this: class Val(object): Return super(OrderedList, self)._getitem_(i) Return super(OrderedList, self).pop(i)įor x in super(OrderedList, self)._iter_(): Super(OrderedList, self).insert(i, (self.key(x), x)) Super(OrderedList, self).append((self.key(x), x)) pop(0)ĭef _init_(self, iterable=None, key=None): If you want MinQueue like functionality use positive values. If you want MaxQueue like functionality use negative values. """Keep a list sorted as you append or extend itĪn ordered list, this sorts items from smallest to largest using key, so If you want to keep an entire list ordered, not just the top value, I've used some variation of this code in multiple projects, it's a drop in replacement for the standard list class with a similar api: import bisect Obviously, calling put will (and should!) raise an error if you try to insert an object which your key-function cannot process. Python 3 code from queue import PriorityQueue PriorityQueue.put(self, (self.key(x), x)) Python 2 code from Queue import PriorityQueue You won't have to insert (priority, object) tuples manually and the handling feels more natural.ĭemo of the desired behavior: > h = KeyHeap(sum) If you want inserted objects to be prioritized by a specific rule, I found it very helpful to write a simple subclass of PriorityQueue which accepts a key-function. I can either use a (priority, object) as Charlie Martin suggests, or just implement _cmp_ for my object. """Add ``item`` to the queue if doesn't already exist.""" """Remove and return the smallest item from the queue.""" """Check if ``item`` exists in the queue.""" The data structure will be created in O(N). Items (list): An initial item list - it can be unsorted and Want to use the data structure for custom objects. Python's built-in objects, but you should implement those methods if you Important: the items of this data structure must be both comparable and Provides O(1) membership test, O(log N) insertion and O(log N) The result should be quite efficient for all operators: class PriorityQueueSet(object):Ĭombined priority queue and set data structure.Īcts like a priority queue, except that its items are guaranteed to be If (newDistance < destination.I ended up implementing a wrapper for heapq, adding a dict for maintaining the queue's elements unique. NewDistance = edge.getLength() + vertex.getDistance() However, they will be still removed from the PQ. Vertices which got no edges will finish with the shortest distance of infinity because it is not possible 'get to them' from the starting vertex. Compare the shortest distances with all adjacent vertices and if any distance is less than the shortest distance on the current vertex, update adjacent vertex shortest distance inside the PQ. Remove vertex from the PQ and explore all its edges. Start by inserting of all vertices (with its edges) from the graph inside the PQ. Initially, all vertices will have the shortest distance of infinity and the starting vertex will have the shortest distance 0. You should use priority queue where the vertex with the shortest distance from the starting vertex will get the highest priority.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |