You are on page 1of 7

What is a Priority Queue?

Priority Queues Stores prioritized elements


• No notion of storing at particular position

Returns elements in priority order


• Order determined by key

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

What’s so different? What’s it good for?


Stacks and Queues Order of returned elements is not FIFO or
LIFO (as in queue or stack)
• Removal order determined by order of
inserting Random access not necessary (as in
sequence) or desirable
Sequences
• User chooses exact placement when inserting Examples
and explicitly chooses removal order • Plane landings managed by air traffic control
Priority Queue • Processes scheduled by CPU
• Order determined by key • College admissions process for students
• Key may be part of element data or separate —What are some of the criteria?
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

College Admissions Key Student selection process


Student submits: Simple scheme
• Personal data (geography, is parent alum?,
activities?) • Collect applications until due date
• Transcript • Sort by keys
• Essays • Take top k students
• Standardized test scores
More realistic
• Recommendations
• Prioritize applications as they come in
Admissions agent:
• Accept some top students ASAP
• Each datum converted to number
• Formula converts to single numeric key • Maybe even change data/key as you go
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

Elements begin in arbitrary order in a


Each call to insertItem(k,
insertItem(k, e) traverses sorted sequence
sequence to find correct position, then does
insert Move elements from sequence into PQ

• O(n) worst case Extract elements from PQ and reinsert into


sequence in priority order
Each call to extractMin(
extractMin( ) does removeFirst(
removeFirst( )
• O(1) time
Analysis depends on implementation choices
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

2
Analyzing Queue Efficiency
Selection Sort
for Sorting

PQ sorting using unsorted sequence

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

Insertion Sort Sort Analysis


Sel.
Sel. Sort Ins. Sort

foreach element, Ei, in S O(n)


PQ sorting using sorted sequence i=n-
i=n-1
Σ O(1)
i=n-
i=n-1
ΣO(i)
PQ.insert(Ei)
PQ.insert(E i=0
i=0

while !PQ.empty() O(n)


i=n-
i=n-1 i=n-
i=n-1

Sequentially insert items into sequence in PQ.extractMin


PQ.extractMin()
() ΣO(i) Σ O(1)
i=0 i=0

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

Heap Heap Example


Binary tree-
tree-based data structure
• Complete in the sense that it fills up levels as 10
completely as possible
• Height of tree is O(logn)
logn) 15 20
Stores elements with keys
19 17 25 30
All nodes satisfy the heap property:
property:
• The key value at a node is less than or equal to
the key value of the node’s children 31 32 22 21 35

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

Heap, or Not a Heap? Heap, or Not a Heap?

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

Heap, or Not a Heap? Inserting into Heap

Create new node as “last” element


1
Insert key/element into new node
3 5
Bubble node upward until heap property is
satisfied
20 32 6 8
while (!isRoot
(!isRoot(node)
(node) &&
(node.key < node.parent.key))
swap(node, parent)
(just pseudocode - can’t do it exactly like this in Java)
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

Bubble Upward Bubble Upward

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

Heap Insert Analysis Extracting from Heap

Copy element from root node


New node always inserted at lowest level Copy element/key from last node to root node
Node bubbles upward Delete last node
• up to root in worst case Bubble root node downward until heap
• path length to root is O(logn
logn) property satisfied
while (!isExternal
(!isExternal(node)
(node) &&
Total time for insert is O(logn
logn)
(node.key > node.smallestChild
node.smallestChild.key))
.key))
swap(node, node.smallestChild
node.smallestChild)
)
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

Move last node to root Bubble Downward


7 7
25 10

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

Bubble Downward All done


7 7
10 10

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

foreach element, Ei, in S O(n)


i=n-
i=n-1
PQ.insert(Ei)
PQ.insert(E Σ O(logi
logi)
Again, each swap takes constant time i=0

while !PQ.empty() O(n)


Maximum swaps is path length from root to PQ.extractMin
PQ.extractMin()
()
i=n-
i=n-1
ΣO(logi
logi)
leaf i=0

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

What does the heap look like after the


following sequence of insertions:

5 30 2 15 7 45 20 6 18

Johns Hopkins Department of Computer Science


Course 600.226: Data Structures, Professor: Jonathan Cohen

You might also like