1 / 50

Priority Queue In Data Structure | Priority Queue Explained | Simplilearn

This presentation on Priority Queue in Data Structure will acquaint you with a clear understanding of priority queue implementation. In this video, you will understand the Heap implementation of priority queue using the C programming language. Finally, we will cover the applications of a priority queue to understand its importance. So, let's get started!<br><br>The topics covered in this Circular Queue Explained video are:<br>1. Introduction<br>2. Introduction to Priority Queue<br>3. Representation of Priority Queue<br>4. Different Implementation Strategies for Priority Queue<br>5. What Is Heap?<br>

Simplilearn
Télécharger la présentation

Priority Queue In Data Structure | Priority Queue Explained | Simplilearn

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What’s in It For You? • Introduction to Priority Queue • Representation of Priority Queue • Different Implementation Strategies for Priority Queue • What is Heap? • Types of Priority Queue • Operations in Priority Queue (Heap Implementation) • Coding Implementation of Priority Queue • Applications of Priority Queue

  2. Introduction to Priority Queue

  3. Click here to watch the video

  4. Introduction to Priority Queue Hospital Emergency Queue The patients will be treated according to their medical condition! i.e., person in pain – High Priority

  5. Introduction to Priority Queue A priority queue is abstract data-type which behaves similar to the linear queue except that each element has priority. Priority: 1 2 3 4 5 5 7 16 19 32 FRONT (Deletion) REAR (Insertion) 0 1 2 3 4 The priority of elements in the priority queue will determine the order of removal of data-elements.

  6. Introduction to Priority Queue A priority queue only supports comparable elements.

  7. Introduction to Priority Queue For example, we will insert elements 7, 2, 45, 23, 12 respectively in a priority queue. REAR Element with lowest value, will have highest priority! Comparison between two elements 2 7 FRONT

  8. Introduction to Priority Queue For example, Priority queue has inserted with elements 7, 2, 45, 23, 12 respectively. REAR 32 Element Swapping 45 New Insertion! 7 2 FRONT

  9. Introduction to Priority Queue For example, Priority queue has inserted with elements 7, 2, 45, 23, 12 respectively. REAR 12 New Insertion! 45 32 Element Swapping 7 2 FRONT

  10. Introduction to Priority Queue For example, Priority queue has inserted with elements 7, 2, 45, 23, 12 respectively. REAR Element with Lowest Priority 45 32 12 7 Element with Highest Priority 2 FRONT

  11. Characteristics of Priority Queue • Every element of priority queue will have some priority assigned to it. • Element with higher priority will be removed first whereas element with lowest priority at last. • If two elements have same priority, they will be removed according to the FIFO principle.

  12. Representation of Priority Queue

  13. Representation of Priority Queue Let’s understand how we can represent a priority queue with the help of an example. Head Tail START 400 500 200 Now we have to insert new node containing value 2. Since the value 2 has more priority insertion at head node will be ideal. 250

  14. Head Tail START 400 500 200 250 Now what if we have to insert new node containing value 45. For inserting this node, compiler must be performing comparison of new node with each node of a queue. 144

  15. START 250 200 400 500 144

  16. START 250 200 400 500 144

  17. For this insertion there will be N comparisons to reach the appropriate index for insertion. Due to that the complexity for insertion in Priority Linked Queue will become 0(N). Head START 250 200 400 500 For the deletion of elements complexity would be O(1) as insertion is done according to priority. Tail 144

  18. Why Linked List Implementation of Priority Queue is Not a Good Strategy? • Drawbacks of the Linked List Implementation of Priority Queue: • As we have to insert elements at the head node to maintain priority for Deletion, the time complexity for insertion becomes O(N). • The time complexity for Peek() operation also becomes O(N). • Considering time and memory management as basic aspect of data structures, this implementation strategy is not valid for big software applications.

  19. Different Implementation Strategies for Priority Queue

  20. Different Implementation Strategies for Priority Queue

  21. What is Heap?

  22. What is Heap? • A heap is a tree-like data structure that forms complete binary tree. • If A is a parent node of B, then A is ordered with respect to node B for all nodes A and B in a Heap. Any tree following this property is considered as Heap. • Value of Parent node >= Value of Child node • Value of Parent node <= Value of Child Node

  23. Max Heap The Value of Parent Node is greater than child node. 50 20 40 11 5 35 22

  24. Min Heap The Value of Parent Node is smaller than child node. 10 60 70 80 90 97 99

  25. Is this a Valid Heap? 8 6 6 5 4 5 4 3 6 2 3 This is not Valid Min Heap! Child node > Parent node

  26. Is this a Valid Heap? 0 6 2 3 5 7 6 6 11 7 9 8 7 6 This is a Valid Heap!

  27. Is this a Valid Heap? 50 20 40 11 35 22 Every Heap Must be valid tree. And this is not a tree as it forms a closed cycle!

  28. Is this a Valid Heap? 3 3 3 This is a Valid Heap!

  29. Is this a Valid Heap? 5 6 5 This is not a Valid Heap! But, this can be converted into a valid Heap by moving nodes!

  30. Conversion into a Valid Heap? 5 6 Conversion 6 5 5 5 This newly created structure is a valid Heap!

  31. Types of Priority Queue

  32. Types of Priority Queue There are two types of Priority Queue: • Min Priority Queue: Queue in which element with least value will have higher priority. • Max Priority Queue: Queue in which element with highest value will have higher priority. • Min priority queue can be implemented using Min-Heap where max priority queue can be implemented using Max-Heap.

  33. Operations in Priority Queue

  34. Operations in Priority Queue • The common operations that we can perform on a priority queue are insertion, deletion and peek. • While Performing these operations, the order of data elements changes constantly, which will be managed by performing new operation called as Heapify.

  35. Insertion in Priority Queue (Max Heap) 50 20 40 5 11 35 43 Due to addition of new element, the structure won’t follow the priority queue invariant. We want to insert new node consisting element 43.

  36. Heapify operation The Heapify operation solves the previous problem. It rearranges all data elements by carrying out their comparison with each other. 50 20 40 Reshuffling data elements in order! 5 11 35 43

  37. Deletion in Priority Queue (Max Heap) 50 20 43 5 11 35 40 The deletion of element will happen from root node as it has higher priority. After that, Using Heapify the Heap property will be maintained.

  38. Heapify operation 50 20 43 5 11 35 40 Data Element is successfully Removed!

  39. Peek in Priority Queue (Max Heap) 50 20 43 5 11 35 40 Peek operation will return the element present at the root node without removing it.

  40. Implementation of Priority Queue

  41. Instructions to Find Positions of Nodes While implementing heap structure, it is crucial to remember positions like Parent node and its Left and right child. For every ith element these nodes will be calculated using formulas given below: 1 • Parent Node: A[(i-1)/2] • Left Child: A[(2*i) + 1] • Right Child: A[(2*i) + 2] 1 2 Heap stored in Array 2 3 0 1 2 3 4 5 6 7 4 6 7 5 0 1 2 3 4 5 6 For 1st index: Left child = 2*1 + 1 = 3 Right Child = 2*1 + 2 = 4 Let’s take an example of 0th index: Left child = 2*0 + 1 = 1 Right Child = 2*0 + 2 = 2 For 2nd index: Left child = 2*2 + 1 = 5 Right Child = 2*2 + 2 = 6 3 4 5 6

  42. Implementation of Priority Queue (Max Heap) Let’s now formulate the code for implementation of Priority Queue using Heap data structure!

  43. Applications of Priority Queue

  44. Digital Mapping Services in Google Maps The Dijkstra’s Shortest Path algorithm is used to find minimum distance between two locations to find the closest path. The Dijkstra’s shortest path algorithm uses priority queue to find out the optimal path.

  45. Digital Mapping Services in Google Maps All possible paths will be provided to Dijkstra’s algorithm to search for ideal path. Nodes are representing distance between your location and your destination location. Stores paths in Min Heap 10 20 30 27 40 22 35

  46. Data Compression in Formats like GZIP, WINZIP Multiple Files Encoded Zip File Huffman Encoding Algorithm uses priority queue to map data from different files. Huffman Encoding Algorithm

  47. Example of Data Compression A Text file contains following data: AAABCAABCABAA B C Huffman Encoding Algorithm Huffman encoding algorithm generates codes for each letter depending on its frequency.

  48. Example of Data Compression A B C Text file contains following data: AAABCAABCABAA Letter A will have less code after that, comes B and C respectively. Those generated codes will have less size in compressed format.

More Related