Home >Documents >Priority Queue

# Priority Queue

Date post:23-Feb-2016
Category:
View:39 times
Description:
Priority Queue. What is that? Implementation with linked list with O(n) behaviour The Heap (O(log(n)) An implementation using an array Your mission …. Priority queue. Store a collection of prioritized elements Elements are comparable Allow insertion of an element - PowerPoint PPT Presentation
Transcript:

Slide 1

Priority QueueWhat is that?Implementation with linked list with O(n) behaviourThe Heap (O(log(n))An implementation using an arrayYour mission Q.insert(e)e = Q.removeMin()Q.size()Q.isEmpty()Q.min()Priority queueStore a collection of prioritized elementsElements are comparableAllow insertion of an elementCan only remove the element with highest priorityComes first in orderWe present 3 implementationsPriority queueExample applications of a priority queueDispatching processes in a computerHospital waiting listsStandby passengers for a flightQueuing at call centresStore a collection of prioritized elementsElements are comparableAllow insertion of an elementCan only remove the element with highest priorityComes first in orderPriority queueHow can we compare objects?Two examples on comparing things An exampleHow can we compare objects?

This is a VertexAn exampleHow can we compare objects?This is a Comparator for vertices

An exampleHow can we compare objects?Using the comparator to sort an array of Vertex

An exampleHow can we compare objects?

Another example: a CarAn exampleHow can we compare objects?This is a CarComparator

An exampleHow can we compare objects?Using the CarComparator

unsorted listWe might use a linked listTo insert we add to the front of the listTo find the minimum we must iterate over entire the listTo remove the minimum we must find the minimum and remove itMaintain a counter of number of elements in the listMethodTimesizeO(1)isEmptyO(1)insertO(1)removeMinO(n)minO(n)Implementing a priority queue with an unsorted listsorted listWe might use a linked listThe list is maintained in non-decreasing orderTo insert we scan to find position and splice in (see below)To find the minimum we deliver the first element in the listTo remove the minimum we return and remove the first elementImplementing a priority queue with an sorted list

downheap bubbling: when we remove from the heapWhat we just sawAdd and remove are O(log(n)) processesAn implementation of a Heap data structure

An implementation of a Heap data structureAn implementation of a Heap data structureNumber the vertices as followsAn implementation of a Heap data structure123489510116121371415Number the vertices as followsAn implementation of a Heap data structure123489510116121371415Number the vertices as followsNote: parent of node i is i/2 .... An implementation of a Heap data structure123489510116121371415Number the vertices as followsNote: parent of node i is i/2 .... An implementation of a Heap data structure123489510116121371415Number the vertices as followsNote: parent of node i is i/2 .... An implementation of a Heap data structure123489510116121371415Number the vertices as followsNote: left child of i is i2 An implementation of a Heap data structure123489510116121371415Number the vertices as followsNote: right child of i is (i2) +1 An implementation of a Heap data structure123489510116121371415Represent as a one dimensional arrayAn implementation of a Heap data structure123489510116121371415Represent as a one dimensional array S0123456789101112131415SAn implementation of a Heap data structure123489510116121371415Represent as a one dimensional array S**0123456789101112131415STo simplify implementation we do not use S[0]An implementation of a Heap data structure**0123456789101112131415Require two integer variables, last and capacity where last is initially 0In our example capacity is 15Represent as a one dimensional array SSAn implementation of a Heap data structure**0123456789101112131415Represent as a one dimensional array SSlast: 0capacity: 15An implementation of a Heap data structureConsider the following heap HAn implementation of a Heap data structure498171969263293505516Consider the following heap HSlast: 12capacity: 15**4981726501619693293550123456789101112131415An implementation of a Heap data structure498171969263293505516Slast: 12capacity: 15**4981726501619693293550123456789101112131415H.add(6)An implementation of a Heap data structure4981719692632935055616Slast: 12capacity: 15**49817265016196932935560123456789101112131415S[last+1] = 6An implementation of a Heap data structure4981719692632935055616Slast: 13capacity: 15**49817265016196932935560123456789101112131415last++An implementation of a Heap data structure4981719692632935055616Slast: 13capacity: 15**49817265016196932935560123456789101112131415upheapBubble(13)An implementation of a Heap data structure4981719692632935055616Slast: 13capacity

Popular Tags:

of 136

Embed Size (px)
Recommended

Documents

Documents

Documents

Documents

Documents

Documents