Ads Unit 3
Ads Unit 3
com
1
www.JntukMaterials.com
Motivation
Queues are a standard mechanism for
ordering tasks on a first-come, first-served
basis
However, some tasks may be more
important or timely than others (higher
priority)
Priority queues
Store tasks using a partial ordering based on priority
Ensure highest priority task at head of queue
Heaps are the underlying data structure of
priority queues
www.JntukMaterials.com
2
www.JntukMaterials.com
Priority Queues
Main operations
insert (i.e., enqueue)
deleteMin (i.e., dequeue)
Finds the minimum element in the queue, deletes it
from the queue, and returns it
Performance
Goal is for operations to be fast
Will be able to achieve O(log2N) time
insert/deleteMin amortized over multiple
operations
Will be able to achieve O(1) time inserts
amortized over multiple insertions
3
www.JntukMaterials.com
Simple Implementations
Unordered list
O(1) insert
O(N) deleteMin
Ordered list
O(N) insert
O(1) deleteMin
Balanced BST
O(log2N) insert and deleteMin
Observation: We don’t need to keep the
priority queue completely ordered
4
www.JntukMaterials.com
Binary Heap
A binary heap is a binary tree
with two properties
Structure property
A binary heap is a complete binary
tree
Each level is completely filled
Bottom level may be partially filled from
left to right
Height of a complete binary tree
with N elements is log2 N
5
www.JntukMaterials.com
6
www.JntukMaterials.com
Binary Heap
Heap-order property
For every node X, key(parent(X)) ≤
key(X)
Except root node, which has no
parent
Thus, minimum key always at root
Or, maximum, if you choose
Insert and deleteMin must
maintain heap-order property
www.JntukMaterials.com
7
www.JntukMaterials.com
Implementing
Complete Binary
Trees as Arrays
Given element at position i in the
array
i’s left child is at position 2i
i’s right child is at position 2i+1
i’s parent is at position i / 2
www.JntukMaterials.com
8
www.JntukMaterials.com
Heap Insert
Insert new element into the heap
at the next available slot (“hole”)
According to maintaining a complete
binary tree
Then, “percolate” the element
up the heap while heap-order
property not satisfied
1
0
www.JntukMaterials.com
Heap Insert:
Example
Insert 14:
www.JntukMaterials.com
1
1
www.JntukMaterials.com
Heap Insert:
Implementation
www.JntukMaterials.com
1
2
www.JntukMaterials.com
Heap DeleteMin
Minimum element is always at the
root
Heap decreases by one in size
Move last element into hole at root
Percolate down while heap-
order property not satisfied
www.JntukMaterials.com
1
3
www.JntukMaterials.com
1
4
www.JntukMaterials.com
1
5
www.JntukMaterials.com
1
6
www.JntukMaterials.com
Heap DeleteMin:
Implementation
1
7
www.JntukMaterials.com
Heap DeleteMin:
Implementation
1
8
www.JntukMaterials.com
Building a Heap
Construct heap from initial set of N
items
Solution 1
Perform N inserts
O(N) average case, but O(N log2 N) worst-
case
Solution 2
Assume initial set is a heap
Perform a percolate-down from each
internal node (H[size/2] to H[1])
www.JntukMaterials.com
2
0
www.JntukMaterials.com
BuildHeap Example
2
1
www.JntukMaterials.com
BuildHeap Example
2
2
www.JntukMaterials.com
BuildHeap Example
2
3
www.JntukMaterials.com
BuildHeap Example
2
4
www.JntukMaterials.com
BuildHeap Implementation
2
5
www.JntukMaterials.com
BuildHeap Analysis
Running time of buildHeap
proportional to sum of the heights of
the nodes
Theorem 6.1
For the perfect binary tree of height h
h+1
containing 2 – 1 nodes, the sum of
h+1
heights of the nodes is 2 – 1 – (h + 1)
Since N = 2h+1 – 1, then sum of
heights is O(N)
Slightly better for complete binary tree
www.JntukMaterials.com
2
6
www.JntukMaterials.com
Binary Heap
Operations Worst-
case Analysis
Height of heap is log2 N
insert: O(log2N)
2.607 comparisons on average, i.e., O(1)
deleteMin: O(log2N)
decreaseKey: O(log2N)
increaseKey: O(log2N)
remove: O(log2N)
buildHeap: O(N)
www.JntukMaterials.com 2
7
www.JntukMaterials.com
Applications
Operating system scheduling
Process jobs by priority
Graph algorithms
Find the least-cost, neighboring
vertex
Event simulation
Instead of checking for events at
each time click, look up next event to
happen
www.JntukMaterials.com
2
8
www.JntukMaterials.com
Priority Queues:
Alternatives to Binary
Heaps
d-Heap
Each node has d children
insert in O(logd N) time
deleteMin in O(d logd N) time
Binary heaps are 2-Heaps
www.JntukMaterials.com 2
9
www.JntukMaterials.com
Mergeable Heaps
Heap merge operation
Useful for many applications
Merge two (or more) heaps into one
Identify new minimum element
Maintain heap-order property
Merge in O(log N) time
Still support insert and deleteMin in O(log N)
time
Insert = merge existing heap with one-element
heap
d-Heaps require O(N) time to merge
www.JntukMaterials.com
3
0
www.JntukMaterials.com
Leftist Heaps
Null path length npl(X) of node X
Length of the shortest path from X to a
node without two children
Leftist heap property
For every node X in heap,
npl(leftChild(X)) ≥ npl(rightChild(X))
Leftist heaps have deep left
subtrees and shallow right subtrees
Thus if operations reside in right subtree,
they will be faster
3
1
www.JntukMaterials.com
Leftist Heaps
npl(X) shown in nodes
3
2
www.JntukMaterials.com
Leftist Heaps
Theorem 6.2
A leftist tree with r nodes on the right
path must have at least 2r – 1 nodes.
Thus, a leftist tree with N nodes
has a right path with at most log(N
+1) nodes
www.JntukMaterials.com
3
3
www.JntukMaterials.com
Leftist Heaps
Merge heaps H1 and H2
Assume root(H1) > root(H2)
Recursively merge H1 with right subheap of
H2
If result is not leftist, then swap the left
and right subheaps
Running time O(log N)
DeleteMin
Delete root and merge children
www.JntukMaterials.com
3
4
www.JntukMaterials.com
3
5
www.JntukMaterials.com
3
6
www.JntukMaterials.com
3
7
www.JntukMaterials.com
3
8
www.JntukMaterials.com
Skew Heaps
Self-adjusting version of leftist heap
Skew heaps are to leftist heaps as splay
trees are to AVL trees
Skew merge same as leftist merge,
except we always swap left and right
subheaps
No need to maintain or test NPL of
nodes
Worst case is O(N)
www.JntukMaterials.com
Amortized cost of M operations is O(M
log N)
3
9
www.JntukMaterials.com
Binomial Queues
Support all three operations in
O(log N) worst-case time per
operation
Insertions take O(1) average-case
time
Key idea
Keep a collection of heap-ordered
trees to postpone merging
www.JntukMaterials.com
4
0
www.JntukMaterials.com
Binomial Queues
A binomial queue is a forest of binomial
trees
Each in heap order
Each of a different height
A binomial tree Bk of height k consists
of two Bk-1 binomial trees
The root of one Bk-1 tree is the child of the
root of the other Bk-1 tree
Bk =
B
k-1
B
k-1
www.JntukMaterials.com
4
1
www.JntukMaterials.com
Binomial Trees
4
2
www.JntukMaterials.com
Binomial Trees
Binomial trees of height k have
exactly 2k nodes
k
,
Number of nodes at depth d is the
d
binomial coefficient
A priority queue of any size
can be represented by a
binomial queue
Binary representation of Bk
www.JntukMaterials.com
4
3
www.JntukMaterials.com
Binomial Queue
Operations
Minimum element found by
checking roots of all trees
At most (log2 N) of them, thus O(log
N)
Or, O(1) by maintaining
pointer to minimum element
www.JntukMaterials.com
4
4
www.JntukMaterials.com
Binomial Queue
Operations
Merge (H1,H2) H3
Add trees of H1 and H2 into H3 in increasing order by
depth
Traverse H3
If find two consecutive Bk trees, then create a Bk+1 tree
If three consecutive Bk trees, then leave first, combine
last two
Never more than three consecutive Bk trees
Keep binomial trees ordered by height
min(H3) = min(min(H1),min(H2))
Running time O(log N)
www.JntukMaterials.com
4
5
www.JntukMaterials.com
Merge Example
4
6
www.JntukMaterials.com
Binomial Queue
Operations
Insert (x, H1)
Create single-element queue H2
Merge (H1,H2)
Running time proportional to minimum k such
that Bk not in heap
O(log N) worst case
Probability Bk not present is 0.5
Thus, likely to find empty Bk after two tries on
average
O(1) average case
www.JntukMaterials.com
4
7
www.JntukMaterials.com
Binomial Queue
Operations
deleteMin (H1)
Remove min(H1) tree from H1
Create heap H2 from the children of min(H)
Merge (H1,H2)
Running time O(log N)
www.JntukMaterials.com
4
8
www.JntukMaterials.com
deleteMin Example
4
9
www.JntukMaterials.com
Binomial Queue
Implementation
Array of binomial trees
Trees use first-child, right-
sibling representation
H3 :
www.JntukMaterials.com
50
www.JntukMaterials.com
5
1
www.JntukMaterials.com
5
2
www.JntukMaterials.com
5
3
www.JntukMaterials.com
5
4
www.JntukMaterials.com
merge (cont.)
5
5
www.JntukMaterials.com
merge (cont.)
5
6
www.JntukMaterials.com
5
7
www.JntukMaterials.com
deleteMin (cont.)
5
8
www.JntukMaterials.com
5
9
www.JntukMaterials.com
6
1
www.JntukMaterials.com
Summary
Priority queues maintain the
minimum or maximum element
of a set
Support O(log N) operations worst-
case
insert, deleteMin, merge
Support O(1) insertions average
case
www.JntukMaterials.com
Many applications in support of
other algorithms
6
2