heaps 3
play

heaps 3 Nov. 6, 2017 1 STEM Support https://infomcgillste - PowerPoint PPT Presentation

COMP 250 Lecture 25 heaps 3 Nov. 6, 2017 1 STEM Support https://infomcgillste m.wixsite.com/stems upportmcgill MSSG = McGill Space systems group http://www.mcgillspace.com/#!/ 2 RECALL: min Heap (definition) a b e u f l k m


  1. COMP 250 Lecture 25 heaps 3 Nov. 6, 2017 1

  2. STEM Support https://infomcgillste m.wixsite.com/stems upportmcgill MSSG = McGill Space systems group http://www.mcgillspace.com/#!/ 2

  3. RECALL: min Heap (definition) a b e u f l k m Complete binary tree with (unique) comparable elements, such that each node’s element is less than its children’s element(s). 3

  4. Heap index relations parent = child / 2 left = 2*parent right = 2*parent + 1 a 1 b e 2 3 4 5 6 7 u f l k m Not used 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 4

  5. buildHeap() removeMin() add() upHeap(element) downHeap(1, size) 5

  6. How to build a heap ? (slight variation) buildHeap(){ // assume that an array already contains size elements for (k = 2; k <= size; k++) upHeap( k ) } } 6

  7. How to build a heap ? (slight variation) buildHeap(){ // assume that an array already contains size elements for (k = 2; k <= size; k++) upHeap( k ) } } upHeap(k){ i = k while (i > 1) and ( heap[i] < heap[i / 2] ){ swapElement(i, i/2) i = i/2 } } 7

  8. Recall last lecture: Worse case of buildHeap π‘šπ‘π‘• 2 π‘œ 12 8 4 1 2 π‘œ π‘šπ‘π‘• 2 π‘œ ≀ 𝑒 π‘œ ≀ π‘œ π‘šπ‘π‘• 2 π‘œ 0 𝑗 π‘œ 0 1000 2000 3000 4000 5000

  9. Thus, worst case: buildHeap is Θ π‘œ π‘šπ‘π‘• 2 π‘œ Next, I will show you a Θ(π‘œ) algorithm for building a heap. 9

  10. How to build a heap ? (fast) f m c a e Half the nodes of a heap are leaves. (Each leaf is a heap with one node.) The last non-leaf node has index size/2. 10

  11. How to build a heap ? (fast) buildHeapFast(){ // assume that heap[ ] array contains size elements for (k = size/2; k >= 1; k--) downHeap( k, size ) } 11

  12. 1 2 3 4 5 6 ---------------------- w x t p r f k = 3 w 1 2 3 t x 4 5 6 f p r 12

  13. 1 2 3 4 5 6 ---------------------- w x t p r f k = 3 w 1 2 3 t x downHeap( 3, 6 ) 4 5 6 f p r 13

  14. 1 2 3 4 5 6 ---------------------- w x f p r t k = 3 w 1 2 3 f x downHeap( 3, 6 ) 4 5 6 t p r 14

  15. 1 2 3 4 5 6 ---------------------- w x f p r t k = 2 w 1 downHeap( 2, 6 ) 2 3 f x 4 5 6 t p r 15

  16. 1 2 3 4 5 6 ---------------------- w p f x r t k = 2 w 1 2 3 f p 4 5 6 t x r 16

  17. 1 2 3 4 5 6 ---------------------- w p f x r t k = 1 downHeap( 1, 6 ) w 1 2 3 f p 4 5 6 t x r 17

  18. 1 2 3 4 5 6 ---------------------- f p w x r t k = 1 f 1 2 3 w p 4 5 6 t x r 18

  19. 1 2 3 4 5 6 ---------------------- f p t x r w k = 1 f 1 2 3 t p 4 5 6 w x r 19

  20. buildHeap Fast (list){ copy list into a heap array for (k = size/2; k >= 1; k--) downHeap( k, size ) } Claim: this algorithm is Θ (n). What is the intuition for why this algorithm is so fast? 20

  21. We tends to draw binary trees like this: height h But the number of nodes doubles at each level. So we should draw trees like this: height h 21

  22. buildheap algorithms today last lecture height h Most nodes swap ~ h Few nodes swap ~h times in worst case. times in worst case. 22

  23. How to show buildHeapFast is Θ(π‘œ) ? The worst case number of swaps needed to downHeap node 𝑗 is the height of that node. π‘œ = 𝑗=1 𝑒 π‘œ β„Žπ‘“π‘—π‘•β„Žπ‘’ 𝑝𝑔 π‘œπ‘π‘’π‘“ 𝑗 Β½ of the nodes do no swaps. ΒΌ of the nodes do at most one swap. 1/8 of the nodes do at most two swaps…. 23

  24. Let’s do the calculation for a tree that whose last level is full. level height 0 f 3 1 2 1 2 3 m c 4 5 6 7 g 1 j a e 2 d d j d d d j d 0 3 8 9 10 11 12 13 14 15 24

  25. Worse case of buildHeapFast ? How many elements at π‘šπ‘“π‘€π‘“π‘š π‘š ? ( π‘š ∈ 0, . . ., β„Ž ) What is the height of each π‘šπ‘“π‘€π‘“π‘š π‘š node? 25

  26. Worse case of buildHeapFast ? π‘šπ‘“π‘€π‘“π‘š π‘š has 2 π‘š elements, π‘š ∈ 0, . . ., β„Ž π‘šπ‘“π‘€π‘“π‘š π‘š nodes have height β„Ž βˆ’ π‘š . π‘œ = 𝑗=1 𝑒 π‘œ β„Žπ‘“π‘—π‘•β„Žπ‘’ 𝑝𝑔 π‘œπ‘π‘’π‘“ 𝑗 ? = 26

  27. Worse case of buildHeapFast ? π‘šπ‘“π‘€π‘“π‘š π‘š has 2 π‘š elements, π‘š ∈ 0, . . ., β„Ž π‘šπ‘“π‘€π‘“π‘š π‘š nodes have height β„Ž βˆ’ π‘š . π‘œ = 𝑗=1 𝑒 π‘œ β„Žπ‘“π‘—π‘•β„Žπ‘’ 𝑝𝑔 π‘œπ‘π‘’π‘“ 𝑗 β„Ž 2 π‘š = π‘š=0 β„Ž βˆ’ π‘š 27

  28. Easy Difficult (number (sum of node of nodes) depths) 28

  29. (See next slide) Since , we get : 29

  30. Second term index goes to h-1 only 30

  31. Since , we get : 31

  32. Summary: buildheap algorithms today last lecture height h 𝑃(π‘œ) 𝑃 π‘œ π‘šπ‘π‘• 2 π‘œ 32

  33. Heapsort Given a list with size elements: Build a heap. Repeatedly call removeMin() and put the removed elements into a list.

  34. β€œin place” Heapsort Given an array heap[ ] with size elements: heapsort(){ buildheap( ) for i = 1 to size{ swapElements( heap[1], heap[size + 1 - i]) downHeap( 1, size – i ) } return reverse(heap) }

  35. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w |

  36. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w | w d b e l u k f | a

  37. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w | w d b e l u k f | a b d w e l u k f | a

  38. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w | w d b e l u k f | a b d w e l u k f | a b d k e l u w f | a

  39. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w | b d k e l u w f | a

  40. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w | b d k e l u w f | a f d k e l u w | b a

  41. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w | b d k e l u w f | a f d k e l u w | b a d f k e l u w | b a

  42. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w | b d k e l u w f | a f d k e l u w | b a d f k e l u w | b a d e k f l u w | b a

  43. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w | b d k e l u w f | a d e k f l u w | b a

  44. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w | b d k e l u w f | a d e k f l u w | b a e f k w l u | d b a

  45. 1 2 3 4 5 6 7 8 9 ------------------------------------- a d b e l u k f w | b d k e l u w f | a d e k f l u w | b a e f k w l u | d b a f l k w u | e d b a k l u w | f e d b a l w u | k f e d b a u w | l k f e d b a w | u l k f e d b a w u l k f e d b a

  46. Heapsort heapsort(list){ buildheap(list) for i = 1 to size{ swapElements( heap[1], heap[size + 1 - i]) downHeap( 1, size - i) } return reverse(heap) }

Recommend


More recommend