CS226 - Day 26 - Spring 2013 HEAPS continued --------------------------------------------------- Build-Heap in O(N) - bottom up approach - throw all values in heap - starting at height 1 - check each value and percolate down, then go up to next level & repeat all the way up to root Example: 13 20 15 3 10 7 10 5 2 8 12 4 1 9 14 phase1: (height 1 nodes) 13 20 15 2 8 1 9 5 3 10 12 4 7 10 14 phase2: 13 2 1 3 8 4 9 5 20 10 12 15 7 10 14 phase3: 1 2 4 3 8 7 9 5 20 10 12 15 13 10 14 - complexity analysis: - each node at height i makes at most i swaps, - <= 2^(h-i) nodes at height i, where h is tree height #swaps = sum of heights = sum (i=1,h) i * 2^(h-i) (checkout WolframAlpha on-line) = 2^(h+1) -1 - (h+1) h is ~~ log N, sum <= 2N = O(N) PQ application: selection problem - find kth smallest element - obvious heap solution: - put all values in min heap, do k removeMin ops - time is O(N + k log N) - less obvious solution: to find the kth smallest element - make MAX heap of first k elements O(k) bottom up - for other N-k elements, compare to max in heap, if element is smaller, removeMax and insert value: O((N-k)log K) - when done, max value is kth smallest - total time O(N log k) - if k=N/2, these find the median in O(N log N) Adaptable Priority Queue - keys or values in entries may change ----------------------------------------------------------------- new methods have monkeywrench - must find specific entries - remove(e) - remove specific entry from queue - replaceKey(e, k) - replace entry e key with k - replaceValue(e, x) - replace entry e with x - use location aware entries that have ref to position in PQ (eg - hash table lookup from entries to positions in PQ) - delete(position) - replace with last leaf, percolate up or down - decreaseKey(position, change) - percolate up - log N - increaseKey(position, change) - percolate down - log N HEAP VARIATIONS -------------------------------------- D-Heap: - d-ary tree (instead of binary) - height is logd N (less than log2 N) - deleteMin is O(d logd N) - why? d steps to find min child - when to use? Goal of these heap variations is to support fast merges Leftist Heap - long on left side, work on shorter right side Skew Heap - sort of like a splay tree Binomial Queue - collection of binomial trees to support merging SORTING -------------------------------------------------------- Review of sorting so far: (see sorting.pdf) ----------------------------- - bubble sort O(N^2) - selection sort O(N^2) - insertion sort O(N^2) - merge sort O(N log N) Heap Sort in place: T(N) = N+NlogN = O(N log N) O(N) - build max heap bottom up O(N log N) - swap each root max to next leaf & adjust - ranked array representation is now in order smallest to largest