CSC 1700 Analysis of Algorithms: Heaps Professor Henry Carter Fall 2016
Recap Transform-and-conquer preprocesses a problem to make it simpler/more familiar Three types: Instance simplification Representation change Problem reduction Balanced search trees take advantage of modified representation to make data access more efficient 2
Data Structure: Heaps Binary tree structure Two properties: Shape: the tree must be (essentially) complete Heap: the key in each node must be greater than or equal to the keys stored in its children Min-heaps reverse the heap property inequality 3
Example heap 4
Heap application: priority queue Data structure operations: Add element with a priority Read the highest priority element Remove the highest priority element In a heap, the highest priority element is always the root 5
A heap of fun facts For any size n there is a unique heap structure The root is always the largest element Any node considered with its descendants is also a heap A heap can be implemented as an array Store each level sequentially 6
Heap insertion 7
Max element deletion 8
Heap creation (bottom-up) 9
Heap creation 10
Heap Creation Analysis 11
Sorting with heaps Build the array into a heap Repeatedly remove the root Insert elements in reverse order 12
Analysis 13
Sort Comparisons Bubble Sort Selection Sort Insertion Sort Mergesort Quicksort Heapsort Strategy Brute force Brute force Decreasen-conq Divide-nconq Divide-nconq Transform -n-conq Average efficiency Worst efficiency Θ(n 2 ) Θ(n 2 ) Θ(n 2 ) Θ(n log n) Θ(n log n) Θ(n log n) Θ(n 2 ) Θ(n 2 ) Θ(n 2 ) Θ(n log n) Θ(n 2 ) Θ(n log n) Stable yes no yes yes no no In-place yes yes yes no no yes 14
Further representation change: Horner s rule Polynomial evaluation problem Previous discussion revealed the algorithm must be Ω(n) Restricting operations to addition and multiplication, can we implement it in Θ(n)? 15
Nested representation 2x 3 + x 2 3x + 1 16
Horner s algorithm Horner(P [0...n],x) input : an array A[] of coe cients and a number x output: The value of the polynomial with coe p P [n] evaluated at x for i n 1 to 0 do p x p + P [i] end cients in A[] 17
Binary Exponentiation Borrow the nested representation from Horner s rule Represent the exponent as a polynomial Perform equivalent exponent math to evaluate the polynomial based on the bits of the exponent 18
The Binary Polynomial n = p(x) = Example: n = 13 19
Exponent Math Multiplying by the base addition in the exponent Squaring the current product doubles the exponent 20
Evaluating the polynomial 21
Left-to-right Algorithm LeftRightExponentiation(a, b(n)) input :Anumbera and a list of binary digits b(n) in the positive integer n output: The value a n product a for i I 1 downto 0 do product product product end if b i =1then product end return product product a 22
Recap Representation change allows for unintuitive performance gains Heaps allow for fast implementations of priority queues and fast sorting Horner s rule provides a nested representation for fast polynomial evaluation Fast exponentiation can be achieved based on a modification of Horner s rule 23
Next Time... Levitin Chapter 6.6 Remember, you need to read it BEFORE you come to class! Homework 6.4: 2, 3, 5, 10, 12 6.5: 3, 4, 10 24