CH9. PRIORITY QUEUES
Transcript of CH9. PRIORITY QUEUES
CH9.PRIORITY QUEUESACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH
DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND
GOLDWASSER (WILEY 2016)
PRIORITY QUEUES
β’ Stores a collection of elements each with an associated βkeyβ value
β’ Can insert as many elements in any order
β’ Only can inspect and remove a single element β the minimum (or maximum depending)
element
β’ Applications
β’ Standby Flyers
β’ Auctions
β’ Stock market
PRIORITY QUEUE ADT
β’ A priority queue stores a collection of entries
β’ Each Entry is a key-value pair, with the ADT:
β’ Key getKey()
β’ Value getValue()
β’ Main methods of the Priority Queue ADT
β’ Entry insert(k, v)
inserts an entry with key k and value v
β’ Entry removeMin()
removes and returns the entry with smallest key,
or null if the the priority queue is empty
β’ Additional methods
β’ Entry min()returns, but does not remove, an entry
with smallest key, or null if the the
priority queue is empty
β’ size(), isEmpty()
TOTAL ORDER RELATION
β’ Keys in a priority queue can be
arbitrary objects on which an order
is defined, e.g., integers
β’ Two distinct items in a priority queue
can have the same key
β’ Mathematical concept of total order
relation
β’ Reflexive property:
π β€ π
β’ Antisymmetric property:
if k1 β€ π2 and π2 β€ π1, then π1 = π2
β’ Transitive property:
if π1 β€ π2 and π2 β€ π3 then π1 β€ π3
COMPARATOR ADT
β’ A comparator encapsulates the
action of comparing two objects
according to a given total order
relation
β’ A generic priority queue uses an
auxiliary comparator, i.e., it is
external to the keys being
compared, to compare two keys
β’ Primary method of the Comparator ADT
β’ Integer compare(x, y): returns an
integer π such that
β’ π < 0 if π₯ < π¦,
β’ π = 0 if π₯ = π¦
β’ π > 0 if π₯ > π¦
β’ An error occurs if a and b cannot be
compared.
SORTING WITH A PRIORITY QUEUE
β’ We can use a priority queue to sort a set of
comparable elements
β’ Insert the elements one by one with a series
of insert(π) operations
β’ Remove the elements in sorted order with a
series of removeMin() operations
β’ Running time depends on the PQ
implementation
π π = πππππ π + πππππ π
Algorithm PriorityQueueSort()
Input: List πΏ storing π elements and
a Comparator πΆ
Output: Sorted List πΏ
1. Priority Queue π using πΆ
2. while Β¬πΏ.isEmpty() do
3. π.insert(πΏ.removeFirst())
4. while Β¬π.isEmpty() do
5. πΏ.insertLast(π.removeMin())
6. return πΏ
LIST-BASED PRIORITY QUEUE
Unsorted list implementation
β’ Store the items of the priority queue in a list, in arbitrary order
β’ Performance:
β’ insert(e) takes π(1) time since we can insert the item at the beginning or end of the list
β’ removeMin() and min() take π(π)time since we have to traverse the entire sequence to find the smallest key
Sorted list implementation
β’ Store the items of the priority queue ina list, sorted by key
β’ Performance:
β’ insert(e) takes π(π) time since we have to find the place where to insert the item
β’ removeMin() and min() take π(1)time since the smallest key is at the beginning of the list
4 5 2 3 1 1 2 3 4 5
SELECTION-SORT
β’ Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted list
β’ Running time of Selection-sort:β’ Inserting the elements into the priority queue with π insert(e) operations takes π(π)
time
β’ Removing the elements in sorted order from the priority queue with π removeMin()operations takes time proportional to
π=0
π
π β π = π + π β 1 +β―+ 2 + 1 = π π2
β’ Selection-sort runs in π π2 time
4 5 2 3 1
EXERCISESELECTION-SORT
β’ Selection-sort is the variation of PQ-sort where the priority queue is implemented with an unsorted list (do π insert(e) and then πremoveMin())
β’ Illustrate the performance of selection-sort on the following input sequence:
β’ (22, 15, 36, 44, 10, 3, 9, 13, 29, 25)
4 5 2 3 1
INSERTION-SORT
β’ Insertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted List
β’ Running time of Insertion-sort:
β’ Inserting the elements into the priority queue with π insert(e) operations takes time proportional to
π=0
π
π = 1 + 2 + β―+ π = π π2
β’ Removing the elements in sorted order from the priority queue with a series of π removeMin() operations
takes π π time
β’ Insertion-sort runs in π π2 time
1 2 3 4 5
EXERCISEINSERTION-SORT
β’ Insertion-sort is the variation of PQ-sort where the priority queue is implemented with a sorted list (do π insert(e) and then πremoveMin())
β’ Illustrate the performance of insertion-sort on the following input sequence:
β’ (22, 15, 36, 44, 10, 3, 9, 13, 29, 25)
1 2 3 4 5
IN-PLACE INSERTION-SORT
β’ Instead of using an external data structure, we can implement selection-sort and insertion-sort in-place (only π(1) extra storage)
β’ A portion of the input list itself serves as the priority queue
β’ For in-place insertion-sort
β’ We keep sorted the initial portion of the list
β’ We can use swap(π, π) instead of modifying the list
5 4 2 3 1
5 4 2 3 1
4 5 2 3 1
2 4 5 3 1
2 3 4 5 1
1 2 3 4 5
1 2 3 4 5
HEAPS
2
65
79
WHAT IS A HEAP?
β’ A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:β’ Heap-Order: for every node π£ other
than the root,key π£ β₯ key(π£. parent())
β’ Complete Binary Tree: let β be the height of the heap
β’ for π = 0β¦β β 1, there are 2π nodes on level π
β’ at level β β 1, nodes are filled from left to right
β’ Can be used to store a priority queue efficiently
2
65
79
last node
HEIGHT OF A HEAP
β’ Theorem: A heap storing π keys has height π logπ
β’ Proof: (we apply the complete binary tree property)
β’ Let β be the height of a heap storing β keys
β’ Since there are 2π keys at level π = 0β¦β β 1 and at least one key on level β, we have
π β₯ 1 + 2 + 4 +β―+ 2ββ1 + 1 = 2β β 1 + 1 = 2β
β’ Level β has at most 2β nodes: π β€ 2β+1 β 1
β’ Thus, log n + 1 β 1 β€ β β€ logπ β
1
2
2ββ1
1
keys
0
1
π β π
π
depth
EXERCISEHEAPS
β’ Let π» be a heap with 7 distinct elements (1, 2, 3, 4, 5, 6, and 7). Is it possible
that a preorder traversal visits the elements in sorted order? What about an
inorder traversal or a postorder traversal? In each case, either show such a
heap or prove that none exists.
INSERTION INTO A HEAP
β’ insert(π)consists of three steps
β’ Find the insertion node π§ (the new last
node)
β’ Store π at π§ and expand π§ into an
internal node
β’ Restore the heap-order property
(discussed next)
2
65
79
insertion node
z
2
65
79 1z
UPHEAPβ’ After the insertion of a new element π, the heap-order property may be violated
β’ Up-heap bubbling restores the heap-order property by swapping π along an upward path
from the insertion node
β’ Upheap terminates when π reaches the root or a node whose parent has a key smaller than
or equal to key(π)
β’ Since a heap has height π(log π), upheap runs in π logπ time
1
25
79 6z
2
15
79 6z
REMOVAL FROM A HEAP
β’ removeMin() corresponds to the
removal of the root from the heap
β’ The removal algorithm consists of three steps
β’ Replace the root with the element of the last node π€
β’ Compress π€ and its children into a leaf
β’ Restore the heap-order property (discussed next)
2
65
79
last node
w
7
65
9
w
DOWNHEAP
β’ After replacing the root element of the last node, the heap-order property may be violated
β’ Down-heap bubbling restores the heap-order property by swapping element π along a
downward path from the root
β’ Downheap terminates when π reaches a leaf or a node whose children have keys greater
than or equal to key(π)
β’ Since a heap has height π logπ , downheap runs in π(log π) time
7
65
9
w
5
67
9
w
UPDATING THE LAST NODE
β’ The insertion node can be found by traversing a path of O(log n) nodes
β’ Go up until a left child or the root is reached
β’ If a left child is reached, go to the right child
β’ Go down left until a leaf is reached
β’ Similar algorithm for updating the last node after a removal
HEAP-SORT
β’ Consider a priority queue with π
items implemented by means of a
heap
β’ the space used is π(π)
β’ insert(e) and removeMin()
take π logπ time
β’ min(), size(), and empty()
take π 1 time
β’ Using a heap-based priority queue, we can sort a sequence of πelements in π(π log π) time
β’ The resulting algorithm is called heap-sort
β’ Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort and selection-sort
EXERCISEHEAP-SORT
β’ Heap-sort is the variation of PQ-sort where the priority queue is implemented with a heap (do π insert(e) and then π removeMin())
β’ Illustrate the performance of heap-sort on the following input sequence (draw the heap at each step):
β’ (22, 15, 36, 44, 10, 3, 9, 13, 29, 25)
ARRAY-BASED HEAP IMPLEMENTATION
β’ We can represent a heap with π elements by means of a vector of length πβ’ Links between nodes are not explicitly stored
β’ The leaves are not represented
β’ The cell at index 0 is the root
β’ For the node at index πβ’ the left child is at index 2π + 1
β’ the right child is at index 2π + 2
β’ insert(e) corresponds to inserting at index π + 1
β’ removeMin()corresponds to removing element at index π
β’ Yields in-place heap-sort
2
65
79
2 5 6 9 7
1 2 3 40
PRIORITY QUEUE SUMMARY
insert(e) removeMin() PQ-Sort total
Ordered List
(Insertion Sort)
π(π) π(1) π(π2)
Unordered List
(Selection Sort)
π(1) π(π) π(π2)
Binary Heap,
Array-based Heap
(Heap Sort)
π logπ π logπ π π log π
MERGING TWO HEAPS
β’ We are given two two heaps and a
new element π
β’ We create a new heap with a root
node storing π and with the two
heaps as subtrees
β’ We perform downheap to restore
the heap-order property
7
3
58
2
64
2
3
58
4
67
3
58
2
64
7
BOTTOM-UP HEAP CONSTRUCTION
β’ We can construct a heap storing π
given elements in using a bottom-up
construction with log π phases
β’ In phase π, pairs of heaps with 2π β 1
elements are merged into heaps with
2π+1 β 1 elements
2i -1 2i -1
2i+1-1
EXAMPLE
1516 124 76 2023
25
1516
5
124
11
76
27
2023
EXAMPLE
25
1516
5
124
11
96
27
2023
15
2516
4
125
6
911
20
2723
EXAMPLE
7
15
2516
4
125
8
6
911
20
2723
4
15
2516
5
127
6
8
911
20
2723
EXAMPLE
4
15
2516
5
127
10
6
8
911
20
2723
5
15
2516
7
1210
4
6
8
911
20
2723
ANALYSIS
β’ We visualize the worst-case time of a downheap with a proxy path that goes first right and then repeatedly
goes left until the bottom of the heap (this path may differ from the actual downheap path)
β’ Since each node is traversed by at most two proxy paths, the total number of nodes of the proxy paths is
π(π)
β’ Thus, bottom-up heap construction runs in π(π) time
β’ Bottom-up heap construction is faster than π successive insertions and speeds up the first phase of heap-sort
ADAPTABLE PRIORITY QUEUES
β’ One weakness of the priority queues so far is that we do not have an ability to
update individual entries, like in a changing price market or bidding service
β’ Recall that insert(e) returns an entry. We need to save these values to be able
to adapt them
β’ Additional ADT support (also includes standard priority queue functionality)
β’ Entry remove(e) β remove a specific entry π
β’ Key replaceKey(e, k) β replace the key of entry π with π, and return the old key.
β’ Value replaceValue(e, k) β replace the value of entry π with π, and return the old
value.
LOCATION-AWARE ENTRY
β’ Locators decouple positions and entries
in order to support efficient adaptable
priority queue implementations (i.e., in a
heap)
β’ Each position has an associated locator
β’ Each locator stores a pointer to its
position and memory for the entry
a
g e
POSITIONS VS. LOCATORS
β’ Position
β’ represents a βplaceβ in a data structure
β’ related to other positions in the data structure
(e.g., previous/next or parent/child)
β’ often implemented as a pointer to a node or
the index of an array cell
β’ Position-based ADTs (e.g., sequence and tree)
are fundamental data storage schemes
β’ Locator
β’ identifies and tracks a (key, element) item
β’ unrelated to other locators in the data
structure
β’ often implemented as an object storing the
item and its position in the underlying structure
β’ Key-based ADTs (e.g., priority queue) can be
augmented with locator-based methods
INTERVIEW QUESTION 1
β’ Numbers are randomly generated and passed to a method. Write a program
to find and maintain the median value as new values are generated.
GAYLE LAAKMANN MCDOWELL, "CRACKING THE CODE INTERVIEW: 150 PROGRAMMING QUESTIONS AND
SOLUTIONS", 5TH EDITION, CAREERCUP PUBLISHING, 2011.