{"id":9920,"date":"2021-07-09T11:02:58","date_gmt":"2021-07-09T05:32:58","guid":{"rendered":"https:\/\/python-programs.com\/?p=9920"},"modified":"2021-11-22T18:50:10","modified_gmt":"2021-11-22T13:20:10","slug":"priority-queue-beginners-guide","status":"publish","type":"post","link":"https:\/\/python-programs.com\/priority-queue-beginners-guide\/","title":{"rendered":"Priority Queue: A Beginner\u2019s Guide"},"content":{"rendered":"
Prerequisites<\/strong><\/p>\n To learn about Priority Queue, you must know:<\/p>\n Before you go ahead and read this tutorial, I highly recommend you to read the previous tutorial on\u00a0Queues\u00a0and\u00a0Circular Queues\u00a0as it will give you a better foundation and help you grasp the the content here.<\/p>\n What would you do if you wanted to track the least played songs in your playlist? The easiest solution would be to sort the list but that is time-consuming and wasteful. You only have to keep track of the song with the least hits. A min heap or priority queue helps you do this.<\/p>\n Priority Queues, also known as heap queues, are abstract data structures. Heaps are binary trees where every parent node has a value less than or equal to any of its children. In other words, this type of queue keeps track of the minimum value. Thus it helps retrieve the minimum value at all times. For this reason, it is also referred to as min heap. Thus, position 0 holds the smallest\/minimum value. There is also a max heap whose operation is quite similar.<\/p>\n Note:<\/b>\u00a0heap queues or priority queues don\u2019t sort lists in ascending order. It just keeps the smallest element in its 0th position. The rest of the elements may or may not be sorted.<\/p>\n To use priority queue, you will have to import the\u00a0heapq\u00a0<\/i>library. This is done as follows:<\/p>\n The following heap commands can be performed once the\u00a0heapq\u00a0<\/i>module is imported:<\/p>\n Note:<\/strong>\u00a0Only the first element is in its correct sorted position.<\/p>\n Try adding a negative number and observe what happens.<\/p>\n If you try the above command with a number smaller than the min value of heap, you will notice that the same element gets popped. For instance:<\/p>\n heapq.heappushpop(h,0) #returns 0 print(h) #prints [1, 2, 2, 4, 5, 6, 3, 8, 7]<\/p>\n The above-mentioned commands are the main ones you will use when dealing with heaps but there are also other general commands like\u00a0merge(), nlargest()\u00a0<\/i>and\u00a0nsmallest().\u00a0<\/i>You can explore these on your own!<\/p>\n Priority Queues are widely used in different fields such as Artificial Intelligence, Statistics, Operating systems and in graphs.<\/p>\n Conclusion<\/strong><\/p>\n Try solving the music player problem discussed in the introduction. You will need to\u00a0heapify<\/em>\u00a0a list of tuples where each tuple should look like\u00a0(number of hits, songid, name of the song)<\/i>. The\u00a0heapify<\/em>\u00a0command will track the min according to the first element of the tuple which is why the first element of the tuple is\u00a0the number of hits<\/em>. Post your answers below. That\u2019s it for this tutorial. Happy Pythoning!<\/p>\n","protected":false},"excerpt":{"rendered":" Prerequisites To learn about Priority Queue, you must know: Python 3 Linear Queue Basic Python data structure concepts –\u00a0lists, tuples What is a priority queue? Before you go ahead and read this tutorial, I highly recommend you to read the previous tutorial on\u00a0Queues\u00a0and\u00a0Circular Queues\u00a0as it will give you a better foundation and help you grasp …<\/p>\n\n
What is a priority queue?<\/h2>\n
How To Implement Priority Queue<\/b><\/h4>\n
import heapq<\/pre>\n
\n
h = [5,2,6,8,0,1,2,4]\r\nheapq.heapify(h) #returns [0, 2, 1, 4, 5, 6, 2, 8]<\/pre>\n
\n
heapq.heappush(h,7)\r\nprint(h) #[0, 2, 1, 4, 5, 6, 2, 8, 7]<\/pre>\n
\n
heapq.heappop(h) #returns 0<\/pre>\n
\n
heapq.heappushpop(h,3) #returns 0\r\nprint(h) #prints [1, 2, 2, 4, 5, 6, 3, 8, 7]<\/pre>\n
\n
heapq.heapreplace(h,0) #returns 1\r\nprint(h) #prints [0, 2, 2, 4, 5, 6, 3, 8, 7]<\/pre>\n
Applications<\/b><\/h3>\n