overview merge sort heaps data structures and algorithms 2020 09 07 heapsort intuitively lecture 3 maintaining the max-heap property building a max-heap overview merge sort application tree merge sort be able to apply merge sort ‘on the fly’ heaps recursion tree heapsort intuitively be able to make a picture of the recursion tree with cost analysis maintaining the max-heap property recurrence equation building a max-heap be able to solve a recurrence equation
divide and conquer programming paradigm overview merge sort heaps heapsort intuitively • divide: divide the problem into smaller subproblems maintaining the max-heap property • conquer: solve the subproblems using recursion building a max-heap • combine: combine the solutions to the subproblems to a solution of the original problem tree: recall definitions tree: recall definitions (see book B5) set of nodes with a parent-child relation the depth of a node is the length of the path to the root there is a unique distinguished node root the height of a node is the maximal length of a path to a leaf every non-root has a unique ancestor / predecessor / parent the height of a tree is the height of the root, or the maximal depth a node may have successors / descendants / children a level or layer of a tree consists of all nodes of the same depth a node without successors is a leaf or external the number of levels is the height of the tree plus one a node with successors is internal
binary tree: definition almost complete binary tree: definition binary tree is complete if: all levels are completely filled binary tree: binary tree is almost or nearly complete if : all levels are completely filled every node has zero, one, or two (ordered) successors except possibly the lowest one which is filled left-to-right complete ⇒ almost complete ⇒ normal binary an almost complete binary tree corresponds naturally to an array height and number of elements (see exercise) parent-children relation in the array i an index in the array consider an almost complete binary tree of height h Algorithm parent( i ): if the lowest level contains one element: number of elements is n = 1 + 2 + . . . + 2 h − 1 + 1 = 2 h − 1 + 1 = 2 h return ⌊ i / 2 ⌋ if the lowest level is full: number of elements is n = 1 + 2 + . . . + 2 h = 2 h +1 − 1 Algorithm left( i ): so 2 h ≤ n ≤ 2 h +1 − 1 < 2 h +1 return 2 i so h ≤ log n < h + 1 so h = ⌊ log n ⌋ Algorithm right( i ): this is important for the complexity of heapsort return 2 i + 1
max-heap max-heap: definition condition on the shape: an almost complete binary tree data structure used for sorting every node is labeled with a key / label from a totally ordered set and for other things (priority queue) intuition: binary tree max-heap property on the keys: on every path from the root to a leaf the labels / keys are non-increasing all levels from as full as possible H [parent( i )] ≥ H [ i ] if we walk downwards then keys decrease hence in a max-heap the max key is at the root we can imagine the definition of a min-heap: labels increase wlaking downwards max-heap: example overview merge sort 16 14 heaps 10 heapsort intuitively 9 11 7 maintaining the max-heap property building a max-heap 16 14 10 11 7 9
heapsort intuition heapsort: idea inventor: J.W.J. Williams in 1964 first part: • turn the input-array into a max-heap we organize the input-array as a max-heap which is a ‘neat binary tree’ we deal with the height of the tree (log n ) second part: • swap the key on the root (the max!) with the key on the last node instead of with the length of the array ( n ) • exclude the last node from the heap, so decrease the heap–size we have to start by turning our arbitrary input-array into a max-heap • reconstruct the heap also we have to maintain the property of being a max-heap procedures for heapsort heapsort: pseudo-code H [1 . . . n ] an array of integers directly after building the heap: H . heap-size = H . length how to turn an array into a max-heap: procedure buildMaxHeap Algorithm heapsort( H ): how to make a root with max-heaps as chidren into a max-heap: buildMaxHeap(H) for i = H . length downto 2 do procedure MaxHeapify swap H [1] and H [ i ] H . heap-size := H . heap-size − 1 MaxHeapify(H , 1)
overview MaxHeapify: bubble in heaps merge sort heaps we have a node with left and right max-heaps heapsort intuitively we reconstruct the max-heap property using a down-heap bubble maintaining the max-heap property building a max-heap MaxHeapify MaxHeapify: pseudo-code Algorithm MaxHeapify( A , i ): MaxHeapify(H , i) with i a node in H l := left( i ) left and right of i satisfy the max-heap property r := right( i ) • consider i , its left-child l and its right-child r if l ≤ A . heap-size and A [ l ] > A [ i ] then largest := l • determine max of labels of i , l , r else • if i has the largest label then done largest := i if r ≤ A . heap-size and A [ r ] > A [ largest ] then • if l largest label: swap labels of i and of l , do MaxHeapify(H , l) largest := r • if r largest label; swap labels of i and of r , do MaxHeapify(H , r) if largest � = i then swap( A [ i ] , A [ largest ]) MaxHeapify( A , largest )
overview bottom-up heap construction: idea merge sort consider array as proto-heap heaps all leaves are already max-heaps heapsort intuitively consider from highest to lowest indices for non-leaves maintaining the max-heap property apply MaxHeapify those indices building a max-heap building a heap: example building a heap: pseudo-code build a heap from the following input consisting of 2 4 − 1 = 15 numbers: Algorithm buildMaxHeap( H ): H . heap-size := H . length 18 21 20 3 23 17 1 12 13 24 16 22 19 5 8 for i = ⌊ H . length / 2 ⌋ downto 1 do MaxHeapify(H , i)
material recursion tree and recurrence equation merge sort definition of heap intuition for build a max heap and heapsort we will come back to the pseudo-code background: see appendix B5 of the book wiki on sorting
Recommend
More recommend