Home >Documents >Priority Queue

Priority Queue

Date post:23-Feb-2016
Category:
View:39 times
Download:0 times
Share this document with a friend
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

MethodTimesizeO(1)isEmptyO(1)insertO(n)removeMinO(1)minO(1)An alternative the heapheap a heap H is a binary tree heap a heap H is a binary tree H is a complete binary treeheap a heap H is a binary tree H is a complete binary treeFill up level d before moving to level d+1 each level but the last must be full in last level fill from left to rightheap a heap H is a binary tree H is a complete binary treeFill up level d before moving to level d+1 each level but the last must be full in last level fill from left to rightheap a heap H is a binary tree H is a complete binary treeFill up level d before moving to level d+1 each level but the last must be full in last level fill from left to rightheap a heap H is a binary tree H is a complete binary treeFill up level d before moving to level d+1 each level but the last must be full in last level fill from left to rightNot a heap!heap a heap H is a binary tree H is a complete binary tree heap order property is maintainedheap a heap H is a binary tree H is a complete binary tree heap order property is maintainedGiven a node v (not the root) the parent of v is less than or equal to vheap a heap H is a binary tree H is a complete binary tree heap order property is maintained459156207Given a node v (not the root) the parent of v is less than or equal to vheap a heap H is a binary tree H is a complete binary tree heap order property is maintained459156207Given a node v (not the root) the parent of v is less than or equal to vA heap H with n nodes has height O(log(n))Example: adding to a heapheapExample: adding to a heap4561516259141271120heapExample: adding to a heap4561516259141271120Insert 8heapExample: adding to a heap45615162591412711820Insert 8heapExample: adding to a heap45615162591412711820Insert 88 is greater than parent (7) ... doneheapExample: adding to a heap45615162591412711820heapExample: adding to a heap45615162591412711820Insert 2heapExample: adding to a heap456151625914127118202Insert 2heapExample: adding to a heap456151625914127118202Insert 22 is less than parent 20heapExample: adding to a heap456151625914127118202Insert 22 is less than parent 20 ... swap!heapExample: adding to a heap456151625914127118220Insert 2heapExample: adding to a heap456151625914127118220Insert 2heapExample: adding to a heap456151625914127118220Insert 22 is less than parent 6heapExample: adding to a heap456151625914127118220Insert 22 is less than parent 6 ... swap!heapExample: adding to a heap452151625914127118620Insert 22 is less than parent 6 ... swap!heapExample: adding to a heap452151625914127118620Insert 2heapExample: adding to a heap452151625914127118620Insert 22 is less than parent 4heapExample: adding to a heap452151625914127118620Insert 22 is less than parent 4 ... swap!heapExample: adding to a heap254151625914127118620Insert 22 is less than parent 4 ... swap!heapExample: adding to a heap254151625914127118620Insert 2heapExample: adding to a heap254151625914127118620Insert 2Done!heapExample: adding to a heap254151625914127118620Example: removal from a heapNOTE: it is a heap in a different stateheapExample: removal from a heap456151625914127111320heapExample: removal from a heap456151625914127111320Save off top of heapheapExample: removal from a heap456151625914127111320Save off top of heap4heapExample: removal from a heap456151625914127111320Copy last item in heap to top of heap4heapExample: removal from a heap456151625914127111320Copy last item in heap to top of heap4heapExample: removal from a heap456151625914127111320Copy last item in heap to top of heap4heapExample: removal from a heap1356151625914127111320Copy last item in heap to top of heap4heapExample: removal from a heap1356151625914127111320Delete last item in heap4heapExample: removal from a heap135615162591412711204heapExample: removal from a heap13561516259141271120Compare current node with its children4heapExample: removal from a heap13561516259141271120Compare current node with its children4heapExample: removal from a heap13561516259141271120If greater then swap with smallest child4heapExample: removal from a heap13561516259141271120If greater then swap with smallest child4heapExample: removal from a heap51361516259141271120If greater then swap with smallest child4heapExample: removal from a heap513615162591412711204heapExample: removal from a heap51361516259141271120If greater then swap with smallest child4Compare current node with its childrenheapExample: removal from a heap51361516259141271120If greater then swap with smallest child4heapExample: removal from a heap51361516259141271120If greater then swap with smallest child4heapExample: removal from a heap59615162513141271120If greater then swap with smallest child4heapExample: removal from a heap596151625131412711204heapExample: removal from a heap596151625131412711204Compare current node with its childrenheapExample: removal from a heap596151625131412711204If greater then swap with smallest childheapExample: removal from a heap596151625131412711204If greater then swap with smallest childheapExample: removal from a heap596151625131412711204If greater then swap with smallest childheapExample: removal from a heap596151625121413711204If greater then swap with smallest childheapExample: removal from a heap5961516251214137112044heapExample: removal from a heap59615162512141371120Return resultheapExample: removal from a heap59615162512141371120Done upheap bubbling: when we add to the heap

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

Click here to load reader

Embed Size (px)
Recommended