Indexed Priority Queue with Implementation
Priority queue is a data structure in which data is stored on basis of its priority. In an Indexed Priority Queue, data is stored just like standard priority queue and along with this, the value of a data can be updated using its key. It is called “indexed” because a hash map can be used to store the index in container using the key of key-value pair input as the key of hash map. This comes handy while implementing Dijkstra’s Algorithm using min-heap. It can also be used in any other program where it is required to put key-value pairs in priority queue and along with it you need to update value of keys with push or pop function .
Operations in an Indexed Priority Queue: The operations which can be performed in Indexed Priority Queue are:
- push: Adding key-value pair in Indexed Priority Queue according to its priority. Implementation: To do this, add the key value pair in container and then heapify on basis of value in key-value pair. Time Complexity: O( log(n) )
- pop: Removing the highest priority key-value pair. Implementation: Remove top of heap and then heapify rest of the container. Time Complexity: O( log(n))
- top: Return Key-Value pair to user. Implementation: Return Key-Value pair which is at top of the heap. Time Complexity: O(1)
- size: Return the number of key-value pairs in the Indexed Priority Queue. Implementation: Keep a track if number of elements in queue in the class and return the variable when size() function is called. Time Complexity: O(1)
- empty: Return true when Indexed Priority Queue is empty. Implementation: Return true when number of elements variable is equal to zero. Time Complexity: O(1)
- changeAtKey: This function differentiate Indexed Priority Queue from standard priority queue. It takes two arguments from user, first is key and second is new value, and it update old value associated with the key to new value provided and update its position according to priority of new value. Implementation: Keep a hash-map whose key is key in key-value pair and it points to index of the key in container. When the function is called update the value at required index and position current element according to its priority and finally change index value in hash map. Time Complexity: O( log(n) )
Implementation of Indexed Priority Queue:
There are four parameters to be passed while defining an instance of Indexed Priority Queue(two mandatories and two optional) which are:
- Data type of key: This is the first parameter in the definition and it should be data type that can be hashed in hash map or user have pass his own hash function as fourth parameter. To learn more about hash function in hash map see this article.
- Data type of value: This is second parameter in definition.
- Comparator: This is third and optional parameter. By default Indexed Priority Queue will be implemented using max heap to change that user have to pass different comparator with its parameter (i.e. parameter of comparator) as the data type of value.
- Hash function: This is fourth parameter and required only when user pass custom data type (like a class) for key, then user have to pass his own hash function.
Below is the implementation of Indexed Priority Queue: