![]() The priority queue supports only comparable elements, which means that the elements are either arranged in an ascending or descending order.įor example, suppose we have some values like 1, 3, 4, 8, 14, 22 inserted in a priority queue with an ordering imposed on the values is from least to the greatest. The priority of the elements in a priority queue will determine the order in which elements are removed from the priority queue. It’s trivial to show that insert, deleting the element with the highest priority, and changing the key of a component take logarithmic time.A priority queue is an abstract data type that behaves similarly to the normal queue except that each element has some priority, i.e., the element with the highest priority would come first in a priority queue. Since we want to associate each key with an index in order to have fast access. Note that even in Python, we have to know the maximum number of elements in the queue to support indexing. key is None : raise Inde圎rror ( 'index i is not in the IndexedMinPQ' ) assert type ( i ) is int assert key > self. N : raise Inde圎rror ( 'index i is not in the range' ) if self. key is None : raise Inde圎rror ( 'index i is not in the IndexedMinPQ' ) assert type ( i ) is int assert key self. ![]() ![]() total ) def _swim ( self, i ): parent_i = i // 2 while parent_i > 0 : key = self. key is not None : raise Inde圎rror ( 'index is already in the IndexedMinPQ.' ) self. N : raise Inde圎rror ( 'index is out of the range of IndexedMinPQ.' ) if self. total = 0 def insert ( self, i, key ): assert type ( i ) is int if i >= self. We also have to modify swim and sink methods to support changing the key of an element inside the queue.Ĭlass IndexedMinPQ : def _init_ ( self, N ): self. The keys (keys), the heap representation of the priority queue (pq), and an array containing the index of each key in the heap representation (qp). In an Indexed priority queue, we store the information in three arrays. However, with a minor change, we can add indexing to the priority queue. While priority queues are useful in many applications, they do not support changing the priority of an element in logarithmic time. _sink ( 1 ) return out raise Exception ( 'PQ is empty' ) _swim ( len ( arr ) - 1 ) def remove (): if not self. arr idx = p else : break def isEmpty (): return len ( self. arr idx = child else : break def _swim ( idx ): assert type ( idx ) is int assert idx > 0 and idx 0 : p = idx // 2 if self. arr : child = child + 1 if arr > arr : self. N = N def _sink ( self, idx ): assert type ( idx ) is int assert idx > 0 and idx self. ![]() The swim method ensures that the node does not have a higher priority than its parent, and the sink method makes sure that the node’s children have lower priority.Ĭlass PQ : def _init_ ( self, N ): self. Heaps use the swim and the sink methods to ensure that the node with the highest priority is always on top of the tree. To have efficient access to the node’s children or the node’s parent, an array is usually used to represent the tree where if the key of node i is pq, the children are pq and pq. A heap is a tree-based data structure where for node C, C’s key is greater than the key of C’s parent (for MinPQ). Priority queues are usually implemented with heaps. But what if in the middle of the program, the priority of an object changes? Indexed Priority Queue gives us the ability to change the priority of an element without having to go through all the elements.īefore going through the details on how to implement an Indexed Priority Queue, let’s see how we can implement a priority queue. However, instead of removing the ‘First In’ element, the priority queue removes the element with the highest ‘priority’. Like a regular queue, the priority queue is an abstract data structure that supports adding and removing elements. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |