week 10
play

Week 10 Oliver Kullmann Binary heaps Sorting Heapification - PowerPoint PPT Presentation

CS 270 Algorithms Week 10 Oliver Kullmann Binary heaps Sorting Heapification Building a heap Binary heaps 1 HEAP- SORT Priority Heapification 2 queues QUICK- Building a heap 3 SORT Analysing HEAP-SORT QUICK- 4 SORT


  1. CS 270 Algorithms Week 10 Oliver Kullmann Binary heaps Sorting Heapification Building a heap Binary heaps 1 HEAP- SORT Priority Heapification 2 queues QUICK- Building a heap 3 SORT Analysing HEAP-SORT QUICK- 4 SORT Priority queues Tutorial 5 QUICK-SORT 6 Analysing QUICK-SORT 7 Tutorial 8

  2. CS 270 General remarks Algorithms Oliver Kullmann Binary heaps Heapification Building a heap We return to sorting, considering HEAP-SORT and HEAP- QUICK-SORT. SORT Priority queues Reading from CLRS for week 7 QUICK- SORT 1 Chapter 6, Sections 6.1 - 6.5. Analysing 2 Chapter 7, Sections 7.1, 7.2. QUICK- SORT Tutorial

  3. CS 270 Discover the properties of binary heaps Algorithms Running example Oliver Kullmann Binary heaps Heapification Building a heap HEAP- SORT Priority queues QUICK- SORT Analysing QUICK- SORT Tutorial

  4. CS 270 First property: level-completeness Algorithms Oliver Kullmann Binary heaps In week 7 we have seen binary trees : Heapification 1 We said they should be as “balanced” as possible. Building a heap 2 Perfect are the perfect binary trees. HEAP- SORT 3 Now close to perfect come the level-complete binary Priority trees : queues QUICK- We can partition the nodes of a (binary) tree T into levels, 1 SORT according to their distance from the root. Analysing We have levels 0 , 1 , . . . , ht( T ). QUICK- 2 Level k has from 1 to 2 k nodes. SORT 3 Tutorial If all levels k except possibly of level ht( T ) are full (have 4 precisely 2 k nodes in them), then we call the tree level-complete .

  5. CS 270 Examples Algorithms Oliver The binary tree Kullmann 1 ❚ 2 ❥❥❥❥❥❥❥❥❥❥❥❥❥ ❚ Binary heaps ❚ ❚ ❚ ❚ ❚ ❚ ❚ ❚ ❚ Heapification ❚ ❚ 3 ❖ ❄ ❖ Building a ❖ ❄ ⑧ ⑧ ❖ ❄ ⑧ ⑧ ❖ ❄ ❖ heap ⑧ ⑧ ❖ ⑧ ⑧ ❖ 4 5 6 7 HEAP- ❄ ❄ ❄ ❄ ⑧ ⑧ SORT ⑧ ❄ ⑧ ❄ ⑧ ⑧ 10 13 14 15 Priority queues QUICK- is level-complete (level-sizes are 1 , 2 , 4 , 4), while SORT 1 Analysing ❚ 2 ❥❥❥❥❥❥❥❥❥❥❥❥❥ ❚ ❚ ❚ ❚ QUICK- ❚ ❚ ❚ ❚ ❚ SORT ❚ ❚ ❚ 3 4 ♦♦♦♦♦♦♦♦ Tutorial ❄ ❄ ⑧ ❄ ⑧ ❄ ⑧ ⑧ 5 6 ❄ ❄ ❄ ❄ ⑧ ⑧ ❄ ⑧ ❄ ❄ ⑧ ⑧ ❄ ⑧ ❄ ❄ ⑧ ⑧ ⑧ ⑧ 8 9 10 11 12 13 is not (level-sizes are 1 , 2 , 3 , 6).

  6. CS 270 The height of level-complete binary trees Algorithms Oliver Kullmann Binary heaps Heapification For a level-complete binary tree T we have Building a heap ht( T ) = ⌊ lg(#nds( T )) ⌋ . HEAP- SORT Priority That is, the height of T is the binary logarithm of the number queues of nodes of T , after removal of the fractional part. QUICK- SORT Analysing We said that “balanced” T should have QUICK- ht( T ) ≈ lg(#nds( T )). SORT Tutorial Now that’s very close.

  7. CS 270 Second property: completeness Algorithms Oliver Kullmann Binary heaps Heapification To have simple and efficient access to the nodes of the tree, the Building a heap nodes of the last layer better are not placed in random order: HEAP- SORT Best is if they fill the positions from the left without gaps. Priority queues A level-complete binary tree with such gap-less last layer is QUICK- called a complete tree . SORT So the level-complete binary tree on the examples-slide is Analysing QUICK- not complete. SORT Tutorial While the running-example is complete.

  8. CS 270 Third property: the heap-property Algorithms Oliver The running-example is not a binary search tree: Kullmann 1 It would be too expensive to have this property together Binary heaps Heapification with the completeness property. Building a 2 However we have another property related to order (not heap just related to the structure of the tree): The value of every HEAP- SORT node is not less than the value of any of its successors (the Priority nodes below it). queues QUICK- 3 This property is called the heap property . SORT 4 More precisely it is the max-heap property . Analysing QUICK- SORT Definition 1 Tutorial A binary heap is a binary tree which is complete and has the heap property. More precisely we have binary max-heaps and binary min-heaps .

  9. CS 270 Fourth property: Efficient index computation Algorithms Oliver Kullmann Binary heaps Heapification Consider the numbering (not the values) of the nodes of the Building a running-example: heap HEAP- 1 This numbering follows the layers, beginning with the first SORT layer and going from left to right. Priority queues 2 Due to the completeness property (no gaps!) these numbers QUICK- SORT yield easy relations between a parent and its children. Analysing 3 If the node has number p , then the left child has number QUICK- SORT 2 p , and the right child has number 2 p + 1. Tutorial 4 And the parent has number ⌊ p / 2 ⌋ .

  10. CS 270 Efficient array implementation Algorithms Oliver For binary search trees we needed full-fledged trees (as discussed Kullmann in week 7): Binary heaps Heapification 1 That is, we needed nodes with three pointers: to the parent Building a and to the two children. heap 2 However now, for complete binary trees we can use a more HEAP- SORT efficient array implementation, using the numbering for the Priority queues array-indices. QUICK- SORT So a binary heap with m nodes is represented by an array with Analysing m elements: QUICK- SORT Tutorial C-based languages use 0-based indices (while the book uses 1-based indices). For such an index 0 ≤ i < m the index of the left child is 2 i + 1, and the index of the right child is 2 i + 2. While the index of the parent is ⌊ ( i − 1) / 2 ⌋ .

  11. CS 270 Float down a single disturbance Algorithms Oliver Kullmann Binary heaps Heapification Building a heap HEAP- SORT Priority queues QUICK- SORT Analysing QUICK- SORT Tutorial

  12. CS 270 The idea of heapification Algorithms Oliver Kullmann The input is an array A and index i into A . Binary heaps It is assumed that the binary trees rooted at the left and Heapification right child of i are binary (max-)heaps, but we do not Building a heap assume anything on A [ i ]. HEAP- After the “heapification”, the values of the binary tree SORT rooted at i have been rearranged, so that it is a binary Priority queues (max-)heap now. QUICK- SORT For that, the algorithm proceeds as follows: Analysing QUICK- SORT 1 First the largest of A [ i ] , A [ l ] , A [ r ] is determined, where Tutorial l = 2 i and r = 2 i + 1 (the two children). 2 If A [ i ] is largest, then we are done. 3 Otherwise A [ i ] is swapped with the largest element, and we call the procedure recursively on the changed subtree.

  13. CS 270 Analysing heapification Algorithms Oliver Kullmann Binary heaps Heapification Building a Obviously, we go down from the node to a leaf (in the worst heap case), and thus the running-time of heapification is HEAP- SORT Priority queues linear in the height h of the subtree. QUICK- SORT This is O (lg n ), where n is the number of nodes in the subtree Analysing QUICK- (due to h = ⌊ lg n ⌋ ). SORT Tutorial

  14. CS 270 Heapify bottom-up Algorithms Oliver Kullmann Binary heaps Heapification Building a heap HEAP- SORT Priority queues QUICK- SORT Analysing QUICK- SORT Tutorial

  15. CS 270 The idea of building a binary heap Algorithms Oliver Kullmann Binary heaps Heapification One starts with an arbitrary array A of length n , which shall be Building a re-arranged into a binary heap. Our example is heap HEAP- A = (4 , 1 , 3 , 2 , 16 , 9 , 10 , 14 , 8 , 7) . SORT Priority queues We repair (heapify) the binary trees bottom-up: QUICK- SORT 1 The leaves (the final part, from ⌊ n / 2 ⌋ + 1 to n ) are already Analysing QUICK- binary heaps on their own. SORT 2 For the other nodes, from right to left, we just call the Tutorial heapify-procedure.

  16. CS 270 Analysing building a heap Algorithms Oliver Kullmann Binary heaps Heapification Building a Roughly we have O ( n · lg n ) many operations: heap HEAP- 1 Here however it pays off to take into account that most of SORT the subtrees are small. Priority queues 2 Then we get run-time O ( n ). QUICK- SORT Analysing QUICK- So building a heap is linear in the number of elements. SORT Tutorial

  17. CS 270 Heapify and remove from last to first Algorithms Oliver Kullmann Binary heaps Heapification Building a heap HEAP- SORT Priority queues QUICK- SORT Analysing QUICK- SORT Tutorial

  18. CS 270 The idea of HEAP-SORT Algorithms Oliver Kullmann Binary heaps Heapification Now the task is to sort an array A of length n : Building a heap 1 First make a heap out of A (in linear time). HEAP- 2 Repeat the following until n = 1: SORT Priority The maximum element is now A [1] — swap that with the queues 1 last element A [ n ], and remove that last element, i.e., set QUICK- SORT n := n − 1. Analysing Now perform heapification for the root, i.e., i = 1. We have 2 QUICK- a binary (max-)heap again (of length one less). SORT Tutorial The run-time is O ( n · lg n ).

Recommend


More recommend