heaps and priority queues heaps complete binary tree
play

Heaps and Priority Queues Heaps Complete Binary Tree Complete - PowerPoint PPT Presentation

Heaps and Priority Queues Heaps Complete Binary Tree Complete binary tree All layers (except lowest) are full. Lowest layer is filled from left to right. 3 / 28 Complete Binary Tree Array Representation 1-Based Array 0-Based Array


  1. Heaps and Priority Queues

  2. Heaps

  3. Complete Binary Tree Complete binary tree ◮ All layers (except lowest) are full. ◮ Lowest layer is filled from left to right. 3 / 28

  4. Complete Binary Tree – Array Representation 1-Based Array 0-Based Array � i � � i − 1 � Parent 2 2 2 i + 1 2 i Left child 2 i + 1 2 i + 2 Right child 0 1 2 3 4 5 6 7 8 9 4 / 28

  5. (Min) Heap Property Min Heap Property A complete binary tree satisfies min heap property if, for each node i which is not the root, key ( parent ( i )) ≤ key ( i ) . In case of an array A : A [ parent ( i )] ≤ A [ i ] . 1 3 3 2 2 1 5 / 28

  6. Heap Heap A heap is an array based representation of a complete binary tree satisfy- ing the heap property. 6 / 28

  7. Building a Heap

  8. Building a Heap We have given an unsorted array. How do we transform it into a heap? 9 8 7 6 5 4 3 2 1 0 8 / 28

  9. Building a Heap Observation ◮ Leafs are valid heaps. 9 / 28

  10. Building a Heap Idea ◮ Assume left and right subtrees are valid heaps. 10 / 28

  11. Building a Heap Idea ◮ Assume left and right subtrees are valid heaps. ◮ If necessary, exchange root with root of left or right subtree. 10 / 28

  12. Building a Heap Idea ◮ Assume left and right subtrees are valid heaps. ◮ If necessary, exchange root with root of left or right subtree. ◮ Recursively repair subtree. 10 / 28

  13. Building a Heap Idea ◮ Assume left and right subtrees are valid heaps. ◮ If necessary, exchange root with root of left or right subtree. ◮ Recursively repair subtree. 10 / 28

  14. Building a Heap Idea ◮ Assume left and right subtrees are valid heaps. ◮ If necessary, exchange root with root of left or right subtree. ◮ Recursively repair subtree. 10 / 28

  15. Building a Heap Idea ◮ Assume left and right subtrees are valid heaps. ◮ If necessary, exchange root with root of left or right subtree. ◮ Recursively repair subtree. 10 / 28

  16. Building a Heap — Algorithm 1 Procedure Min-Heapify ( A , i ) Set l := left ( i ) and r := right ( i ) . 2 If l ≥ | A | Then Return 3 If r < | A | and A [ r ] < A [ l ] Then smallest := r 4 Else smallest := l 5 If A [ smallest ] < A [ i ] Then 6 Exchange A [ smallest ] and A [ i ] . 7 Min-Heapify( A , smallest ) 8 The algorithm assumes 0-based arrays. 11 / 28

  17. Building a Heap — Algorithm 1 Procedure Build-Min-Heap ( A ) For i := ⌊| A | / 2 ⌋ − 1 DownTo 0 2 Min-Heapify( A , i ) 3 The algorithm assumes 0-based arrays. Theorem The algorithm Build-Min-Heap runs in linear time. 12 / 28

  18. Example 9 8 7 6 5 4 3 2 1 0 13 / 28

  19. Example 9 8 7 6 5 4 3 2 1 0 13 / 28

  20. Example 9 8 7 6 0 4 3 2 1 5 13 / 28

  21. Example 9 8 7 1 0 4 3 2 6 5 13 / 28

  22. Example 9 8 3 1 0 4 7 2 6 5 13 / 28

  23. Example 9 0 3 1 5 4 7 2 6 8 13 / 28

  24. Example 0 1 3 2 5 4 7 9 6 8 13 / 28

  25. Example 0 1 3 2 5 4 7 9 6 8 13 / 28

  26. Priority Queues

  27. Queues (“FIFO") O ( 1 ) Enqueue ◮ Adds an element to the queue. O ( 1 ) Dequeue ◮ Removes the oldest element in the queue. O ( 1 ) Front ◮ Return the oldest element in the queue without removing it. Enqueue Dequeue 5 2 4 1 3 Front 15 / 28

  28. Priority Queues Enqueue ◮ Adds an element to the queue. Dequeue ◮ Removes the smallest element in the queue. Min ◮ Return the smallest element in the queue without removing it. Dequeue Enqueue 5 2 4 1 3 Min We can implement a priority queue using a heap. 16 / 28

  29. Using a Heap – Min Finding the minimum ◮ The root of the heap ◮ O ( 1 ) time Min 0 1 3 2 5 4 7 9 6 8 17 / 28

  30. Using a Heap – Dequeue Dequeue ◮ “Remove ” the root: Replace it by last element. 8 1 3 2 5 4 7 9 6 8 18 / 28

  31. Using a Heap – Dequeue Dequeue ◮ “Remove ” the root: Replace it by last element. ◮ Restore heap property: Call Min-Heapify( A , 0 ) 8 1 3 2 5 4 7 9 6 18 / 28

  32. Using a Heap – Dequeue Dequeue ◮ “Remove ” the root: Replace it by last element. ◮ Restore heap property: Call Min-Heapify( A , 0 ) ◮ O ( log n ) time 1 2 3 6 5 4 7 9 8 18 / 28

  33. Using a Heap – Enqueue Enqueue ◮ Add new element at the end. 1 2 3 5 8 4 7 Enqueue 9 6 1 19 / 28

  34. Using a Heap – Enqueue Enqueue ◮ Add new element at the end. ◮ Restore heap property: Exchange with parent until parent is smaller or equal. 1 2 3 5 8 4 7 9 6 1 19 / 28

  35. Using a Heap – Enqueue Enqueue ◮ Add new element at the end. ◮ Restore heap property: Exchange with parent until parent is smaller or equal. ◮ O ( log n ) time 1 1 3 5 2 4 7 9 6 8 19 / 28

  36. Heapsort

  37. Heapsort Idea ◮ Make array A to a heap and use it as priority queue. ◮ Order of removing is order in sorted array. 21 / 28

  38. Heapsort Algorithm ◮ Make array A to a max heap. Max-Heap Sorted 22 / 28

  39. Heapsort Algorithm ◮ Make array A to a max heap. ◮ Exchange A [ 0 ] (root of the heap) with A [ heapSize − 1 ] 22 / 28

  40. Heapsort Algorithm ◮ Make array A to a max heap. ◮ Exchange A [ 0 ] (root of the heap) with A [ heapSize − 1 ] ◮ Decrease heapSize by 1 22 / 28

  41. Heapsort Algorithm ◮ Make array A to a max heap. ◮ Exchange A [ 0 ] (root of the heap) with A [ heapSize − 1 ] ◮ Decrease heapSize by 1 ◮ Call Max-Heapify( A , 0 ). 22 / 28

  42. Heapsort Algorithm ◮ Make array A to a max heap. ◮ Exchange A [ 0 ] (root of the heap) with A [ heapSize − 1 ] ◮ Decrease heapSize by 1 ◮ Call Max-Heapify( A , 0 ). Max-Heap Sorted 22 / 28

  43. Heapsort Properties ◮ Runtime: O ( n log n ) ◮ Memory: O ( 1 ) (if implemented correctly) ◮ Not stable 23 / 28

  44. Exercises

  45. Exercises You wish to store a set of n numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. (a) Want to find the maximum element quickly. (b) Want to be able to delete an element quickly. (c) Want to be able to form the structure quickly. (d) Want to find the minimum element quickly. 25 / 28

  46. Exercises Give an O ( n log k ) time algorithm to merge k sorted lists into one sorted list, where n is the total number of elements in all the input lists. 26 / 28

  47. Exercises Design a data structure that supports the following two operations: ◮ addNum( i ) Adds an integer to the data structure. ◮ fi ndMedian() Returns the median of all elements so far. 27 / 28

  48. Exercises You are given a max-heap with n elements. Give an algorithm to find the k largest elements. You are allowed to destroy the heap. How fast is your algorithm? 28 / 28

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend