CSE326:DataStructures Lecture#3 MindyourPriorityQs BartNiswonger - - PDF document

cse 326 data structures lecture 3 mind your priority qs
SMART_READER_LITE
LIVE PREVIEW

CSE326:DataStructures Lecture#3 MindyourPriorityQs BartNiswonger - - PDF document

CSE326:DataStructures Lecture#3 MindyourPriorityQs BartNiswonger SummerQuarter2001 TodaysOutline TheFirstQuiz! ThingsBartDidntGettoonWednesday


slide-1
SLIDE 1

1

CSE326:DataStructures Lecture#3 MindyourPriorityQs

BartNiswonger SummerQuarter2001

Today’sOutline

  • TheFirstQuiz!
  • ThingsBartDidn’tGettoonWednesday

(PriorityQueues&Heaps)

  • Extraheapoperations
  • d-Heaps
slide-2
SLIDE 2

2

BacktoQueues

  • Someapplications

– orderingCPUjobs – simulatingevents – pickingthenextsearchsite

  • Problems?

– shortjobsshouldgofirst – earliest(simulatedtime)eventsshouldgofirst – mostpromisingsitesshouldbesearchedfirst

PriorityQueueADT

  • PriorityQueueoperations

– create – destroy – insert – deleteMin – is_empty

  • PriorityQueueproperty:fortwoelementsin

thequeue,x andy,ifx hasalowerpriority value thany,x willbedeletedbeforey

F(7) E(5) D(100) A(4) B(6)

insert deleteMin

G(9) C(3)

slide-3
SLIDE 3

3

ApplicationsofthePriorityQ

  • Holdjobsforaprinterinorderoflength
  • Storepacketsonnetworkroutersin
  • rderofurgency
  • Simulateevents
  • Selectsymbolsforcompression
  • Sortnumbers
  • Anythinggreedy

NaïvePriorityQDataStructures

  • Unsortedlist:

– insert: – deleteMin:

  • Sortedlist:

– insert: – deleteMin:

slide-4
SLIDE 4

4

BinarySearchTreePriorityQ DataStructure(that’samouthful)

4 12 10 6 2 11 5 8 14 13 7 9 insert: deleteMin:

BinaryHeapPriorityQData Structure

20 14 12 9 11 8 10 6 7 5 4 2

  • Heap-orderproperty

– parent’skeyislessthan children’skeys – result:minimumis alwaysatthetop

  • Structureproperty

– completetreewithfringe nodespackedtotheleft – result:depthisalways O(logn);nextopen locationalwaysknown

slide-5
SLIDE 5

5 20 14 12 9 11 8 10 6 7 5 4 2 2 4 5 7 6 10 8 11 9 12 14 20 12

1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12

NiftyStorageTrick

  • Calculations:

– child: – parent: – root: – nextfree:

DeleteMin

20 14 12 9 11 8 10 6 7 5 4 ? 2 20 14 12 9 11 8 10 6 7 5 4 2 pqueue.deleteMin()

slide-6
SLIDE 6

6

PercolateDown

20 14 12 9 11 8 10 6 7 5 4 ? 20 14 12 9 11 8 10 6 7 5 ? 4 20 14 12 9 11 8 10 ? 7 5 6 4 20 14 20 9 11 8 10 12 7 5 6 4

Finally…

14 20 9 11 8 10 12 7 5 6 4

slide-7
SLIDE 7

7

DeleteMin Code

ObjectdeleteMin(){ assert(!isEmpty()); returnVal =Heap[1]; size--; newPos= percolateDown(1, Heap[size+1]); Heap[newPos]= Heap[size+1]; returnreturnVal; }

intpercolateDown(inthole,Objectval){ while(2*hole<=size){ left=2*hole; right=left+1; if(right<=size&& Heap[right]<Heap[left]) target=right; else target=left; if(Heap[target]<val ){ Heap[hole]=Heap[target]; hole=target; } else break; } returnhole; }

runtime:

Insert

20 14 12 9 11 8 10 6 7 5 4 2 20 14 12 9 11 8 10 6 7 5 4 2 pqueue.insert(3) ?

slide-8
SLIDE 8

8

PercolateUp

20 14 12 9 11 8 10 6 7 5 4 2 ? 20 14 12 9 11 8 ? 6 7 5 4 2 10 20 14 12 9 11 8 5 6 7 ? 4 2 10 20 14 12 9 11 8 5 6 7 3 4 2 10 3 3 3

InsertCode

voidinsert(Objecto){ assert(!isFull()); size++; newPos= percolateUp(size,o); Heap[newPos]=o; }

intpercolateUp(inthole, Objectval){ while(hole>1&& val<Heap[hole/2]) Heap[hole]=Heap[hole/2]; hole/=2; } returnhole; }

runtime:

slide-9
SLIDE 9

9

ChangingPriorities

  • Inmanyapplicationsthepriorityofanobject

inapriorityqueuemaychangeovertime

– ifajobhasbeensittingintheprinterqueuefora longtimeincreaseitspriority – unix“renice”

  • Musthavesome(separate)wayoffindthe

positioninthequeueoftheobjecttochange (e.g. ahashtable)

OtherPriorityQueue Operations

  • decreaseKey

– giventhepositionofanobjectinthequeue, reduceitspriorityvalue

  • increaseKey

– giventhepositionofananobjectinthequeue, increaseitspriorityvalue

  • remove

– giventhepositionofanobjectinthequeue, removeit

  • buildHeap

– givenasetofitems,buildaheap

slide-10
SLIDE 10

10

DecreaseKey,IncreaseKey, andRemove

voiddecreaseKey(int obj){ assert(size>=obj); temp=Heap[obj]; newPos= percolateUp(obj,temp); Heap[newPos]=temp; } voidincreaseKey(intobj){ assert(size>=obj); temp=Heap[obj]; newPos=percolateDown(obj,temp); Heap[newPos]=temp; }

voidremove(intobj){ assert(size>= obj); percolateUp(obj, NEG_INF_VAL); deleteMin(); }

BuildHeap

Floyd’sMethod.Thankyou,Floyd.

5 11 3 10 6 9 4 8 1 7 2 12 pretendit’saheapandfixtheheap-orderproperty! 2 7 1 8 4 9 6 10 3 11 5 12

slide-11
SLIDE 11

11

Build(this)Heap

6 7 1 8 4 9 2 10 3 11 5 12 6 7 10 8 4 9 2 1 3 11 5 12 11 7 10 8 4 9 6 1 3 2 5 12 11 7 10 8 4 9 6 5 3 2 1 12

Finally…

11 7 10 8 12 9 6 5 4 2 3 1 runtime:

slide-12
SLIDE 12

12

ThinkingaboutHeaps

  • Observations

– findingachild/parentindexisamultiply/divideby two – operationsjumpwidelythroughtheheap – eachoperationlooksatonlytwonewnodes – insertsareatleastascommonasdeleteMins

  • Realities

– divisionandmultiplicationbypowersoftwoare fast – lookingatonenewpieceofdatasucksinacache line – withhuge datasets,diskaccessesdominate 4 9 6 5 4 2 3 1 8 10 12 7 11

Solution:d-Heaps

  • Eachnodehasd children
  • Stillrepresentableby

array

  • Goodchoicesford:

– optimizeperformancebasedon #ofinserts/removes – chooseapoweroftwofor efficiency – fitonesetofchildreninacache line – fitonesetofchildrenona memorypage/diskblock

3 7 2 8 5 121110 6 9 1 12

slide-13
SLIDE 13

13

OneMoreOperation

  • Mergetwoheaps.Ideas?

ToDo

  • Readchapter6inthebook
  • Haveteams
  • Doproject1
  • Askquestions!
slide-14
SLIDE 14

14

ComingUp

  • Mergeableheaps
  • DictionaryADTandSelf-Balancing

Trees

  • UnixDevelopmentTutorial(Tuesday)
  • Firstprojectdue(nextWednesday)