Priority Queues are abstract data structures where each data/value in the queue has a certain priority. For example, In airlines, baggage with the title “Business” or “First-class” arrives earlier than the rest.
Priority Queue is an extension of the queue with the following properties.
1) An element with high priority is dequeued before an element with low priority.
2) If two elements have the same priority, they are served according to their order in the queue.
Various applications of Priority queue in Computer Science are:
Job Scheduling algorithms, CPU and Disk Scheduling, managing resources that are shared among different processes, etc.
Key differences between Priority Queue and Queue:
1)In Queue, the oldest element is dequeued first. While, in Priority Queue, element based on highest priority is dequeued.
2)When elements are popped out of a priority queue then result obtained in either sorted in Increasing order or in Decreasing Order. While, when elements are popped from a simple queue, a FIFO order of data is obtained in the result.
Below is simple implementation of priority queue.
12 1 14 7 14 12 7 1
Note that the time complexity of delete is O(n) in the above code.
By using heap datastructure to implement Priority Queues, Time complexity: Insert Operation: O(log(n)) Delete Operation: O(log(n))
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Priority Queue using Queue and Heapdict module in Python
- Difference between Circular Queue and Priority Queue
- Multithreaded Priority Queue in Python
- Heap and Priority Queue using heapq module in Python
- Why is Binary Heap Preferred over BST for Priority Queue?
- Priority Queue | Set 1 (Introduction)
- Applications of Priority Queue
- How to implement stack using priority queue or heap?
- Priority Queue using Linked List
- Priority Queue using doubly linked list
- Priority queue of pairs in C++ (Ordered by first)
- Double ended priority queue
- Implementation of Non-Preemptive Shortest Job First using Priority Queue
- Huffman Coding using Priority Queue
- Priority Queue of Vectors in C++ STL with Examples
- CPU Scheduling in Operating Systems using priority queue with gantt chart
- Priority queue of pairs in C++ with ordering by first and second element
- Find the K closest points to origin using Priority Queue
- Priority Queue using Binary Heap
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : simranjenny84