Title: Chapter 6 Priority Queues Heaps
1Chapter 6 Priority Queues (Heaps)
2Motivation
- Have you ever been jammed by a huge job while you
are waiting for just one-page printout ? - This is a typical situation for a simple first-in
first-out (FIFO) queue - Can there be a smarter printer (multi-user
computing)?
36.1 Model
Priority Queue H
Insert(H)
DeleteMin(H)
Function level
46.2 Simple Implementations
- There are multiple possibilities for the
implementation. - Simple linked list (Suggestion 1)
- insert in front O(1)
- delete minimum O(N)
- Sorted linked list (Suggestion 2)
- insert O(N)
- delete minimum O(1)
56.2 Simple Implementations
- Binary search tree (Suggestion 3)
- O (log N) on average for both operations
- Binary heap (Suggestion of this chapter)
- O (1) for finding the minimum
- O (log N) for insert but O(1) on average
- Complexity for building a priority queue
- O(N) on average and O(N logN) for worst case
(Why?)
66.3 Binary Heap (Heap)
- (A) Structure Property
- A heap is a binary tree that is completely
filled, except at the bottom level, which is
filled from left to right. - A complete binary tree of height h has between 2h
and 2h1 - 1 nodes - The height of a complete binary tree ?log N?
(round down, e.g. ?2.7? 2).
76.3 Binary Heap (Heap)
- A complete binary tree can be represented in an
array without using pointers.
8- The root is at position 1 (reserve position 0
for a sentinel - MinData). - For an element at position i, its left child is
at position 2i and its it right child at 2i 1
its parent is at floor ?i/2?.
96.3 Binary Heap
- (B) Heap Order Property
- The value at any node should be smaller than all
of its descendants (guarantee that the node with
the minimum value is at the root).
not a heap !!
10A Heap
A Binary Search Tree
Note the difference in node ordering!!
116.3 Binary Heap
Definitions
struct HeapStruct int Capacity / maximum
size / int Size / actual size /
ElementType Elements typedef struct
HeapStruct PriorityQueue
126.3 Binary Heap
Some functions
PriorityQueue Initialize (int MaxElements)
void Destroy (PriorityQueue H) void MakeEmpty
(PriorityQueue H) void Insert (ElementType X,
PriorityQueue H) ElementType DeleteMin
(PriorityQueue H) ElementType FindMin
(PriorityQueue H) int IsEmpty (PriorityQueue
H) int IsFull (PriorityQueue H)
136.3 Binary Heap - Initialize
/ Fig 6.4
/ PriorityQueue Initialize (int MaxElements)
PriorityQueue H if (MaxElements lt MinPQSize)
Error ("Priority queue size is too small") H
malloc (sizeof (struct HeapStruct)) if (H
NULL) FatalError ("Out of space!!!")
146.3 Binary Heap - Initialize
/ Allocate the array plus one extra for
sentinel / H-gtElements malloc ((MaxElements
1) sizeof (ElementType))
if (H-gtElements NULL) FatalError ("Out of
space!!!") H-gtCapacity MaxElements
H-gtSize 0 H-gtElements 0 MinData
return H
156.3 Binary Heap - Insert
166.3 Binary Heap - Insert
176.3 Binary Heap - Insert
- To insert an element X,
- create a hole in the next available location.
- If X can be placed in the hole without violating
heap order, insertion is complete. - Otherwise slide the element that is in the holes
parent node into the hole, i.e., bubbling the
hole up towards the root. - Continue this process until X can be placed in
the hole (a percolating up process). - Worst case running time is O (log N) - the new
element is percolating up all the way to the root.
186.3 Binary Heap - Insert
/ H-gtElement 0 is a sentinel / void Insert
(ElementType X, PriorityQueue H) int i
if (IsFull (H)) Error ("Priority queue
is full") return for (i
H-gtSize H-gtElements i / 2 gt X i / 2 )
H-gtElements i H-gtElements i / 2
H-gtElements i X
196.3 Binary Heap - Insert
206.3 Binary Heap - DeleteMin
216.3 Binary Heap - DeleteMin
31
226.3 Binary Heap - DeleteMin
236.3 Binary Heap - DeleteMin
- The element at the root (position 1) is to be
removed, and a hole is created. - If X is smaller than the child(ren), job is done.
- Otherwise slide the smaller of the holes
children into the hole, thus pushing the hole
down one level.
246.3 Binary Heap - DeleteMin
- Repeat the previous step until X can be placed in
the hole (percolating down). - Some node may have only one child (be careful
when coding!). - Worst case running time is O (log N).
- On average, the hole is percolated almost to the
bottom of the heap, so the average running time
is O (log N) again.
256.3 Binary Heap - DeleteMin
/ Fig. 6.12 / ElementType DeleteMin
(PriorityQueue H) int i, Child
ElementType MinElement, LastElement if
(IsEmpty (H)) Error ("Priority queue
is empty") return H-gtElements 0
MinElement H-gtElements 1 LastElement
H-gtElements H-gtSize--
266.3 Binary Heap - DeleteMin
for (i 1 i 2 lt H-gtSize i Child)
/ Find smaller child / Child i 2
if ( Child ! H-gtSize H-gtElementsChild 1 lt
H-gtElementsChild ) Child /
Percolate one level / if (LastElement gt
H-gtElements Child) H-gtElements i
H-gtElements Child else break
H-gtElements i LastElement return
MinElement
276.3 Binary Heap - Other Heap Operations
- There is no way to find any particular key
without a linear scan through the entire heap. - However, if we know the position, we can access
the key immediately.
286.3 Binary Heap - Other Heap Operations
- DecreaseKey (P, ?, H)
- Lower the key value at position P by ?.
- Fix the heap order by percolating up.
- Advance the priority of a job.
- Delete (P, H)
- Remove the node at position P.
- DecreaseKey (P, ?, H) and DeleteMin (H)
296.3 Binary Heap - Other Heap Operations
- BuildHeap (H)
- Given N unordered keys, how to build a heap?
- N successive appends at the end of the array,
each takes O (1) average and O(logN) worst-case
time. - PercolateDown (i), for i N/2 to 1.
306.3 Binary Heap - Other Heap Operations
316.3 Binary Heap - Other Heap Operations
326.3 Binary Heap - Other Heap Operations
336.3 Binary Heap - Other Heap Operations
346.3 Binary Heap - Other Heap Operations
356.3 Binary Heap - Other Heap Operations
366.3 Binary Heap - Other Heap Operations
376.3 Binary Heap - Other Heap Operations
386.4 Applications of Priority Queues
- Find the kth smallest elements
- It requires k DeleteMin operations.
- O (N) to create the heap.
- O (log N) for each DeleteMin.
- Total running time is O (N k log N).
- If k O (N/log N), running time is O (N).
- For large value of k, running time is O (k log N).
396.4 Applications of Priority Queues
- Discrete Event Simulation
- E.g., bank waiting line
- Given
- customers interarrival distribution
- number of tellers (server)
- arriving customer joins the shortest queue
- customers are served on FIFO within the queue
- no queue switching
- service time (transaction time) distribution
406.4 Applications of Priority Queues
- Statistics required
- average waiting time
- average banking time (waiting time service
time) - maximum waiting time, banking time, queue length
- Generate service time of each customer
- Generate arrival time of each customer (arrival
time of current customer time interval for the
next customer to come) - One customer queue for each teller
416.4 Applications of Priority Queues
- Event queue with 2 types of events (in event
occurrence sequence) - customer arrival
- complete of service of one customer
- At customer arrival event
- generate service time for this customer
- insert the customer into the end of the shortest
teller queue
426.4 Applications of Priority Queues
- generate interarrival time and then compute
arrival time of the next customer - using the arrival time of the next customer,
generate an arrival event and insert it into the
event queue (not necessarily the last in the
queue) - At service completion event
- remove the customer from the teller queue
- compute relevant statistics for this customer
436.4 Applications of Priority Queues
- if this teller queue is not empty, serve the next
customer in the queue - compute the service completion time (current time
service time) - generate service completion event, and insert it
into the event queue
446.4 Applications of Priority Queues
456.4 Applications of Priority Queues
Departure time Arrival time Waiting
time Service time
466.4 Applications of Priority Queues
Approach Using a heap
Check the next arrival
DeleteMin if arrival event
Insert arrival event
Y, compute the departure time insert the
departure event
server available?
N, go into the queue
Event Queue (Heap)
FIFO Teller Queue
476.4 Applications of Priority Queues
Approach Using a heap
DeleteMin if departure event
Queue Empty ?
Y, go to next DeleteMin
N, remove one from the queue, compute the
departure time insert as a
departure event
Event Queue (Heap)
FIFO Teller Queue
486.4 Applications of Priority Queues
- Given N customers, How many events altogether?
- What is the running (CPU) time for each event?
- What is the overall complexity?