CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of...
Transcript of CSC 8301– Design and Analysis of Algorithms: Heaps · CSC 8301– Design and Analysis of...
Villanova University Department of Computing Sciences
CSC 8301– Design andAnalysis of Algorithms:
Heaps
Professor Henry CarterFall 2016
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
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
Villanova University Department of Computing Sciences
Example heap
4
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
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
Villanova University Department of Computing Sciences
Heap insertion
7
Villanova University Department of Computing Sciences
Max element deletion
8
Villanova University Department of Computing Sciences
Heap creation (bottom-up)
9
Villanova University Department of Computing Sciences
Heap creation
10
Villanova University Department of Computing Sciences
Heap Creation Analysis
11
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
Villanova University Department of Computing Sciences
Analysis
13
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
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
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
Villanova University Department of Computing Sciences
Nested representation
• 2x3 + x2 – 3x + 1
17
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
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
Villanova University Department of Computing Sciences
The Binary Polynomial
• n =
• p(x) =
• Example: n = 13
20
Villanova University Department of Computing Sciences
Exponent Math
• Multiplying by the base → addition in the exponent
• Squaring the current product → doubles the exponent
21
Villanova University Department of Computing Sciences
Evaluating the polynomial
22
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
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
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
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