Priority Queues - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Priority Queues

Description:

Insert elements into pqueue in the same fashion. as elements where inserted into a regular queue. ... whether high priorities are represented with high or low ... – PowerPoint PPT presentation

Number of Views:143
Avg rating:3.0/5.0
Slides: 8
Provided by: anniegro
Category:
Tags: priority | queues

less

Transcript and Presenter's Notes

Title: Priority Queues


1
Priority Queues
  • CS341
  • Western Washington University

2
Definitions
A priority queue is a collection of zero or
more elements, where each element has a priority
or value. Operations defined for a priority
queue Create Size Insert Retrieve
3
Ideas for Representation
Scenario 1 Insert elements into pqueue in the
same fashion as elements where inserted into a
regular queue. Retrieve element with highest
priority by performing a sequential search of
queue. Scenario 2 Insert element in
appropriate position in pqueue. This position
depends on whether high priorities are
represented with high or low values. Retrieve
element in the same way that elements were
retrieved from a regular queue. What is the
order of magnitude for insertion and retrieval in
these scenarios?
4
Can We Do Better?
Of courseI wouldnt ask if we couldnt. Lets
consider a tree representation. Definitions A
max tree is a tree in which the value of each
node is greater than or equal to those in
its children. Nodes of a max tree may have more
than 2 children. A max heap is a max tree that
is also a complete binary tree. Notice that
these definitions allow for duplicate keys/priorit
ies.
5
Heap Representation
We can represent a pqueue using a heap. The
value of a node will correspond to the
priority of an element. Insertion We can
insert into a heap in O(height)
O(logn). Retrieval We will always retrieve the
root of the heap, and then need to reshape the
heap. We can do this in O(height)
O(logn). How does a heap differ from a BST?
6
Heapsort
One possible application of the heap structure
is sorting. Consider the following algorithm to
sort n elements in ascending order template
ltclass Tgt void heapsort(T array , int n) //
initialize a heap with the // n
elements MinheapltTgt heap heap.initialize(array,
n) // retrieve the root of the heap // and
insert back into the array for (int i 0 i lt
n i) arrayi heap.retrieve()
7
Analyze the performance of heapsortconsider both
the space and time complexity. How does heapsort
compare to other sorting algorithms? Bubble
sort Insertion sort Merge sort Quick sort
Write a Comment
User Comments (0)
About PowerShow.com