1 / 83

HEAPS & PRIORITY QUEUES

HEAPS & PRIORITY QUEUES. Array and Tree implementations. Priority queue. A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element is the first one removed.

walter
Télécharger la présentation

HEAPS & 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 & PRIORITY QUEUES Array and Tree implementations

  2. Priority queue • A stack is first in, last out • A queue is first in, first out • A priority queue is least-first-out • The “smallest” element is the first one removed

  3. The definition of “smallest” is up to the programmer (for example, • you might define it by implementing Comparator or Comparable)

  4. Priority queue • If there are several “smallest” elements, • the implementer must decide which to remove first • Remove any “smallest” element (don’t care which) • Remove the first one added

  5. A priority queue is an: • ADT where items are removed based on their priority • (highest-to-lowest), regardless of the order they ARE inserted in the structure

  6. A priority queue ADT • PriorityQueue(): Methods • void add(Comparable o): inserts o into the priority queue • Comparable removeLeast(): removes and returns the least element • Comparable getLeast(): returns (but does not remove) the least element • booleanisEmpty():returns true iff empty • int size(): returns the number of elements • void clear():discards all elements

  7. Evaluating implementations • When we choose a data structure, • it is important to look at usage patterns • If we load an array once and do thousands of searches on it, • we want to make searching fast— • so we would probably sort the array

  8. When we choose a data structure, • it is important to look at usage patterns • If we load a huge array and expect to do only a few searches, • we probably don’t want to spend time sorting the array

  9. Evaluating implementations • For almost all uses of a queue (including a priority queue), • we eventually remove everything that we add

  10. TIMING • Hence, • when we analyze a priority queue, • neither “add” nor “remove” is more important— • we need to look at the timing for (“add + remove”)

  11. Array implementations • A priority queue could be implemented as an unsorted array (with a count of elements) • Adding an element would take O(1) time (why?) • Removing an element would take O(n) time (why?) • Hence, (adding and removing) an element takes O(n) time • This is an inefficient representation – a BigO(N)

  12. Array implementations • A priority queue could be implemented as a sortedarray (again, with a counter of elements) • Adding an element would take O(n) time (why?) • Removing an element would take O(1) time (why?) • So adding and removing) an element takes • O(n) time • Again, this is very inefficient

  13. Linked list implementations A priority queue could be implemented as an • unsorted linked list • Adding an element would take O(1)time (why?) • Removing an element would take (N) time (why?) • Again, inefficient!

  14. SORTED LINKED LIST • A priority queue could be implemented as a sorted linked list • Adding an element would take O(n) time (why?) • Removing an element would take O(1)time (why?) • Again, an inefficient algorithm

  15. Binary tree implementations • A priority queue could be represented as a • balanced binary search tree • Insertion and removal could destroy the balance • We would need an algorithm to rebalance the binary tree • Good rebalancing algorithms require O(log n) time, but are complicated

  16. Heap Implementaton • The concepts of heaps is used to refer to memory in a computer, • E.g. the part of the memory that provides dynamic memory allocation ( get it as you need it) • “Heap” is also used as a name for a special kind of binary tree

  17. Heap is a complete binary tree • A heap is a complete binary tree with values stored in its nodes such that no child has a value bigger than the value of its parent – (though it can be equal)

  18. Therefore we would like to find a better algorithm. We want to: • Insert and Remove with a Big O of one

  19. HEAPS & Priority Queues • A binary tree represented as a heap facilitates the operation to find the maximum element: “it is always in the root of the tree “ • Because finding the node with the maximum value in a heap is a BigO(I) • A HEAP is the most efficient structure for implementing priority queues

  20. Heaps A heap is a certain kind of complete binary tree. Root When a complete binary tree is built, its first node must be the root.

  21. Heaps Complete binary tree. Right child Left child The Second and third Nodes are always the left and right child of the root.

  22. Heaps Complete binary tree. The next nodes always fill the next level from left-to-right.

  23. Heaps Complete binary tree.

  24. Heaps A heap is a certain kind of complete binary tree. 45 35 23 27 21 22 4 19 Each node in a heap contains a key that can be compared to other nodes' keys.

  25. A node has the heap property if it is Equal to or greater than as its children OR if it is smaller than or equal to its children (since smaller numbers represent higher priorities) 3 12 8 12 8 3 Priority queue: Yellow node has the heap property - less than its children Heap: Yellow node has the heap property To implement a heap as a priority queue

  26. Heaps A heap is a certain kind of complete binary tree. This example is a max heap. 45 35 23 27 21 22 4 19 The "heap property" requires that each node's key is >= to the keys of its children

  27. Priority Queues • A heap can easily implement a priority queue • but what happens with we remove the element of highest priority? • How do we re-arrange the tree?

  28. Removing from a heap • There are two things we need to consider when rearranging: • We want to end up with a heap which means that • The tree has to be complete – • ALL HEAPS ARE COMPLETE TREES!

  29. To remove an element at top of tree: • Remove the element at location 0 • Move the element in the lastIndexto location 0, decrement lastIndex (lastIndex is at the end of the heap) • Reheap the new root node (the one now at location 0) • This is called down-heap bubbling or percolating down • Down-heap bubblingrequires O(log n) time to remove an element

  30. Removing the Top of a Heap(max heap) • Move the last node onto the root. 45 42 23 35 21 22 4 19 27

  31. Removing the Top of a Heap(max heap) • Move the value in the last node into the root. 27 42 23 35 21 22 4 19

  32. Removing the Top of a Heap • Move the last node onto the root. • Push the out-of-place node downward, • swapping with its larger child • until the new node reaches an acceptable location. 27 42 23 35 21 22 4 19

  33. Removing the Top of a Heap • Move the last node onto the root. • Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 42 27 23 35 21 22 4 19

  34. Removing the Top of a Heap • Move the last node onto the root. • Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. • Note that we discard half of the tree with each move downward 42 35 23 27 21 22 4 19

  35. reheapificationdownward. • We stop when: • The children all have keys <= to the out-of-place node, or • The node reaches the leaf. • The process of pushing the node downward is called: reheapificationdownward. 42 35 23 27 21 22 4 19

  36. Priority Queues • Another Example : • How to delete a node from a Priority Queue • Since it is queue - first in first out, • we remove the first one The Root

  37. 23 12 16 7 9 13 5 3 2 1 An Example of a MAX HeapThe root has largest value

  38. 12 16 7 9 13 5 3 2 1 Deleting a node from the heap • We use a heap to implement a priority queue • the "next" element ( the root ) in the queue was removed (pop operation). • We end up with a root with no value Root has no value

  39. Deleting a node from the heap The obvious question is: which node can we use to replace this one? 12 16 7 9 13 5 3 2 1

  40. Deleting a node from the heap 12 16 7 9 13 5 If we want this tree to stay complete the rightmost element in the bottommost level is the obvious choice 3 2 1

  41. Deleting a node from the heap We now have a problem: We havea complete tree but this is not a heap! WHY??? This can be solved byapplying systematicswap of nodes. 2 12 16 7 9 13 5 3 1

  42. Deleting a node from the heap 2 12 16 7 9 13 5 We systematically swap the root with the larger of the children nodes until no more swaps can be done 3 1

  43. Deleting a node from the heap 16 12 2 7 9 13 5 3 1

  44. Deleting a node from the heap 16 12 13 7 9 2 5 3 1

  45. Deleting a node from the heap 16 12 13 7 9 2 5 3 1 The tree has restored its heap property!

  46. Insert operation • Another example - the heap below • Recall that while delete causes pairwise swaps from the root to the bottom, • insert causes pairwise swaps from the bottom to the top. • Suppose that we want to insert an element with value 20 22 17 12 11 3 13 5 4 1 6

  47. insert (20) 22 12 17 11 3 13 5 4 1 20 6

  48. insert (20) 22 12 17 11 20 13 5 4 1 3 6

  49. insert (20) 22 20 17 12 11 13 5 4 1 3 6

  50. insert (20) 22 20 17 11 12 13 5 4 1 3 6

More Related