overview merge sort heaps data structures and algorithms 2020 09 - - PowerPoint PPT Presentation

overview
SMART_READER_LITE
LIVE PREVIEW

overview merge sort heaps data structures and algorithms 2020 09 - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

data structures and algorithms 2020 09 07 lecture 3

  • verview

merge sort heaps heapsort intuitively maintaining the max-heap property building a max-heap

  • verview

merge sort heaps heapsort intuitively maintaining the max-heap property building a max-heap

merge sort

application tree be able to apply merge sort ‘on the fly’ recursion tree be able to make a picture of the recursion tree with cost analysis recurrence equation be able to solve a recurrence equation

slide-2
SLIDE 2

divide and conquer programming paradigm

  • divide: divide the problem into smaller subproblems
  • conquer: solve the subproblems using recursion
  • combine: combine the solutions to the subproblems to a solution of

the original problem

  • verview

merge sort heaps heapsort intuitively maintaining the max-heap property building a max-heap

tree: recall definitions

(see book B5) set of nodes with a parent-child relation there is a unique distinguished node root every non-root has a unique ancestor / predecessor / parent a node may have successors / descendants / children a node without successors is a leaf or external a node with successors is internal

tree: recall definitions

the depth of a node is the length of the path to the root the height of a node is the maximal length of a path to a leaf the height of a tree is the height of the root, or the maximal depth a level or layer of a tree consists of all nodes of the same depth the number of levels is the height of the tree plus one

slide-3
SLIDE 3

binary tree: definition

binary tree: every node has zero, one, or two (ordered) successors

almost complete binary tree: definition

binary tree is complete if: all levels are completely filled binary tree is almost or nearly complete if : all levels are completely filled 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)

consider an almost complete binary tree of height h if the lowest level contains one element: number of elements is n = 1 + 2 + . . . + 2h−1 + 1 = 2h − 1 + 1 = 2h if the lowest level is full: number of elements is n = 1 + 2 + . . . + 2h = 2h+1 − 1 so 2h ≤ n ≤ 2h+1 − 1 < 2h+1 so h ≤ log n < h + 1 so h = ⌊log n⌋ this is important for the complexity of heapsort

parent-children relation in the array

i an index in the array Algorithm parent(i): return ⌊i/2⌋ Algorithm left(i): return 2i Algorithm right(i): return 2i + 1

slide-4
SLIDE 4

max-heap

data structure used for sorting and for other things (priority queue) intuition: binary tree all levels from as full as possible if we walk downwards then keys decrease

max-heap: definition

condition on the shape: an almost complete binary tree every node is labeled with a key / label from a totally ordered set max-heap property on the keys:

  • n every path from the root to a leaf the labels / keys are non-increasing

H[parent(i)] ≥ H[i] 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

11 7 9 16 14 10 11 7 9 14 10 16

  • verview

merge sort heaps heapsort intuitively maintaining the max-heap property building a max-heap

slide-5
SLIDE 5

heapsort intuition

inventor: J.W.J. Williams in 1964 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) instead of with the length of the array (n) we have to start by turning our arbitrary input-array into a max-heap also we have to maintain the property of being a max-heap

heapsort: idea

first part:

  • turn the input-array into a max-heap

second part:

  • swap the key on the root (the max!) with the key on the last node
  • exclude the last node from the heap, so decrease the heap–size
  • reconstruct the heap

procedures for heapsort

how to turn an array into a max-heap: procedure buildMaxHeap how to make a root with max-heaps as chidren into a max-heap: procedure MaxHeapify

heapsort: pseudo-code

H[1 . . . n] an array of integers directly after building the heap: H.heap-size = H.length Algorithm heapsort(H): buildMaxHeap(H) for i = H.length downto 2 do swap H[1] and H[i] H.heap-size := H.heap-size − 1 MaxHeapify(H, 1)

slide-6
SLIDE 6
  • verview

merge sort heaps heapsort intuitively maintaining the max-heap property building a max-heap

MaxHeapify: bubble in heaps

we have a node with left and right max-heaps we reconstruct the max-heap property using a down-heap bubble

MaxHeapify

MaxHeapify(H, i) with i a node in H left and right of i satisfy the max-heap property

  • consider i, its left-child l and its right-child r
  • determine max of labels of i, l, r
  • if i has the largest label then done
  • if l largest label: swap labels of i and of l, do MaxHeapify(H, l)
  • if r largest label; swap labels of i and of r, do MaxHeapify(H, r)

MaxHeapify: pseudo-code

Algorithm MaxHeapify(A, i): l := left(i) r := right(i) if l ≤ A.heap-size and A[l] > A[i] then largest := l else largest := i if r ≤ A.heap-size and A[r] > A[largest] then largest := r if largest = i then swap(A[i], A[largest]) MaxHeapify(A, largest)

slide-7
SLIDE 7
  • verview

merge sort heaps heapsort intuitively maintaining the max-heap property building a max-heap

bottom-up heap construction: idea

consider array as proto-heap all leaves are already max-heaps consider from highest to lowest indices for non-leaves apply MaxHeapify those indices

building a heap: example

build a heap from the following input consisting of 24 − 1 = 15 numbers: 18 21 20 3 23 17 1 12 13 24 16 22 19 5 8

building a heap: pseudo-code

Algorithm buildMaxHeap(H): H.heap-size := H.length for i = ⌊H.length/2⌋ downto 1 do MaxHeapify(H, i)

slide-8
SLIDE 8

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