{"id":26967,"date":"2017-12-28T21:48:50","date_gmt":"2017-12-28T16:18:50","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26967"},"modified":"2017-12-28T21:48:50","modified_gmt":"2017-12-28T16:18:50","slug":"queue-data-structure-priority-queue","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/queue-data-structure-priority-queue\/","title":{"rendered":"Queue-Data Structure-Priority Queue"},"content":{"rendered":"<p>Priority Queue is an extension of <a href=\"http:\/\/quiz.geeksforgeeks.org\/queue-set-1introduction-and-array-implementation\/\" target=\"_blank\" rel=\"noopener noreferrer\">queue <\/a>with following properties.<br \/>\n1) Every item has a priority associated with it.<br \/>\n2) An element with high priority is dequeued before an element with low priority.<br \/>\n3) If two elements have the same priority, they are served according to their order in the queue.<\/p>\n<p>A typical priority queue supports following operations.<br \/>\n<strong>insert(item, priority): <\/strong>Inserts an item with given priority.<br \/>\n<strong>getHighestPriority():<\/strong> Returns the highest priority item.<br \/>\n<strong>deleteHighestPriority(): <\/strong>Removes the highest priority item.<\/p>\n<p><strong>How to implement priority queue?<\/strong><\/p>\n<p><strong>Using Array:<\/strong> A simple implementation is to use array of following structure.<\/p>\n<pre>struct item {\r\n   int item;\r\n   int priority;\r\n}<\/pre>\n<p>insert() operation can be implemented by adding an item at end of array in O(1) time.<\/p>\n<p>getHighestPriority() operation can be implemented by linearly searching the highest priority item in array. This operation takes O(n) time.<\/p>\n<p>deleteHighestPriority() operation can be implemented by first linearly searching an item, then removing the item by moving all subsequent items one position back.<\/p>\n<p>We can also use Linked List, time complexity of all operations with linked list remains same as array. The advantage with linked list is deleteHighestPriority() can be more efficient as we don\u2019t have to move items.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Using Heaps:<\/strong><br \/>\nHeap is generally preferred for priority queue implementation because heaps provide better performance compared arrays or linked list. In a Binary Heap, getHighestPriority() can be implemented in O(1) time, insert() can be implemented in O(Logn) time and deleteHighestPriority() can also be implemented in O(Logn) time.<br \/>\nWith <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fibonacci_heap\" target=\"_blank\" rel=\"noopener\">Fibonacci heap<\/a>, insert() and getHighestPriority() can be implemented in O(1) amortized time and deleteHighestPriority() can be implemented in O(Logn) amortized time<\/p>\n<p><strong>Applications of Priority Queue:<\/strong><br \/>\n1) CPU Scheduling<br \/>\n2) Graph algorithms like <a href=\"http:\/\/www.geeksforgeeks.org\/greedy-algorithms-set-7-dijkstras-algorithm-for-adjacency-list-representation\/\" target=\"_blank\" rel=\"noopener noreferrer\">Dijkstra\u2019s shortest path algorithm<\/a>, <a href=\"http:\/\/www.geeksforgeeks.org\/greedy-algorithms-set-5-prims-mst-for-adjacency-list-representation\/\" target=\"_blank\" rel=\"noopener noreferrer\">Prim\u2019s Minimum Spanning Tree<\/a>, etc<br \/>\n3) All <a href=\"http:\/\/www.geeksforgeeks.org\/applications-of-queue-data-structure\/\" target=\"_blank\" rel=\"noopener noreferrer\">queue applications<\/a> where priority is involved.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Priority Queue &#8211; Priority Queue is an extension of queue with following properties. Every item has a priority associated with it.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73012,80124],"tags":[83780,79664,75618,83784,70915,83781,70907,83783,83782,83785,83786,80122,80120,80121],"class_list":["post-26967","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-queue","tag-application-of-priority-queue-in-data-structure","tag-application-of-queue-in-data-structure","tag-circular-queue-in-data-structure","tag-max-priority-queue","tag-priority-queue-example","tag-priority-queue-geeksforgeeks","tag-priority-queue-in-c","tag-priority-queue-tutorialspoint","tag-priority-queue-using-linked-list","tag-queue-data-structure-in-c","tag-queue-data-structure-java","tag-queue-in-data-structure-pdf","tag-queue-in-data-structure-using-c","tag-types-of-queue-in-data-structure"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26967"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26967\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}