Date post: | 18-Dec-2015 |
Category: |
Documents |
Upload: | austen-waters |
View: | 242 times |
Download: | 5 times |
1
G64ADSAdvanced Data Structures
Priority Queue
2
Why Priority Queue
o Queues are a standard mechanism for ordering tasks on a first-come first-served basiso However some tasks may be more important
or timely than others (higher priority)o Priority queues
o Store tasks using a partial ordering based on priorityo Ensure highest priority task at head of queue
o Heaps are the underlying data structure of priority queues
3
Priority Queue
o Main operations
o Insert (ie enqueue)o deleteMin (ie dequeue)
o Finds the minimum element in the queue deletes it from the queue and returns it
o Performance
o Goal is for operations to be fasto Will be able to achieve O(logN) time insertdeleteMin
amortized over multiple operationso Will be able to achieve O(1) time inserts amortized over
multiple insertions
4
Simple Implementations
o Unordered listo O(1) inserto O(N) deleteMin
o Ordered listo O(N) inserto O(1) deleteMin
o Balanced BSTo O(logN) insert and deleteMin
o Observation We donrsquot need to keep the priority queue completely ordered
5
Binary Heap
o A binary heap is a binary tree with two properties
oStructure propertyo A binary heap is a complete binary treeo Each level is completely filledo Bottom level may be partially filled from left to
right
o Height of a complete binary tree with N elements is Floor[logN]
6
Binary Heap Example
7
Binary Heap
o Heap-order property
o For every node X key(parent(X)) le key(X)o Except root node which has no parento Thus minimum key always at root
oOr maximum if you choose
o Insert and deleteMin must maintain heap-order property
8
Implementing Complete Binary Trees as Arrays
o Given element at position i in the arrayo irsquos left child is at position 2io irsquos right child is at position 2i+1o irsquos parent is at position Floor[i2]
9
Implementing Complete Binary Trees as Arrays
10
Heap Insert
o Insert new element into the heap at the next available slot (ldquoholerdquo)o According to maintaining a complete
binary tree
o Then ldquopercolaterdquo the element up the heap while heap-order property not satisfied
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
2
Why Priority Queue
o Queues are a standard mechanism for ordering tasks on a first-come first-served basiso However some tasks may be more important
or timely than others (higher priority)o Priority queues
o Store tasks using a partial ordering based on priorityo Ensure highest priority task at head of queue
o Heaps are the underlying data structure of priority queues
3
Priority Queue
o Main operations
o Insert (ie enqueue)o deleteMin (ie dequeue)
o Finds the minimum element in the queue deletes it from the queue and returns it
o Performance
o Goal is for operations to be fasto Will be able to achieve O(logN) time insertdeleteMin
amortized over multiple operationso Will be able to achieve O(1) time inserts amortized over
multiple insertions
4
Simple Implementations
o Unordered listo O(1) inserto O(N) deleteMin
o Ordered listo O(N) inserto O(1) deleteMin
o Balanced BSTo O(logN) insert and deleteMin
o Observation We donrsquot need to keep the priority queue completely ordered
5
Binary Heap
o A binary heap is a binary tree with two properties
oStructure propertyo A binary heap is a complete binary treeo Each level is completely filledo Bottom level may be partially filled from left to
right
o Height of a complete binary tree with N elements is Floor[logN]
6
Binary Heap Example
7
Binary Heap
o Heap-order property
o For every node X key(parent(X)) le key(X)o Except root node which has no parento Thus minimum key always at root
oOr maximum if you choose
o Insert and deleteMin must maintain heap-order property
8
Implementing Complete Binary Trees as Arrays
o Given element at position i in the arrayo irsquos left child is at position 2io irsquos right child is at position 2i+1o irsquos parent is at position Floor[i2]
9
Implementing Complete Binary Trees as Arrays
10
Heap Insert
o Insert new element into the heap at the next available slot (ldquoholerdquo)o According to maintaining a complete
binary tree
o Then ldquopercolaterdquo the element up the heap while heap-order property not satisfied
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
3
Priority Queue
o Main operations
o Insert (ie enqueue)o deleteMin (ie dequeue)
o Finds the minimum element in the queue deletes it from the queue and returns it
o Performance
o Goal is for operations to be fasto Will be able to achieve O(logN) time insertdeleteMin
amortized over multiple operationso Will be able to achieve O(1) time inserts amortized over
multiple insertions
4
Simple Implementations
o Unordered listo O(1) inserto O(N) deleteMin
o Ordered listo O(N) inserto O(1) deleteMin
o Balanced BSTo O(logN) insert and deleteMin
o Observation We donrsquot need to keep the priority queue completely ordered
5
Binary Heap
o A binary heap is a binary tree with two properties
oStructure propertyo A binary heap is a complete binary treeo Each level is completely filledo Bottom level may be partially filled from left to
right
o Height of a complete binary tree with N elements is Floor[logN]
6
Binary Heap Example
7
Binary Heap
o Heap-order property
o For every node X key(parent(X)) le key(X)o Except root node which has no parento Thus minimum key always at root
oOr maximum if you choose
o Insert and deleteMin must maintain heap-order property
8
Implementing Complete Binary Trees as Arrays
o Given element at position i in the arrayo irsquos left child is at position 2io irsquos right child is at position 2i+1o irsquos parent is at position Floor[i2]
9
Implementing Complete Binary Trees as Arrays
10
Heap Insert
o Insert new element into the heap at the next available slot (ldquoholerdquo)o According to maintaining a complete
binary tree
o Then ldquopercolaterdquo the element up the heap while heap-order property not satisfied
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
4
Simple Implementations
o Unordered listo O(1) inserto O(N) deleteMin
o Ordered listo O(N) inserto O(1) deleteMin
o Balanced BSTo O(logN) insert and deleteMin
o Observation We donrsquot need to keep the priority queue completely ordered
5
Binary Heap
o A binary heap is a binary tree with two properties
oStructure propertyo A binary heap is a complete binary treeo Each level is completely filledo Bottom level may be partially filled from left to
right
o Height of a complete binary tree with N elements is Floor[logN]
6
Binary Heap Example
7
Binary Heap
o Heap-order property
o For every node X key(parent(X)) le key(X)o Except root node which has no parento Thus minimum key always at root
oOr maximum if you choose
o Insert and deleteMin must maintain heap-order property
8
Implementing Complete Binary Trees as Arrays
o Given element at position i in the arrayo irsquos left child is at position 2io irsquos right child is at position 2i+1o irsquos parent is at position Floor[i2]
9
Implementing Complete Binary Trees as Arrays
10
Heap Insert
o Insert new element into the heap at the next available slot (ldquoholerdquo)o According to maintaining a complete
binary tree
o Then ldquopercolaterdquo the element up the heap while heap-order property not satisfied
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
5
Binary Heap
o A binary heap is a binary tree with two properties
oStructure propertyo A binary heap is a complete binary treeo Each level is completely filledo Bottom level may be partially filled from left to
right
o Height of a complete binary tree with N elements is Floor[logN]
6
Binary Heap Example
7
Binary Heap
o Heap-order property
o For every node X key(parent(X)) le key(X)o Except root node which has no parento Thus minimum key always at root
oOr maximum if you choose
o Insert and deleteMin must maintain heap-order property
8
Implementing Complete Binary Trees as Arrays
o Given element at position i in the arrayo irsquos left child is at position 2io irsquos right child is at position 2i+1o irsquos parent is at position Floor[i2]
9
Implementing Complete Binary Trees as Arrays
10
Heap Insert
o Insert new element into the heap at the next available slot (ldquoholerdquo)o According to maintaining a complete
binary tree
o Then ldquopercolaterdquo the element up the heap while heap-order property not satisfied
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
6
Binary Heap Example
7
Binary Heap
o Heap-order property
o For every node X key(parent(X)) le key(X)o Except root node which has no parento Thus minimum key always at root
oOr maximum if you choose
o Insert and deleteMin must maintain heap-order property
8
Implementing Complete Binary Trees as Arrays
o Given element at position i in the arrayo irsquos left child is at position 2io irsquos right child is at position 2i+1o irsquos parent is at position Floor[i2]
9
Implementing Complete Binary Trees as Arrays
10
Heap Insert
o Insert new element into the heap at the next available slot (ldquoholerdquo)o According to maintaining a complete
binary tree
o Then ldquopercolaterdquo the element up the heap while heap-order property not satisfied
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
7
Binary Heap
o Heap-order property
o For every node X key(parent(X)) le key(X)o Except root node which has no parento Thus minimum key always at root
oOr maximum if you choose
o Insert and deleteMin must maintain heap-order property
8
Implementing Complete Binary Trees as Arrays
o Given element at position i in the arrayo irsquos left child is at position 2io irsquos right child is at position 2i+1o irsquos parent is at position Floor[i2]
9
Implementing Complete Binary Trees as Arrays
10
Heap Insert
o Insert new element into the heap at the next available slot (ldquoholerdquo)o According to maintaining a complete
binary tree
o Then ldquopercolaterdquo the element up the heap while heap-order property not satisfied
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
8
Implementing Complete Binary Trees as Arrays
o Given element at position i in the arrayo irsquos left child is at position 2io irsquos right child is at position 2i+1o irsquos parent is at position Floor[i2]
9
Implementing Complete Binary Trees as Arrays
10
Heap Insert
o Insert new element into the heap at the next available slot (ldquoholerdquo)o According to maintaining a complete
binary tree
o Then ldquopercolaterdquo the element up the heap while heap-order property not satisfied
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
9
Implementing Complete Binary Trees as Arrays
10
Heap Insert
o Insert new element into the heap at the next available slot (ldquoholerdquo)o According to maintaining a complete
binary tree
o Then ldquopercolaterdquo the element up the heap while heap-order property not satisfied
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
10
Heap Insert
o Insert new element into the heap at the next available slot (ldquoholerdquo)o According to maintaining a complete
binary tree
o Then ldquopercolaterdquo the element up the heap while heap-order property not satisfied
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
11
Heap Insert
Insert 14
Creating the hole and building the hole up
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
12
Heap DeleteMin
o Minimum element is always at the root
o 1048708Heap decreases by one in sizeo 1048708Move last element into hole at root
o Percolate down while heap-order property not satisfied
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
13
Heap DeleteMin
Creation of the hole at the root
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
14
Heap DeleteMin
Next two steps in DeleteMin
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
15
Heap DeleteMin
Last two steps in DeleteMin
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
16
Heap DeleteMin Implementation
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
17
Other Heap Operations
o decreaseKey(pv)o Lowers value of item p to vo Need to percolate upo eg change job priority
o increaseKey(pv)o Increases value of item p to vo Need to percolate down
o remove(p)o First decreaseKey(p-infin)o Then deleteMino eg terminate job
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
18
Building a Heap
o Construct heap from initial set of N items
o Solution 1oPerform N insertsoO(N) average case but O(NlogN) worst-case
o Solution 2oAssume initial set is a heapoPerform a percolate-down from each internal
node (H[size2] to H[1])
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
19
Building a Heap Example
Initial heap After percolateDown(7)
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
20
Building a Heap Example
After percolateDown(6) After percolateDown(5)
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
21
Building a Heap Example
After percolateDown(4) After percolateDown(3)
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
22
Building a Heap Example
After percolateDown(2) After percolateDown(1)
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
23
BuildingHeap Implementation
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
24
BuildHeap Analysis
o Running time of buildHeap proportional to sum of the heights of the nodes
o Theorem 61o For the perfect binary tree of height h containing
2h+1ndash1 nodes the sum of heights of the nodes is 2h+1ndash1 ndash(h + 1)
o Since N = 2h+1ndash1 then sum of heights is O(N)
o Slightly better for complete binary tree
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
25
Binary Heap Operations Worst-case Analysis
o Height of heap is Floor[logN]o insert O(logN)o2607 comparisons on average ie O(1)
o deleteMin O(logN)o decreaseKey O(logN)o increaseKey O(logN)o remove O(logN)o buildHeap O(N)
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
26
Applications
o Operating system schedulingoProcess jobs by priority
o Graph algorithmsoFind the least-cost neighboring vertex
o Event simulationo Instead of checking for events at each time
click look up next event to happen
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
27
Priority Queues Alternatives to Binary Heaps
o d-Heapo Each node has d childreno insert in O(logdN) timeo deleteMin in O(d logdN) timeo Binary heaps are 2-Heaps
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
28
Mergeable Heaps
o Heap merge operationo Useful for many applicationso Merge two (or more) heaps into oneo Identify new minimum elemento Maintain heap-order propertyo Merge in O(log N) time
o Still support insert and deleteMin in O(log N) timeo Insert = merge existing heap with one-element heap
o d-Heaps require O(N) time to merge
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
29
Leftist Heaps
o Null path length npl(X) of node Xo Length of the shortest path from X to a node
without two children
o Leftist heap propertyo For every node X in heap npl(leftChild(X)) ge
npl(rightChild(X))
o Leftist heaps have deep left subtrees and shallow right subtreeso Thus if operations reside in right subtree they
will be faster
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
30
Leftist Heaps
Leftist heapNot a leftist heap
npl(X) shown in nodes
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
31
Leftist Heaps
o Theorem 62o A leftist tree with r nodes on the right
path must have at least 2r ndash 1 nodes
o Thus a leftist tree with N nodes has a right path with at most Floor[log(N+1)] nodes
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
32
Leftist Heaps
o Merge heaps H1 and H2o Assume root(H1) gt root(H2)o Recursively merge H1 with right
subheap of H2o If result is not leftist then swap the left
and right subheapso Running time O(log N)
o DeleteMino Delete root and merge children
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
33
Leftist Heaps Example
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
34
Leftist Heaps Example
Result of merging H2 with H1rsquos right subheap
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
35
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
36
Leftist Heaps Example
Result of attaching leftist heap of previous figure as H1rsquos right child
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
37
Leftist Heaps Example
Result of swapping children of H1rsquos root
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
38
Skew Heaps
o Self-adjusting version of leftist heap
o Skew heaps are to leftist heaps as splay trees are to AVL trees
o Skew merge same as leftist merge except we always swap left and right subheaps
o Worst case is O(N)
o Amortized cost of M operations is O(M log N)
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
39
Binomial Queues
o Support all three operations in O(log N) worst-case time per operation
o Insertions take O(1) average-case time
o Key idea
oKeep a collection of heap-ordered trees to postpone merging
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
40
Binomial Queues
o A binomial queue is a forest of binomial trees
o Each in heap ordero Each of a different height
o A binomial tree Bk of height k consists two Bk-
1 binomial trees
o The root of one Bk-1tree is the child of the root of the other Bk-1tree
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
41
Binomial Trees
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
42
Binomial Trees
o Binomial trees of height k have exactly 2k nodeso Number of nodes at depth d is the
binomial coefficient
o A priority queue of any size can be represented by a binomial queue
o Binary representation of Bk
d
k
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
43
Binomial Queue Operations
o Minimum element found by checking roots of all trees
o At most (log2N) of them thus O(log N)
o Or O(1) by maintaining pointer to minimum element
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
44
Binomial Queue Operations
o Merge (H1H2) rarrH3o Add trees of H1 and H2 into H3 in increasing
order by deptho Traverse H3
o If find two consecutive Bk trees then create a Bk+1treeo If three consecutive Bk trees then leave first
combine last twoo Never more than three consecutive Bk trees
o Keep binomial trees ordered by heighto min(H3) = min(min(H1)min(H2))o Running time O(log N)
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
45
Binomial Queue Merge Example
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
46
Binomial Queue Operations
o Insert (x H1)o Create single-element queue H2o Merge (H1 H2)
o Running time proportional to minimum k such that Bk not in heap
o O(log N) worst caseo Probability Bk not present is 05
o Thus likely to find empty Bk after two tries on average
o O(1) average case
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
47
Binomial Queue Operations
o deleteMin (H1)o Remove min(H1) tree from H1o Create heap H2 from the children of
min(H)o Merge (H1H2)
o Running time O(log N)
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
48
Binomial Queue Delete Example
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
49
Binomial Queue Implementation
o Array of binomial treeso Trees use first-child right-sibling
representation
50
Priority Queue in Standard Library
51
Summary
50
Priority Queue in Standard Library
51
Summary
51
Summary