CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of...

26
Villanova University Department of Computing Sciences CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter Fall 2016

Transcript of CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of...

Page 1: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

CSC 8301– Design andAnalysis of Algorithms:

Heaps

Professor Henry CarterFall 2016

Page 2: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

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

Page 3: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

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

Page 4: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Example heap

4

Page 5: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

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

Page 6: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

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

Page 7: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Heap insertion

7

Page 8: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Max element deletion

8

Page 9: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Heap creation (bottom-up)

9

Page 10: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Heap creation

10

Page 11: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Heap Creation Analysis

11

Page 12: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Sorting with heaps

• Build the array into a heap

• Repeatedly remove the root

• Insert elements in reverse order

12

Page 13: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Analysis

13

Page 14: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Sort Comparisons

14

Bubble Sort

Selection Sort

Insertion Sort

Mergesort Quicksort Heapsort

Strategy Brute force

Brute force

Decrease-n-conq

Divide-n-conq

Divide-n-conq

Transform-n-conq

Average efficiency

Θ(n2) Θ(n2) Θ(n2) Θ(n log n) Θ(n log n) Θ(n log n)

Worst efficiency

Θ(n2) Θ(n2) Θ(n2) Θ(n log n) Θ(n2) Θ(n log n)

Stable yes no yes yes no no

In-place yes yes yes no no yes

Page 15: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Puzzle

• Imagine a handful of uncooked spaghetti, individual rods whose lengths represent numbers that need to be sorted. Outline a “spaghetti sort”–a sorting algorithm that takes advantage of this unorthodox representation.

15

Page 16: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

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)?

16

Page 17: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Nested representation

• 2x3 + x2 – 3x + 1

17

Page 18: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Horner’s algorithm

18

Horner(P [0 . . . n], x)input : an array A[] of coe�cients and a number x

output: The value of the polynomial with coe�cients in A[]

evaluated at x

p P [n]

for i n� 1 to 0 dop x ⇤ p+ P [i]

end

Page 19: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

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

19

Page 20: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

The Binary Polynomial

• n =

• p(x) =

• Example: n = 13

20

Page 21: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Exponent Math

• Multiplying by the base → addition in the exponent

• Squaring the current product → doubles the exponent

21

Page 22: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Evaluating the polynomial

22

Page 23: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Left-to-right Algorithm

23

LeftRightExponentiation(a, b(n))

input : A number a 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 ⇤ productif bi = 1 then

product product ⇤ aend

end

return product

Page 24: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Challenge

• How would you extend the left-to-right binary exponentiation algorithm to work for every nonnegative integer exponent (including n = 0)?

24

Page 25: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

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

25

Page 26: CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of Algorithms: Heaps Professor Henry Carter ... Mergesort Quicksort Heapsort Strategy Brute

Villanova University Department of Computing Sciences

Next Time...

• Levitin Chapter 6.6

‣ Remember, you need to read it BEFORE you come to class!

• Homework

‣ 6.4: 3, 6, 7, 8, 10

‣ 6.5: 4, 7

26