25 Priority Queues
-
Upload
rohitvyasips -
Category
Documents
-
view
230 -
download
0
Transcript of 25 Priority Queues
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 1/13
Priority Queues
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 2/13
2
Priority queue
A stack is first in, last outA queue is first in, first outA priority queue is least-first-out
Th e ³smallest´ element is t h e first one removed(You could also define a largest-first-out priority queue)
Th e definition of ³smallest´ is up to t h e programmer (for example, you mig h t define it by implementing
Comparator or Comparable )I f th ere are several ³smallest´ elements, t h e implementer must decide w h ich to remove first
Remove any ³smallest´ element (don¶t care w h ich )Remove t h e first one added
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 3/13
3
A priority queue ADT
H ere is one possible ADT:
P riorityQueue() : a constructor v oid add(Comparable o) : inserts o into t h e priority queue
Comparable remo v eLeast():
removes and returns th
e leastelementComparable getLeast() : returns (but does not remove) t h eleast elementboolean isEmpty() : returns true iff emptyint size() : returns t h e number of elementsv oid clear() : discards all elements
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 4/13
4
E valuating implementations
W h en we c h oose a data structure, it is important to look at usage patterns
I f we load an array once and do t h ousands of searc h es on it,we want to make searc h ing fast²so we would probably sort
th e arrayI f we load a h uge array and expect to do only a few searc h es,we probably d on¶t want to spend time sorting t h e array
F or almost all uses of a queue (including a priority
queue), we eventually remove everyth
ing th
at we addH ence, w h en we analyze a priority queue, neit h er ³add´nor ³remove´ is more important²we need to look at t h etiming for ³add + remove´
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 5/13
5
A rray implementations
A priority queue could be implemented as an unsorte d
array (wit h a count of elements)A dding an element would take O (1) time (w h y?)Removing an element would take O (n) time (w h y?)H ence, adding an d removing an element takes O (n) timeTh is is an inefficient representation
A priority queue could be implemented as a sorte d array(again, wit h a count of elements)
A dding an element would take O (n) time (w h y?)Removing an element would take O (1) time (w h y?)H ence, adding an d removing an element takes O (n) timeA gain, t h is is inefficient
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 6/13
6
L inked list implementations
A priority queue could be implemented as an unsorte d
linked listA dding an element would take O (1) time (w h y?)Removing an element would take O (n) time (w h y?)
A priority queue could be implemented as a sorte d
linked listA dding an element would take O (n) time (w h y?)Removing an element would take O (1) time (w h y?)
A s wit h array representations, adding an d removing anelement takes O (n) time
A gain, t h ese are inefficient implementations
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 7/13
7
B inary tree implementations
A priority queue could be represented as a (notnecessarily balanced) binary searc h tree
I nsertion times would range from O (log n) to O (n) (wh y?)
Removal times would range from O (log n) to O (n) (wh y?)A priority queue could be represented as a b alance d
binary searc h treeI nsertion and removal could destroy t h e balanceW e need an algorit h m to re b alance th e binary treeG ood rebalancing algorit h ms require only O (log n) time,
but are complicated
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 8/13
8
H eap implementationA priority queue can be implemented as a h eapI n order to do t h is, we h ave to define t h e h eap property
In H eapsort, a node h as th e h eap property if it is at least as large as itsch ildren
F or a priority queue, we will define a node to h ave t h e h eap property if itis as least as small as its c h ildren (since we are using smaller numbers torepresent h igh er priorities)
12
8 3H eapsort : B lue nodeh as th e h eap property
3
8 12Priority queue : B lue nodeh as th e h eap property
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 9/13
9
A rray representation of a h eap
L eft c h ild of node i is 2*i + 1 , rig h t ch ild is 2*i + 2U nless t h e computation yields a value larger t h an lastIndex , inwh ich case t h ere is no suc h ch ild
Parent of node i is (i ± 1)/2U nless i == 0
12
1418
6
8
3
3 12 6 18 14 80 1 2 3 4 5 6 7 8 9 10 11 12
lastIndex = 5
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 10/13
10
U sing t h e h eap
T o add an element :
Increase lastIndex and put t h e new value t h ereReh eap t h e newly added node
Th is is called up- h eap bubbling or percolating up
U p-h eap bubbling requires O (log n) timeT o remove an element :
Remove t h e element at location 0M ove t h e element at location lastIndex to location 0 , and decrementlastIndex
Reh eap t h e new root node (t h e one now at location 0 )Th is is called down- h eap bubbling or percolating downD own- h eap bubbling requires O (log n) time
Th us, it requires O (log n) time to add an d remove an element
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 11/13
11
C omments
A priority queue is a data structure t h at is designed toreturn elements in order of priorityE fficiency is usually measured as t h e sum of t h e timeit takes to add and to remove an element
S imple implementations take O(n) timeH eap implementations take O(log n) timeB alanced binary tree implementations take O(log n) timeB inary tree implementations, wit h out regard to balance, cantake O(n) (linear) time
Th us, for any sort of h eavy-duty use, h eap or balanced binary tree implementations are better
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 12/13
12
J ava 5 java.uti l.Pri orityQueueJ ava 5 finally h as a PriorityQueue class, based on h eaps
Pri orityQueue<E> queue = n ew Pri orityQueue<E> ();
bool ea n add (E o)
bool ea n rem ove (Ob ject o)
bool ea n off er (E o)E peek ()
bool ea n poll()
void clear ()int size ()
8/6/2019 25 Priority Queues
http://slidepdf.com/reader/full/25-priority-queues 13/13
13
Th e E nd