CSE326:DataStructures Lecture#4 HeapsmorePriorityQs BartNiswonger - - PDF document

cse 326 data structures lecture 4 heaps more priority qs
SMART_READER_LITE
LIVE PREVIEW

CSE326:DataStructures Lecture#4 HeapsmorePriorityQs BartNiswonger - - PDF document

CSE326:DataStructures Lecture#4 HeapsmorePriorityQs BartNiswonger SummerQuarter2001 TodaysOutline Returnquizzes ThingsBartDidntFinishonFriday (insert&d-Heaps)


slide-1
SLIDE 1

1

CSE326:DataStructures Lecture#4 HeapsmorePriorityQs

BartNiswonger SummerQuarter2001

Today’sOutline

  • Returnquizzes
  • ThingsBartDidn’tFinishonFriday

(insert&d-Heaps)

  • LeftistHeaps
  • SkewHeaps
  • ComparingHeaps
slide-2
SLIDE 2

2

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) 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:

slide-3
SLIDE 3

3

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()

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-4
SLIDE 4

4

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-5
SLIDE 5

5

OtherPriorityQueue Operations

  • decreaseKey

– giventhepositionofanobjectinthequeue, reduceitspriorityvalue

  • increaseKey

– giventhepositionofananobjectinthequeue, increaseitspriorityvalue

  • remove

– giventhepositionofanobjectinthequeue, removeit

  • buildHeap

– givenasetofitems,buildaheap

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(); }

slide-6
SLIDE 6

6

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

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

slide-7
SLIDE 7

7

Finally…

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

ThinkingaboutHeaps

  • Observations

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

  • Realities

– divisionandmultiplicationbypowersoftwoare fast – lookingatonenewpieceofdatasucksinacache line – withhuge datasets,diskaccessesdominate

slide-8
SLIDE 8

8 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

OneMoreOperation

  • Mergetwoheaps.Ideas?
slide-9
SLIDE 9

9

Merge

Giventwoheaps,mergethemintoone heap

– firstattempt:inserteachelementofthe smallerheapintothelarger. runtime: – secondattempt:concatenateheaps’arrays andrunbuildHeap. runtime:

HowaboutO(logn)time?

Idea:HangaNewTree

12 13 10 6 11 5 2

+

10 14 4 9 1

=

14 12 13 10 6 4 9 11 5 1 2 ? 10 Now,just percolatedown!

slide-10
SLIDE 10

10

Idea:HangaNewTree

12 13 10 6 11 5 2

+

10 6 11 5 2

=

12 13

Problem?

12 13 12 13

LeftistHeaps

  • Idea:
  • Leftistheap:

– almostallnodesareontheleft – allthemergingworkisontheright

makeitsothatalltheworkyou havetodoinmaintainingaheap isinonesmallpart

slide-11
SLIDE 11

11 thenullpathlength(npl) ofanodeisthenumber

  • fnodesbetweenitandanullinthetree

RandomDefinition: NullPathLength

  • npl(null)=-1
  • npl(leaf)=0
  • npl(single-child

node)=0

1 1 1 2 anotherwayoflookingatit: nplistheheightofcomplete subtreerootedatthisnode

LeftistHeapProperties

  • Heap-orderproperty

– parent’spriorityvalueis tochildrens’ priority values – result:minimumelementisattheroot

  • Leftistproperty

– nullpathlengthofleftsubtree is nplofright subtree – result:treeisatleastas“heavy”ontheleftas theright

Areleftisttreescomplete?Balanced?

slide-12
SLIDE 12

12

Leftisttreeexamples

NOT leftist leftist 1 1 1 2 1 1 2 1 1 leftist everysubtreeofaleftist treeisleftist,comrade!

RightPathinaLeftistTreeisShort

  • Iftherightpathhaslengthatleast

r,thetreehasatleast2r-1 nodes

  • Proofbyinduction

Basis:r=1.Treehasatleast

  • nenode:21 - 1=1

Inductivestep:assumetrueforr’<r.Therightsubtreehasa rightpathofatleastr- 1nodes,soithasatleast2r- 1 - 1 nodes.Theleftsubtreemustalsohavearightpathofatleast r- 1 (otherwise,thereisanullpathofr- 3,lessthanthe rightsubtree).Again,thelefthas2r- 1 - 1 nodes.Alltoldthen, thereareatleast: 2r- 1 - 1+2r- 1 - 1+1=2r - 1

  • So,aleftisttreewithatleastn nodeshasaright

pathofatmostlogn nodes

1 1 2 1

slide-13
SLIDE 13

13

Whew!

ToDo

  • UnixdevelopmentTutorial

– Tuesday– 10:50am– Sieg322

  • FinishProjectIforWednesday
  • Readchapters1&2
slide-14
SLIDE 14

14

ComingUp

  • Theory!
  • ProofbyInduction
  • AsymptoticAnalysis
  • Quiz#2(Thursday)