1 / 39

Priority Queues and Heaps

Priority Queues and Heaps. Priority Queues. We already know a ”standard” queue: Elements can only be added to the start of the queue Elements can only be removed from the end of the queue Order of removal is FIFO (first in, first out). Priority Queues.

Download Presentation

Priority Queues and Heaps

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. Priority Queues and Heaps

  2. Priority Queues • We already know a ”standard” queue: • Elements can only be added to the start of the queue • Elements can only be removed from the end of the queue • Order of removal is FIFO (first in, first out) DCS – SWC

  3. Priority Queues • In a Priority Queue, each element is assigned a priority as well • A priority is a numeric value – the lower the number, the higher the priority • Elements are removed in order of their priority – no longer FIFO • Can still add elements in any order DCS – SWC

  4. Priority Queues public interface PriorityQueue<T> { void add(T element); T remove(); T peek(); } DCS – SWC

  5. Priority Queues • The interface to a priority queue is almost the same as to a regular queue • However, elements in the queue must now be of the type Comparable (i.e. implement the interface) • The remove method always returns the element with highest priority (lowest numeric value…) DCS – SWC

  6. Priority Queues • A Priority Queue is an abstract data structure, which can be implemented in various ways • Linked list, removal will be O(n) • Binary search tree, removal will usually be O(log(n)), but not always • Heap, removal will always be O(log(n)) DCS – SWC

  7. Heaps • A Heap is a Binary Tree, but not a Binary Search Tree • In order for a Binary Tree to be a Heap, two conditions must be fulfilled: • A heap is almost complete; only nodes missing in bottom layer • All nodes store values which are at most as large as the values stored in any descendant DCS – SWC

  8. Heaps DCS – SWC

  9. Heaps • On a heap, only two operations are of interest: • Insert a new element • Remove the element with lowest value, i.e. the root element • However, both of these operations must preserve the heap property of the tree DCS – SWC

  10. Heaps - insertion 32 23 29 42 37 56 Find an empty slot in the tree DCS – SWC

  11. Heaps - insertion 32 23 29 42 If the value in the parent is larger than the new value, swap the parent and the new slot (repeat this) 37 56 DCS – SWC

  12. Heaps - insertion 32 23 29 32 37 56 42 Now insert the new value DCS – SWC

  13. Heaps - insertion • Since the heap is ”almost complete”, the number of layers in a tree with n nodes is at most (log(n) + 1) • Each swap operation can be done in constant time, so insertion of an element has the run-time complexity O(log(n)) DCS – SWC

  14. Heaps - removal Remove the root node (always smallest) 23 29 32 37 56 42 DCS – SWC

  15. Heaps - removal 29 32 Move the last element into the root position 37 56 42 DCS – SWC

  16. Heaps - removal If any child has a lower value, swap position with child with lowest value (repeat this) 42 29 32 37 56 DCS – SWC

  17. Heaps - removal If any child has a lower value, swap position with child with lowest value (repeat this) 29 42 32 37 56 DCS – SWC

  18. Heaps - removal The heap property has now been reestablished This is known as ”fixing the heap” 29 37 32 42 56 DCS – SWC

  19. Heaps - removal • Since the heap is ”almost complete”, the number of layers in a tree with n nodes is at most (log(n) + 1) • Each swap operation can be done in constant time, so deletion of an element has the run-time complexity O(log(n)) DCS – SWC

  20. Heaps - removal • The important point is that for a heap, we are guaranteed O(log(n)) run time for insertion and deletion • This cannot be guaranteed for a binary search tree • A binary search tree can ”degenerate” into a linked list; a heap cannot DCS – SWC

  21. Heaps - representation • Due to the regularity of a heap, it can efficiently be stored in an array • Root node is stored in position 1 (not 0) • A node in position i has its children stored in position 2i and (2i + 1) • A node in position i has its parent stored in position i/2 (integer division) • When running out of space, double the size DCS – SWC

  22. Heapsort • A heap can be used for a quite efficient way of sorting an array of n objects • Run-time complexity of O(nlog(n)) • Does not use extra space • Main steps • Turn the array into a heap • Repeatedly remove the root element (which has the smallest value) DCS – SWC

  23. Heapsort • In order to turn the array into a heap, we could just insert all the elements into a new heap • However, we can do this without using an extra heap! • We use the ”fix heap” procedure from the bottom in the tree and upwards DCS – SWC

  24. Heapsort • Why will this work…? • Remember that the ”fix heap” procedure takes two ”subheaps” as input, plus a root node with a ”wrong” value • If we work from the bottom and up, the input will always be like above DCS – SWC

  25. Heapsort Fix heap here Fix heaps here Fix heaps here Trivially a heap DCS – SWC

  26. Heapsort • Now the tree is a heap • Repeatedly remove the root from the heap, and fix the remaining heap • We ”remove” the root by placing it at the end of the array, beyond the last element in the remaining heap DCS – SWC

  27. Heapsort 23 29 32 37 56 42 23 29 32 37 56 42 DCS – SWC

  28. Heapsort 29 32 37 56 42 29 32 37 56 42 23 DCS – SWC

  29. Heapsort 29 37 32 42 56 29 37 32 42 56 23 DCS – SWC

  30. Heapsort 37 32 42 56 37 32 42 56 29 23 DCS – SWC

  31. Heapsort 32 37 56 42 32 37 56 42 29 23 DCS – SWC

  32. Heapsort 37 56 42 37 56 42 32 29 23 DCS – SWC

  33. Heapsort 37 42 56 37 42 56 32 29 23 DCS – SWC

  34. Heapsort 42 56 42 56 37 32 29 23 DCS – SWC

  35. Heapsort 42 56 42 56 37 32 29 23 DCS – SWC

  36. Heapsort 56 56 42 37 32 29 23 DCS – SWC

  37. Heapsort 56 56 42 37 32 29 23 DCS – SWC

  38. Heapsort 56 42 37 32 29 23 DCS – SWC

  39. Heapsort • One minor issue – numbers are sorted in wrong order • Could just reverse order, takes O(n) • Or use max-heap • Min-heap: All nodes store values at most as large as the values stored in any descendant • Max-heap: All nodes store values at least as large as the values stored in any descendant DCS – SWC

More Related