CS 270 Algorithms
Total Page:16
File Type:pdf, Size:1020Kb
CS 270 Algorithms Week 10 Oliver Kullmann Binary heaps Sorting Heapification Building a heap 1 Binary heaps HEAP- SORT Priority 2 Heapification queues QUICK- 3 Building a heap SORT Analysing 4 QUICK- HEAP-SORT SORT 5 Priority queues Tutorial 6 QUICK-SORT 7 Analysing QUICK-SORT 8 Tutorial 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 CLRS Reading from for week 7 QUICK- SORT 1 Chapter 6, Sections 6.1 - 6.5. Analysing 2 QUICK- Chapter 7, Sections 7.1, 7.2. SORT Tutorial CS 270 Discover the properties of binary heaps Algorithms Oliver Running example Kullmann Binary heaps Heapification Building a heap HEAP- SORT Priority queues QUICK- SORT Analysing QUICK- SORT Tutorial CS 270 First property: level-completeness Algorithms Oliver Kullmann Binary heaps In week 7 we have seen binary trees: Heapification Building a 1 We said they should be as “balanced” as possible. heap 2 Perfect are the perfect binary trees. HEAP- SORT 3 Now close to perfect come the level-complete binary Priority trees: queues QUICK- 1 We can partition the nodes of a (binary) tree T into levels, SORT according to their distance from the root. Analysing 2 We have levels 0, 1,..., ht(T ). QUICK- k SORT 3 Level k has from 1 to 2 nodes. Tutorial 4 If all levels k except possibly of level ht(T ) are full (have precisely 2k nodes in them), then we call the tree level-complete. CS 270 Examples Algorithms Oliver The binary tree Kullmann 1 ❚ Binary heaps ❥❥❥❥ ❚❚❚❚ ❥❥❥❥ ❚❚❚❚ ❥❥❥❥ ❚❚❚❚ Heapification 2 ❥ 3 ❖ ❄❄ ❖❖ Building a ⑧⑧ ❄ ⑧⑧ ❖❖ heap ⑧⑧ ❄ ⑧⑧ ❖❖❖ 4 5 6 ❄ 7 ❄ HEAP- ⑧ ❄ ⑧ ❄ SORT ⑧⑧ ❄ ⑧⑧ ❄ Priority 10 13 14 15 queues QUICK- is level-complete (level-sizes are 1, 2, 4, 4), while SORT ❥ 1 ❚❚ Analysing ❥❥❥❥ ❚❚❚❚ QUICK- ❥❥❥ ❚❚❚ SORT ❥❥❥ ❚❚❚❚ 2 ❥❥ 3 ♦♦♦ ❄❄ ⑧ Tutorial ♦♦ ❄❄ ⑧⑧ ♦♦♦ ⑧ 4 ❄ 5 ❄ 6 ❄ ⑧⑧ ❄❄ ⑧ ❄ ⑧ ❄ ⑧⑧ ❄ ⑧⑧ ❄ ⑧⑧ ❄ 8 9 10 11 12 13 is not (level-sizes are 1, 2, 3, 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. 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 nodes of the last layer better are not placed in random order: heap HEAP- SORT Best is if they fill the positions from the left without gaps. Priority A level-complete binary tree with such gap-less last layer is queues QUICK- called a complete tree. SORT Analysing So the level-complete binary tree on the examples-slide is QUICK- not complete. SORT While the running-example is complete. Tutorial 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 with the completeness property. Heapification Building a 2 However we have another property related to order (not heap HEAP- just related to the structure of the tree): The value of every 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 Analysing More precisely it is the max-heap property. 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. CS 270 Fourth property: Efficient index computation Algorithms Oliver Kullmann Binary heaps Consider the numbering (not the values) of the nodes of the Heapification Building a running-example: heap HEAP- 1 This numbering follows the layers, beginning with the first SORT Priority layer and going from left to right. queues 2 Due to the completeness property (no gaps!) these numbers QUICK- SORT yield easy relations between a parent and its children. Analysing 3 QUICK- If the node has number p, then the left child has number SORT 2p, and the right child has number 2p + 1. Tutorial 4 And the parent has number ⌊p/2⌋. 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 HEAP- 2 However now, for complete binary trees we can use a more SORT efficient array implementation, using the numbering for the Priority array-indices. queues QUICK- SORT m So a binary heap with nodes is represented by an array with Analysing m QUICK- elements: SORT C-based languages use 0-based indices (while the book uses Tutorial 1-based indices). For such an index 0 ≤ i < m the index of the left child is 2i +1, and the index of the right child is 2i + 2. While the index of the parent is ⌊(i − 1)/2⌋. 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 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 assume anything on A[i]. heap HEAP- After the “heapification”, the values of the binary tree SORT i Priority rooted at have been rearranged, so that it is a binary queues (max-)heap now. QUICK- SORT Analysing For that, the algorithm proceeds as follows: QUICK- SORT 1 First the largest of A[i], A[l], A[r] is determined, where Tutorial l = 2i and r = 2i + 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. 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 HEAP- case), and thus the running-time of heapification is SORT Priority queues linear in the height h of the subtree. QUICK- SORT Analysing This is O(lg n), where n is the number of nodes in the subtree QUICK- (due to h = ⌊lg n⌋). SORT Tutorial CS 270 Heapify bottom-up Algorithms Oliver Kullmann Binary heaps Heapification Building a heap HEAP- SORT Priority queues QUICK- SORT Analysing QUICK- SORT Tutorial CS 270 The idea of building a binary heap Algorithms Oliver Kullmann Binary heaps One starts with an arbitrary array A of length n, which shall be Heapification 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. 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 Priority the subtrees are small. 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 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 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- SORT 2 Repeat the following until n = 1: Priority 1 The maximum element is now A[1] — swap that with the queues last element A[n], and remove that last element, i.e., set QUICK- n := n − 1. SORT Analysing 2 Now perform heapification for the root, i.e., i = 1. We have QUICK- a binary (max-)heap again (of length one less). SORT Tutorial The run-time is O(n · lg n). CS 270 All basic operations are (nearly) there Algorithms Oliver Recall that a (basic) (max-)priority queue has the operations: Kullmann Binary heaps MAXIMUM Heapification DELETE-MAX Building a heap INSERTION. HEAP- SORT We use an array A containing a binary (max-)heap (the task is Priority queues just to maintain the heap-property!): QUICK- SORT 1 The maximum is A[1]. Analysing QUICK- 2 For deleting the maximum element, we put the last element SORT A[n] into A[1], decrease the length by one (i.e., n := n − 1), Tutorial and heapify the root (i.e., i = 1). 3 And we add a new element by adding it to the end of the current array, and heapifying all its predecessors up on the way to the root. CS 270 Examples Algorithms Oliver Kullmann Binary heaps Using our running-example, a few slides ago for HEAP-SORT: Heapification Building a heap 1 Considering it from (a) to (j), we can see what happens HEAP- when we perform a sequence of DELETE-MAX SORT operations, until the heap only contains one element (we Priority queues ignore here the shaded elements — they are visible only for QUICK- the HEAP-SORT).