Chapter 6 Priority Queues Heaps - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Chapter 6 Priority Queues Heaps

Description:

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). 39. 6.4 Applications of ... – PowerPoint PPT presentation

Number of Views:146
Avg rating:3.0/5.0
Slides: 49
Provided by: CSD5151
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6 Priority Queues Heaps


1
Chapter 6 Priority Queues (Heaps)
2
Motivation
  • 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)?

3
6.1 Model
Priority Queue H
Insert(H)
DeleteMin(H)
Function level
4
6.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)

5
6.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?)

6
6.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).

7
6.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?.

9
6.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 !!
10
A Heap
A Binary Search Tree
Note the difference in node ordering!!
11
6.3 Binary Heap
Definitions
struct HeapStruct int Capacity / maximum
size / int Size / actual size /
ElementType Elements typedef struct
HeapStruct PriorityQueue
12
6.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)
13
6.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!!!")
14
6.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
15
6.3 Binary Heap - Insert
16
6.3 Binary Heap - Insert
17
6.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.

18
6.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
19
6.3 Binary Heap - Insert
20
6.3 Binary Heap - DeleteMin
21
6.3 Binary Heap - DeleteMin
31
22
6.3 Binary Heap - DeleteMin
23
6.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.

24
6.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.

25
6.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--
26
6.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
27
6.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.

28
6.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)

29
6.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.

30
6.3 Binary Heap - Other Heap Operations
  • Initial heap

31
6.3 Binary Heap - Other Heap Operations
  • After PercolateDown (7)

32
6.3 Binary Heap - Other Heap Operations
  • After PercolateDown (6)

33
6.3 Binary Heap - Other Heap Operations
  • After PercolateDown (5)

34
6.3 Binary Heap - Other Heap Operations
  • After PercolateDown (4)

35
6.3 Binary Heap - Other Heap Operations
  • After PercolateDown (3)

36
6.3 Binary Heap - Other Heap Operations
  • After PercolateDown (2)

37
6.3 Binary Heap - Other Heap Operations
  • After PercolateDown (1)

38
6.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).

39
6.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

40
6.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

41
6.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

42
6.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

43
6.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

44
6.4 Applications of Priority Queues
45
6.4 Applications of Priority Queues
Departure time Arrival time Waiting
time Service time
46
6.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
47
6.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
48
6.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?
Write a Comment
User Comments (0)
About PowerShow.com