1 / 84

Heaps and Priority Queues

Heaps and Priority Queues. Starring: Min Heap Co-Starring: Max Heap. Purpose: In this lecture we will discuss the Priority Queue ADT and a Heap implementation of a priority Queue. Resources: Java Methods AB Data Structures Chapter 7 p.175

Télécharger la présentation

Heaps and Priority Queues

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. Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap

  2. Purpose: In this lecture we will discuss the Priority Queue ADT and a Heap implementation of a priority Queue

  3. Resources: Java Methods AB Data Structures Chapter 7 p.175 Java Essentials Study Guide Chapter 17 .7/8 p.330 & Chapter 20.1 p.398 & Chapter 20A.4/5 p.418 Barrons Chapter 9 p.305 & Chapter 12 p.414 AP Java Text Chapter 19.7 p.843 & Chapter 21.4 p.950

  4. Handouts: 1. Priority Queue Interface CODE: PriorityQueue.java PriorityString.java PriorityQueueExample.java Jobs.java

  5. Intro: So far we have seen data structures that maintain and process data in the following ways: Store data in order based on a Key Store data in order based on the “value” of the object Store data in order of arrival Store data in reverse order of its arrival Store data in random order

  6. There are times when we need to maintain and process data based on their relative importance or Priority

  7. For example, people waiting to but concert tickets. The priority of the order they are allowed to purchase tickets is based on the number on a wrist band that are distributed. The number on the wristband determines when they get served

  8. If you go to an amusement park, you can select a predetermined time to get on an attraction. When the time comes, you get into a Priority Queue which gets processed first

  9. In a hospital emergency room, a triage system identifies the urgent patients all of whom are seem before the normal patients regardless of their order of arrival

  10. In operating systems, jobs are processed based on a specific priority so that a recently submitted job with a high priority will process before lower priority jobs even though these jobs were in the queue longer

  11. What we Will Cover in This Lecture: Priority Queue ADT Priority Queue AP Interface Priority Queue Implementations Heap Implementation The AP AB Requirements

  12. Priority Queue ADT PQ’s Support functionality that maintains a large set of elements that are ranked in some way and to have quick access to the HIGHEST ranked element

  13. Priority Queue ADT PQ is a collection of the same type object values These object values contain data and a priority These objects are ordered in a way that allows the HIGHEST priority item to be removed first

  14. Example of a priority queue where items entered have a priority of 1,2 or 3: EMPTY PQ (ADD) A (2) B (2) A (2) C (2) B (2) A (2) (REMOVE MIN) C (2) B(2) (ADD) D (3) C (2) B(2) (REMOVE MIN) D (3) C (2) (ADD) E (1) D (3) C (2) F (1) E (1) D (3) C (2) (REMOVE MIN) F (1) D (3) C (2) D (3) C (2) D (3) EMPTY PQ

  15. If several items maintain the same priority then the first one in the queue is processed first

  16. Priority Queues Must Provide for the adding and removing of elements in a way that ensures the HIGHEST Priority Item is removed First Here is the Priority Queue AP Interface :

  17. public interface PriorityQueue { boolean isEmpty(); void add(Object x); Object removeMin(); Object peekMin(); }

  18. The priority of each item in the queue is established using the Comparable interface Every time the removeMin or peekMin method is executed the SMALLEST element with respect to the compareTo method is returned

  19. Priority Queue Implementations You are not required to know one specific PQ implementation but you need to understand their general principles that require any Data Structure to allow for: Quick insertion of PQ elements Quick retrieval of an element with the top priority Potential PQ implementations include:

  20. Linked List where new nodes are inserted at the front of the list O(1) and the removal searches the list for the highest priority node O(n)

  21. Linked List where nodes are inserted in priority order with the smallest elements in the front nodes O(n) and removal simply unlinks the front node O(1)

  22. Array with nodes inserted at the end of the List O(1) and removals search for the highest priority element O(n)

  23. A sorted Array where the highest priority elements (smallest values) are added at the end of the List O(n) and removal takes off he last element in the array O(1)

  24. TreeSet where adding an element is O(Log N) and Removal is O(Log N)

  25. Heap Implementation The most common implementation of a PQ is a Binary Heap With a PQ we use a Minimum Heap With these type of heaps the VALUE of every node is LESS THAN or EQUAL to that of its children

  26. A HEAP is viewed as a Complete Binary Tree where there are no GAPS at any level. The Last level may have some leaves missing on the right, but every node except the last must have a node after it. The nodes are in the leftmost positions

  27. Each addition to the heap MUST maintain the property where the parent node is at least as small as its children

  28. Example: 1 6 4 8 7 9 10 12 20

  29. The lower the number, the higher the priority The element with the HIGHEST priority is kept at the root so removing an element requires removal of the root and then restructuring of the heap

  30. ReHeaping is a O(LogN) operation Inserting into the heap also requires Reheaping and is O(LogN) ALL elements added to a PQ must implement the Comparable interface and be of the same class

  31. Inserting and Removing elements to the Heap

  32. create a heap: 25 57 48 37 12 92 86 33 25

  33. create a heap: 25 57 48 37 12 92 86 33 25 57

  34. create a heap: 25 57 48 37 12 92 86 33 25 48 57

  35. Create a heap: 25 57 48 37 12 92 86 33 25 48 37 57

  36. Create a heap: 25 57 48 37 12 92 86 33 12 48 25 57 37

  37. create heap: 25 57 48 37 12 92 86 33 12 25 48 57 92 37

  38. create heap: 25 57 48 37 12 9286 33 12 25 48 57 92 37 86

  39. create heap: 25 57 48 37 12 928633 12 25 48 33 92 37 86 57

  40. Given the following Tree: 1 6 4 8 7 9 10 12 20

  41. Match this heap with an indices of an Array: Heap PriorityArray Element (IGNOREZERO) 0 1 1 6 2 4 3 8 4 7 5 9 6 10 7 12 8 20 9

  42. Array Index: 1 2 3 4 5 6 7 8 9

  43. Lets Insert an element with a priority of 5 We insert the new element in the next open slot ( index 10 or left child of element with Priority of 7)

  44. We then Compare This element against its Parent 1 6 4 8 7 9 10 12 20 5

  45. If the Parent is Greater, Then SWAP the elements 1 6 4 8 5 9 10 12 20 7

  46. We AGAIN Compare This element against its Parent 1 6 4 8 5 9 10 12 20 7

  47. If the Parent is Greater, Then SWAP the elements 1 5 4 8 6 9 10 12 20 7

  48. Lets Insert an element with a priority of 5 We insert the new element in the next open slot ( index 10 or left child of element with Priority of 7)

  49. Lets Insert an element with a priority of 5 We insert the new element in the next open slot ( index 10 or left child of element with Priority of 7)

  50. Again We Compare This Element against its New Parent But since the Parent is SMALLER than the this element, we are done RE-HEAPING The order of the Heap is maintained as all children are LARGER than their parent

More Related