Professional Documents
Culture Documents
ADS- RV 4
Can we build a data structure better suited to store and retrieve priorities?
Simple Implementations
Unordered linked list 5 2 10 … 3
O(1) insert
O(n) deleteMin
Ordered linked list 2 3 5 … 10
O(n) insert
O(1) deleteMin
Ordered array 2 3 5 … 10
O(n) insert
O(n) deleteMin
Balanced BST
O(log2n) insert and deleteMin
ADS- RV 6
Binary Heap
A priority queue data structure
A binary heap is a binary tree with two
properties
Structure property
Heap-order property
Binary Heap operations
Insert, deleteMin, IncreaseKey, DecreaseKey,
ADS- RV 7
Structure Property
A binary heap is a complete binary tree
Each level (except possibly the bottom most level) is
completely filled
The bottom most level may be partially filled (from left to
right)
ADS- RV 8
Binary Heap Example
N=10
Every level
(except last)
saturated
Array representation:
ADS- RV 9
Heap-order Property
Heap-order property (for a “MinHeap”)
For every node X, key(parent(X)) ≤ key(X)
Except root node, which has no parent
Thus, minimum key always at root
Alternatively, for a “MaxHeap”, always
keep the maximum key at the root
Insert and deleteMin must maintain
heap-order property
ADS- RV 10
Heap Order Property
Minimum
element
ADS- RV 11
Implementing Complete
Binary Trees as Arrays
Given element at position i in the array
Left child(i) = at position 2i
Right child(i) = at position 2i + 1
Parent(i) = at position i / 2
i
2i
2i + 1
i/2
ADS- RV 12
Heap Insert
Insert new element into the heap at the
next available slot (“hole”)
According to maintaining a complete binary
tree
Then, “percolate” the element up the
heap while heap-order property not
satisfied
ADS- RV 13
Percolating Up
14 hole
ADS- RV 14
Percolating Up
14
14 hole
ADS- RV 15
Percolating Up
14
14 hole
(2)
14 vs. 21
14
ADS- RV 16
Percolating Up
14 hole
(2)
14 vs. 21
Path of percolation up
ADS- RV 17
Just finds the Min
without deleting it
insert
deleteMin
ADS- RV 18
Heap Insert: Implementation
O(log N) time
ADS- RV 19
Heap DeleteMin
Minimum element is always at the root
Heap decreases by one in size
Move last element into hole at root
Percolate down while heap-order
property not satisfied
ADS- RV 20
Percolating down…
Make this
position
empty
ADS- RV 21
Percolating down…
Make this
position Is 31 > min(14,16)?
• Yes - swap 31 with min(14,16)
empty
ADS- RV 22
Percolating down…
31
Is 31 > min(19,21)?
• Yes - swap 31 with min(19,21)
ADS- RV 23
Percolating down…
31
31
Percolating down…
ADS- RV 24
Percolating down…
31
Percolating down…
ADS- RV 25
Percolating down…
31
ADS- RV 26
Heap DeleteMin:
Implementation
O(log N) time
ADS- RV 27
Percolate
down
Left child
Right child
Pick child to
swap with
ADS- RV 28
Other Heap Operations
decreaseKey(p,v) Spec: decrement element
Lowers value of item p to v pointed to by p to
Need to percolate up new priority value v
E.g., promote a job
increaseKey(p,v)
Increases value of item p to v
Need to percolate down
E.g., demote a job
remove(p) Run-times for all three functions?
First, decreaseKey(p,-∞)
Then, deleteMin O(lg n)
E.g., abort/cancel a job
ADS- RV 29
Building a Heap
Construct heap from initial set of N items
Solution 1
Perform N inserts
O(N log2 N) worst-case
Solution 2 (use buildHeap())
Randomly populate initial heap with structure property
Perform a percolate-down from each internal node
(H[size/2] to H[1])
To take care of heap order property
ADS- RV 31
BuildHeap Example
Input: { 150, 80, 40, 10, 70, 110, 30, 120, 140, 60, 50, 130, 100, 20, 90 }
ADS- RV 32
BuildHeap Example
Swap
with left
Nothing child
to do
ADS- RV 34
Swap with
BuildHeap Example
right child
& then with 60
Nothing
to do
ADS- RV 35
BuildHeap Example
Swap path
Final Heap
ADS- RV 36
BuildHeap Implementation
Start with
lowest,
rightmost
internal node
ADS- RV 37
Binary Heap Operations
Worst-case Analysis
Height of heap is log 2 N
insert: O(lg N) for each insert
In practice, expect less
buildHeap insert: O(N) for N inserts
deleteMin: O(lg N)
decreaseKey: O(lg N)
increaseKey: O(lg N)
remove: O(lg N)
ADS- RV 39
Other Types of Heaps
Binomial Heaps
d-Heaps
Generalization of binary heaps (ie., 2-Heaps)
Leftist Heaps
Supports merging of two heaps in o(m+n) time
(ie., sub-linear)
Skew Heaps
O(log n) amortized run-time
Fibonacci Heaps
ADS- RV 41
D- heaps
Generalization of binary heap.
Except that d-heap has exactly d children. (thus a binary heap
is a 2- heap).
Merge operation on two heaps are very expensive.
Fig shows the 3-heap.
ADS- RV 42
Leftist Heaps / Merge able Heaps
A binary heap provides O(log n) inserts and O(log n)
deletes but suffers from O(n log n) merges
A leftist heap offers O(log n) inserts and O(log n) deletes
and O(log n) merges
Note, however, leftist heap inserts and deletes are more
expensive than Binary Heap inserts and deletes. But
provides easy way for MERGING.
Leftist heaps satisfies both structural property and heap
order property.
But not perfectly balanced, but attempt to be very
unbalanced.
ADS- RV 43
Leftist Heaps
A Leftist (min)Heap is a binary tree that satisfies
the following conditions. If X is a node and L and R
are its left and right children, then:
1. X.value ≤ L.value
2. X.value ≤ R.value
3. null path length of L ≥ null path length of R
8 19
12 27 20
15 25 43
node 4 8 19 12 15 25 27 20 43
npl 1 0 1 1 0 0 0 0 0
Identify the leftist heap
Theorem:
A leftist heap with r nodes on the right path must have alteast 2r-1 nodes.
8 0 19 1
What is the npl of the right
child of 8?
12 1
27 0 20 0
What are the npl’s of the
children of 20 and the right
15 0 25 0 43 0 child of 27?
node 4 8 19 12 15 25 27 20 43
npl 1 0 1 1 0 0 0 0 0
Example: Null Path Length
• node 4 violates leftist heap
4 property fix by swapping
children
8 0 19 1
12 1
27 0 20 0
15 0 25 0 43 0
node 4 8 19 12 15 25 27 20 43
npl 1 0 1 1 0 0 0 0 0
Leftist Heap
4
19 1 8 0
27 0 20 0 12 1
43 0
15 0 25 0
node 4 8 19 12 15 25 27 20 43
npl 1 0 1 1 0 0 0 0 0
Merging Leftist Heaps
Consider two leftist heaps …
4 6
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
First, instantiate a
Stack
Merging Leftist Heaps
x y
4 6
19 8 8 7
27 20 12 14
43 15 25
19 8 8 7
27 20 12 14
43 15 25
4
Remember smaller
value
Merging Leftist Heaps
y
4 x 6
19 8 8 7
27 20 12 14
43 15 25
4
Repeat the process with the
right child of the smaller value
Merging Leftist Heaps
y
4 x 6
19 8 8 7
27 20 12 14
43 15 25
6
Remember smaller 4
value
Merging Leftist Heaps
4 x 6
y
19 8 8 7
27 20 12 14
43 15 25
6
Repeat the process with the 4
right child of the smaller
value
Merging Leftist Heaps
4 x 6
y
19 8 8 7
27 20 12 14
43 15 25
7
6
Remember smaller 4
value
Merging Leftist Heaps
4 x 6
19 8 8 7
y
27 20 12 14
null
43 15 25
7
6
Repeat the process with the 4
right child of the smaller
value
Merging Leftist Heaps
4 x 6
19 8 8 7
27 20 12 14
43 15 25
8
7
6
Because one of the 4
arguments is null, return the
other argument
Merging Leftist Heaps
4 6
19 Refers to node 8 7 x
27 20 8 14 8
43 12
8
15 25
7
6
of 7
Merging Leftist Heaps
4 6
19 Refers to node 8 7
27 20 8 14 8
43 12
8
15 25
7
6
Make 7 leftist (by swapping 4
children)
Merging Leftist Heaps
4 6
19 Refers to node 8 7
27 20 8 14 8
43 12
15 25
7
6
Return node 7 4
Merging Leftist Heaps
4 6
19 Refers to node 8 7
27 20 8 14 8
43 12
15 25
7
6
Make 7 the right child of 4
6 (which it already is)
Merging Leftist Heaps
4 6
19 Refers to node 8 7
27 20 8 14 8
43 12
15 25
7
6
Make 6 leftist (it 4
already is)
Merging Leftist Heaps
4 6
19 Refers to node 8 7
27 20 8 14 8
43 12
15 25
6
Return node 6 4
Merging Leftist Heaps
4
19 6
27 20 8 7
43 14 8
12
15 25
6
4
Make 6 the right child
of 4
Merging Leftist Heaps
4
19 6
27 20 8 7
43 14 8
12
15 25
6
4
Make 4 leftist (it already is)
Final Leftist Heap
4
19 6
27 20 8 7
43 14 8
12
15 25
Node 3 10 8 21 14 17 23 26
npl 1 1 0 0 0 0 0 0
ADS- RV 69
Example 2
ADS- RV 70
Example 2
ADS- RV 71
Final leftist heap
ADS- RV 72
Leftist heaps Operations
The Fundamental operation on leftist heaps is merging.
(insertion is also a special case of merging).
We can carry out insertions by making the item to be
inserted a one item heap and performing a merge.
deleteMin – we destroy the root, creating two heaps,
which can then be merged.
deleteMin takes O(log N)
ADS- RV 73
Analysis
Height of a leftist heap ≈ O(log n)
Maximum number of values stored in
Stack ≈ 2 * O(log n) ≈ O(log n)
Total cost of merge ≈ O(log n)
Run-time Per Operation
Insert DeleteMin Merge (=H1+H2)
buildHeap
Leftist Heap O(log n) O(log n) O(log n)
sequence of n inserts
ADS- RV 77
Binomial Heap
A binomial heap is a forest of heap-ordered
binomial trees, satisfying:
i) Structure property, and
ii) Heap order property
ADS- RV 78
Note: A binomial tree need not be a binary tree
d=0 ( 03 )
d=1 ( 13 )
d=2 ( 23 )
d=3 ( 33 )
ADS- RV 79
What will a Binomial Heap with n=31
nodes look like?
We know that:
i) A binomial heap should be a forest of binomial
trees
ii) Each binomial tree has power of 2 elements
So how many binomial trees do we need?
B4 B3 B2 B1 B0
n = 31 = (1 1 1 1 1)2
ADS- RV 80
A Binomial Heap w/ n=31 nodes
B4 B3 B2 B1 B0
Bi == Bi-1 + Bi-1
n = 31 = (1 1 1 1 1)2
Forest of binomial trees {B0, B1, B2, B3, B4 }
B0
B1
B3 B2
ADS- RV 81
Binomial Heap Property
Lemma: There exists a binomial heap for every
positive value of n
Proof:
All values of n can be represented in binary representation
Have one binomial tree for each power of two with co-efficient
of 1
Eg., n=10 ==> (1010)2 ==> forest contains {B3, B1}
ADS- RV 82
Binomial Heaps: Heap-Order
Property
Each binomial tree should contain the
minimum element at the root of every
subtree
Just like binary heap, except that the tree
here is a binomial tree structure (and not a
complete binary tree)
ADS- RV 84
Binomial Heaps: Examples
ADS- RV 85
Key Properties
Could there be multiple trees of the same height in a
binomial heap?
no
Insert(x)
Merge(H1, H2)
ADS- RV 88
DeleteMin()
Goal: Given a binomial heap, H, find the
minimum and delete it
Observation: The root of each binomial tree
in H contains its minimum element
Approach: Therefore, return the minimum of
all the roots (minimums)
Complexity: O(log n) comparisons
(because there are only O(log n) trees)
ADS- RV 89
FindMin() & DeleteMin() Example
B0 B2 B3
B0’ B1’ B2 ’
ADS- RV 90
Insert(x) in Binomial Heap
Goal: To insert a new element x into a
binomial heap H
Observation:
Element x can be viewed as a single
element binomial heap
=> Insert (H,x) == Merge(H, {x})
Logic:
Merge trees of same height, starting from lowest height
trees
If only one tree of a given height, then just copy that
Otherwise, need to do carryover (just like adding two binary
numbers)
ADS- RV 92
Idea: merge tree of same heights
Merge: Example
+
py
co
B0 B1 B2
13
? ?
ADS- RV 93
How to Merge Two Binomial
Trees of the Same Height?
B2: B3:
B2:
+
Note: Merge is defined for only binomial trees with the same height
ADS- RV 94
Merge(H1,H2) example
carryover
13 14 ?
26 16
26
ADS- RV 95
How to Merge more than two
binomial trees of the same height?
Merging more than 2 binomial trees of
the same height could generate carry-
overs
14
+ +
26 16 ?
26
ADS- RV 96
Input:
Merge(H1,H2) : Example
Output:
ADS- RV 97
Run-time Complexities
Merge takes O(log n) comparisons
Corollary:
Insert and DeleteMin also take O(log n)
ADS- RV 99
Run-time Per Operation
Insert DeleteMin Merge (=H1+H2)
buildHeap
Leftist Heap O(log n) O(log n) O(log n)
sequence of n inserts
ADS- RV 101