Professional Documents
Culture Documents
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
1
Priority Queue ADT Keys, Comparitors and Total Orders
insertItem(k,e):
insertItem(k,e): insert element e with key k Key type needs comparison operator (returns
boolean)
boolean) with following properties:
extractMin(
extractMin( ): return element with minimum
key and remove from queue • Reflexive: k ≤ k
• Antisymmetric:
Antisymmetric:
minElement(
minElement( ): return (look at) min element (k1 ≤ k2) && (k2 ≤ k1) → k1 = k2
minKey(
minKey( ): return minimum key • Transitive:
((k1 ≤ k2) && (k2 ≤ k3) → k1 ≤ k3
size( ): return number of elements
These properties guarantee consistent, total
isEmpty(
isEmpty( ): size == 0? ordering
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
Implementing PQ with
Abstracting Comparitors Unsorted Sequence
Allows for different types of comparison
• e.g. Numeric vs.
vs. lexicographic (for strings) Each call to insertItem(k,
insertItem(k, e) uses insertLast(
insertLast( )
to store in Sequence
Several approaches possible
• O(1) time
• Build PQ object to know about specific key
type and comparison Each call to extractMin(
extractMin( ) traverses the entire
• Build key object to know about comparison sequence to find the minimum, then
• Build separate comparitor object for each type removes element
of comparison
• O(n) time
Book argues for #3, but I also recommend #2
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
Implementing PQ with
Sorted Sequence
Sorting Using a PQ
2
Analyzing Queue Efficiency
Selection Sort
for Sorting
N insertElement(
insertElement( ) operations followed by N Insert all n items in input order
extractMin(
extractMin( ) operations
Extract by selecting min item n times
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
sorted order
Extract items easily from sorted sequence i=n-
i=n-1 i=n-
i=n-1
O(n) + ΣO(1) + ΣO(i) = O(n) + O(n) + O(n2) = O(n2)
i=0 i=0
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
Allows insertItem(
insertItem( ) and extractMin( extractMin( ) in
O(logn)
logn) time
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
3
PQ Quiz Show! Heap, or Not a Heap?
g c
Heap, or Not A Heap?
h i e
(no paper necessary)
p n q w k f
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
f c
j v k f
n k w z q m j r
s x p m y s p v
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
4
Heap Insert Example Bubble Upward
10 10
15 20 15 20
19 17 25 30 19 17 7 30
31 32 22 21 35 7 31 32 22 21 35 25
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
10 7
15 7 15 10
19 17 20 30 19 17 20 30
31 32 22 21 35 25 31 32 22 21 35 25
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
5
Heap Extract Example Copy Extracted Element
7
7
15 10 15 10
19 17 20 30 19 17 20 30
31 32 22 21 35 25 31 32 22 21 35 25
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
15 10 15 25
19 17 20 30 19 17 20 30
31 32 22 21 35 31 32 22 21 35
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
15 20 15 20
19 17 25 30 19 17 25 30
31 32 22 21 35 31 32 22 21 35
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
6
Heap Extract Analysis Sort Analysis
Heap Sort
i=n-
i=n-1 i=n-
i=n-1
→ Total work is logn
logn * O(1) = O(logn
logn) O(n) +2 ΣO(logi
logi) < O(n) + 2 ΣO(logn
logn)
i=0 i=0
= O(n) + 2n*O
2n*O((logn
logn) = O(
O(nlogn
logn)
(showing θ (nlogn
logn) is a bit harder)
Johns Hopkins Department of Computer Science Johns Hopkins Department of Computer Science
Course 600.226: Data Structures, Professor: Jonathan Cohen Course 600.226: Data Structures, Professor: Jonathan Cohen
In-
In-class Exercise
5 30 2 15 7 45 20 6 18