CS210- Lecture 14 July 5, 2005 

advertisement
CS210- Lecture 14
July 5, 2005
Agenda
 Inserting into Heap
 Removing from Heap
 Heap Sort
 Heap Construction
 Top down Heap Construction
 Bottom up Heap Construction
 Adaptable PQ
6/30/2016
CS210-Summer 2005, Lecture 14
1
Heaps



An efficient realization of a priority
queue uses a data structure called a
heap.
Heap allows us to perform both
insertions and removals in logarithmic
time.
Heap stores entries in a binary tree
rather than in a list.
6/30/2016
CS210-Summer 2005, Lecture 14
2
Heaps and Priority Queues



We can use a heap to implement a priority
queue
We store a (key, element) item at each node.
We keep track of the position of the last
node
(2, Sue)
(5, Pat)
(9, Jeff)
6/30/2016
(6, Mark)
(7, Anna)
CS210-Summer 2005, Lecture 14
3
Insertion into a Heap


(2, C)
Method insert of the
priority queue ADT
(5, A)
(6, Z)
z
corresponds to the
(15,K)
(9, F)
insertion of an entry
(k,x) to the heap
insertion node
The insertion algorithm
(2, C)
consists of three steps



Add a new node z (the
new last node)
Store e at z
Restore the heap-order
property (discussed next)
6/30/2016
(5, A)
(15,K)
CS210-Summer 2005, Lecture 14
(6, Z)
(9, F)
z
(1, D)
4
Upheap




After the insertion of a new entry e, the heap-order property may
be violated
Algorithm upheap restores the heap-order property by swapping e
along an upward path from the insertion node
Upheap terminates when the entry e reaches the root or a node
whose parent has a key smaller than or equal to key k of e.
Since a heap has height O(log n), upheap runs in O(log n) time
(2, C)
(5, A)
(15,K)
6/30/2016
(1, D)
(1,D)
(9, F) z (6, Z)
(5, A)
(2, C)
z
(15,K)
(9, F)
CS210-Summer 2005, Lecture 14
(6, Z)
5
Removal from a Heap


Method removeMin of
the priority queue ADT
corresponds to the
removal of the root
entry from the heap
The removal algorithm
consists of three steps



Replace the root entry
with the entry of the last
node w
Remove w
Restore the heap-order
property (discussed next)
6/30/2016
(2, C)
(5, A)
(15,K)
(6, Z)
w
(9, F)
last node
(9, F)
(5, A)
(6, Z)
w
(15,K)
new last node
CS210-Summer 2005, Lecture 14
6
Downheap




After replacing the root entry with the entry e of the last node,
the heap-order property may be violated
Algorithm downheap restores the heap-order property by
swapping entry e along a downward path from the root
Upheap terminates when key k of entry e reaches a leaf or a node
whose children have keys greater than or equal to k
Since a heap has height O(log n), downheap runs in O(log n) time
(9, F)
(5, A)
(15,K)
6/30/2016
(5, A)
(6, Z)
(9, F)
w
(15,K)
CS210-Summer 2005, Lecture 14
(6, Z)
w
7
Heap-Sort

Consider a priority
queue with n items
implemented by means
of a heap


methods insert and
removeMin take O(log n)
time
methods size, isEmpty,
and min take time O(1)
time
6/30/2016



Using a heap-based
priority queue, we can
sort a sequence of n
elements in O(n log n)
time
The resulting algorithm
is called heap-sort
Heap-sort is much
faster than quadratic
sorting algorithms, such
as insertion-sort and
selection-sort
CS210-Summer 2005, Lecture 14
8
Heap Construction


We can construct a heap storing n
entries in O(nlogn) time, by means of n
successive insert operations.
However if all the n key-value pairs to
be stored in the heap are given in
advance, there is an alternative bottom
up construction method that runs in
O(n) time.
6/30/2016
CS210-Summer 2005, Lecture 14
9
Bottom up Heap Construction
16 15
4
12
6
7
23 20 25 5
11 27
9
8
14
16
4
15
12
20
6/30/2016
6
25
5
7
11
27
23
9
CS210-Summer 2005, Lecture 14
8
14
10
Bottom up Heap Construction
16
4
15
12
6
20
25
16 15
4
6/30/2016
5
12
6
7
11
7
8
27
20 25 5
8
9
11 27
CS210-Summer 2005, Lecture 14
23
9
14
23 14
11
Bottom up Heap Construction
16
4
15
12
6
20
25
16 15
4
6/30/2016
5
12
6
7
11
7
8
27
20 25 5
8
9
11 27
CS210-Summer 2005, Lecture 14
23
9
14
23 14
12
Bottom up Heap Construction
16
4
15
12
5
20
25
16 15
4
6/30/2016
6
12
5
7
11
7
8
27
20 25 6
8
9
11 27
CS210-Summer 2005, Lecture 14
23
9
14
23 14
13
Bottom up Heap Construction
16
4
15
12
5
20
25
16 15
4
6/30/2016
6
12
5
7
11
7
8
27
20 25 6
8
9
11 27
CS210-Summer 2005, Lecture 14
23
9
14
23 14
14
Bottom up Heap Construction
16
4
15
12
5
20
25
16 15
4
6/30/2016
6
12
5
7
11
7
8
27
20 25 6
8
9
11 27
CS210-Summer 2005, Lecture 14
23
9
14
23 14
15
Bottom up Heap Construction
16
4
5
12
20
16
6
25
5
6/30/2016
4
15
12
6
7
11
7
8
27
8
9
20 25 15 11 27
CS210-Summer 2005, Lecture 14
23
9
14
23 14
16
Bottom up Heap Construction
4
7
5
12
20
16
6
25
5
6/30/2016
4
15
12
6
9
11
7
8
27
8
16
20 25 15 11 27
CS210-Summer 2005, Lecture 14
23
9
14
23 14
17
Adaptable Priority Queues

Suppose we have an online trading system where
orders to purchase and sell a given stock are stored
in two priority queues (one for sell orders and one for
buy orders) as (p,s) entries:






The key, p, of an order is the price
The value, s, for an entry is the number of shares
A buy order (p,s) is executed when a sell order (p’,s’) with
price p’<p is added (the execution is complete if s’>s)
A sell order (p,s) is executed when a buy order (p’,s’) with
price p’>p is added
What if someone wishes to cancel their order before
it executes?
What if someone wishes to update the price or
number of shares for their order?
6/30/2016
CS210-Summer 2005, Lecture 14
18
Methods of the Adaptable PQ ADT



remove(e): Remove and return entry e.
replaceKey(e,k): Replace with k and
return the key of entry e of P; an error
condition occurs if k is invalid (that is, k
cannot be compared with other keys).
replaceValue(e,x): Replace with x and
return the value of entry e of P.
6/30/2016
CS210-Summer 2005, Lecture 14
19
Example
Operation
insert(5,A)
insert(3,B)
insert(7,C)
min()
key(e2)
remove(e1)
replaceKey(e2,9)
replaceValue(e3,D)
remove(e2)
6/30/2016
Output
e1
e2
e3
e2
3
e1
3
C
e2
P
(5,A)
(3,B),(5,A)
(3,B),(5,A),(7,C)
(3,B),(5,A),(7,C)
(3,B),(5,A),(7,C)
(3,B),(7,C)
(7,C),(9,B)
(7,D),(9,B)
(7,D)
CS210-Summer 2005, Lecture 14
20
Locating Entries


In order to implement the operations
remove(k), replaceKey(e), and
replaceValue(k), we need fast ways of
locating an entry e in a priority queue.
We can always just search the entire
data structure to find an entry e, but
there are better ways for locating
entries.
6/30/2016
CS210-Summer 2005, Lecture 14
21
Location-Aware Entries


A locator-aware entry identifies and
tracks the location of its (key, value)
object within a data structure
Main idea:

Since entries are created and returned
from the data structure itself, it can return
location-aware entries, thereby making
future updates easier
6/30/2016
CS210-Summer 2005, Lecture 14
22
List Implementation

A location-aware list entry is an object storing




key
value
position (or rank) of the item in the list
In turn, the position (or array cell) stores the entry
nodes/positions
header
2 c
4 c
5 c
trailer
8 c
entries
6/30/2016
CS210-Summer 2005, Lecture 14
23
Heap Implementation

A location-aware
heap entry is an
object storing





2 d
key
value
position of the entry
in the underlying
heap
In turn, each heap
position stores an
entry
Back pointers are
updated during
entry swaps
6/30/2016
4 a
8 g
6 b
5 e
CS210-Summer 2005, Lecture 14
9 c
24
Performance

Using location-aware entries we can achieve
the following running times:
Method
Unsorted List
size, isEmpty
O(1)
insert
O(1)
min
O(n)
removeMin
O(n)
remove
O(1)
replaceKey
O(1)
replaceValue
O(1)
6/30/2016
Sorted List
O(1)
O(n)
O(1)
O(1)
O(1)
O(n)
O(1)
CS210-Summer 2005, Lecture 14
Heap
O(1)
O(log n)
O(1)
O(log n)
O(log n)
O(log n)
O(1)
25
Download