priorityQueue-ADTLPO.li Stores a collection of pairs ( item , - - PowerPoint PPT Presentation

priorityqueue adtlpo li
SMART_READER_LITE
LIVE PREVIEW

priorityQueue-ADTLPO.li Stores a collection of pairs ( item , - - PowerPoint PPT Presentation

Priority Queue ! Heaps priorityQueue-ADTLPO.li Stores a collection of pairs ( item , priority ) are from - Priorities ordered set some ( For simplicity , we use priorities " ) " highest priority from with 0 , 1,2 , 0 . . . - Main


slide-1
SLIDE 1

Priority Queue ! Heaps

slide-2
SLIDE 2

priorityQueue-ADTLPO.li

Stores a collection of pairs ( item , priority)

  • Priorities

are from

some

  • rdered set

( For simplicity , we

use priorities

from

0, 1,2 ,

. . .

with

"

highest priority

" )
  • Main operations
:
  • insert ( item , priority)

adds item with priority priority

. extract
  • mind

removes 4.returns)

item with least priority

  • update( item , priority)

changes priority of

item to priority

slide-3
SLIDE 3

RootedBmaryTreeter

  • proof
= every

node has

  • r 2 children .
  • perfect
= proper BT . where every

t.nl

has the

same death .

  • We want

a data

structure to implement

efficient

PQS

.

Eeg

. 040g n) time for all operations . . We (again) will

use

a particular

kind of tree

slide-4
SLIDE 4 . . . . . .
  • fordered-binarytrees.ir

isits each node of the tree

  • nce
  • visits every

node at depth i before any node at

depth it1 *

  • visits

every

depth

  • d descendent of

left G)

before any depth

  • d descendent of right G) .

F-

  • Is

g.

Is

# III.

→ sA1→AH#

↳ t.to#.-shg-7-z

> → → * in some texts , it is

bottom - up

, not top
  • down.
slide-5
SLIDE 5

RootedBmaryTreeter

  • proof
= every

node has

  • r 2 children .
  • perfect
= proper BT . where every 1
  • n f.

has the

same death .

* has 2 ** in the

Complete Binary Tree

children

level - order

F-

traversal

A complete binary tree of height

h

is

1

;

a binary tree of height hi 2 with 2d nodes at depth d

, for every

0 ← dah

  • 3. level order traversal visits every internal node before any leaf

**

  • 4. every internal

node is proper4 except perhaps the last

,

which may have just a left child .

. :*, F.

157,53

  • F. IF,

µ

#b

t.sk#osITNjho7Ibbh.Jbb

A

µ sink

G) × 4) *

slide-6
SLIDE 6
slide-7
SLIDE 7

Binary Heap Data Structure

÷

"
  • a complete binary tree]

shape invariant

  • with vertices labelled by keys from

some

  • rdered set
,

ftp.iorities-st.keyldtkeiylpareutlvD-YIIIaut

"

for every

node

V

Eg

. f-

③ ②

/ -⑥

{ to

⑤ '

I

✓ ② to

£1.0

×

  • This is the basic D.S. for implementing

PQS

(Binary

min - cheap) . .
slide-8
SLIDE 8
  • How do
we implement the
  • perations
so that

the invariants are maintained ?

  • Consider Insertion:

If

we want to insert

14

to the heap ,

where should it

go ?

  • Notice:

there

no

choice about how the shade changes

:

T

1ha

I

✓ to

do

d.

do

T

  • do

1

AM

.

hobnob

slide-9
SLIDE 9

HeapIn

T

  • insert an item with key

K

:
  • 1. add a new leaf

v with

key G) = K

, so as to

maintain the shape invariant

2.

re - establish the order invariant by

executing

percolate -upG) .

percolate

  • up G) {

while ( r is not root and

keyLv ) a key

. ( parent lol) ) I swap positions of v and

parent G)

in the tree

}

}

¥

←48

×

/

4

\

€ ↳ ← 8h10

}

i.%#

2

slide-10
SLIDE 10

Insert-2.thenI.thenzinto.io

¥1 T

  • ④ TO

④ ¥¥

¥

I

,①I

← ↳

70s

A

④ ¥

⇐ ⑤ D

'

⑤ ④ ⑥To

i

slide-11
SLIDE 11

Heap Extract

  • Min .
  • Consider :

ssmo%¥

⇒ §yj

these

heap

we must replace the root with the smaller of its children :

f-

① 8%8 ?

10 12

0k

Not

  • k !
slide-12
SLIDE 12

Heap Extract

  • Min .
  • To remove the litem with the) smallest key

from the heap:

1.

remove the root

  • 2. replace the root with the
" last leaf " ,

so as to maintain the shape invariant .

4.

restore the order invariant by calling

percolate

  • down(root)

Percolate

  • down
is more work than

percolate

  • up ,

because it must

look

at

both

children

to

see what to do ( and the children

may

  • r may

not

exist)

slide-13
SLIDE 13

Heapreml

  • remove the root
  • fix the shape invariant by moving the

" bottom right

"
  • r

" last "

nude to the root .

X.tl #

.

percolate

  • down 4){

while ( r has

a child

c with keyG) a key as){

c ← child of

r with the smallest key

among the children of r .

swap r and

c in the tree

}

I

Notice that :

  • V

may have

0,1 ,

  • r 2 children
.

if

v

has

2 children ,

we

care about

the

  • ne

with the smallest

key

.
slide-14
SLIDE 14

Do

extract - min

3 times:

  • AT

*

i

④ ¥h

#

⑤ '

slide-15
SLIDE 15

Doextraot-m

.

I

⑦'

slide-16
SLIDE 16

Complexity of

Heap Insert ! Extract - min

  • Claim : Insert

to Extract - min take time

0( log a)

for

heaps

  • f size
n .

Recall: A perfect binary tree of height h has 2h"

  • I nodes
.
  • II. By induction
  • n h ( or
" the structure of the tree .

B¥s

: If h=o then we have It '
  • I
= 1 nodes . V

III.

: Consider some H70

and

assume the perfect

binarytree of height h

has

2h"

  • I
  • ii. des .
+ ,

II

!

show the

p . b.t . of height htt

has

21h

")
  • I
nodes s .

The tree

is:}

A DI¥

"

so it has 2h

"
  • it 2h11 +I
= 2. 2h11 = 21""I 1 modesty
slide-17
SLIDE 17

sizeboundsoncompletebinarytrees.IE

very complete binary tree with height hand

in nodes

satisfies :

2h

± in ⇐ zhtly

Smallest :

Left

:

I

p.IT . of height h-I

p.b.tv of height h .

# nodes

=

21h

  • htt
. It , = 2h

So

, we have :

2h In

log.2h I

login

h

E login

h= Ollogn)

⇒ Heap insert 4 extract

min take time 0( log a)

slide-18
SLIDE 18

hinkedImplemewtatimr.at

µ

,

D

" last "

I

X.

*

1€

last

" ¥t¥

E

slide-19
SLIDE 19

Array-BasedBinarytteapImplemeutatimllse.co

this embedding of

a

complete

binary tree

  • f

site

n in

a

size - n array

:

← ↳

ith node in

③/h④

⑤ T

  • level - order

④¥ ④ ④

⑤ traversal

4

It

ith array element

  • Children of

node i

are nodes 2it I

& 2 it 2

  • Parent of

node i

is node KE14

1AX

0*14*18%11

www.

slide-20
SLIDE 20

Array-BasedBinarytteapImplemeutatimllse.co

this embedding of

a

complete

binary tree

  • f

site

n in a

size - n array

:

← ↳

ith node in

③/h④

⑤ T

  • level - order

④¥ ④ ④

⑤ ⑤ traversal

4

It

ith array element

  • Children of

node i

are nodes 2it I

¢

2 it 2

  • Parent of

node i

is node kindly

I

was

× Growing 4 Shrinking the tree is easy in the array embedding
slide-21
SLIDE 21

partiaky-fttedArrayImpkmentationofBinarytteap.LI

¥←

=

'

Insert

④¥ ,

¥57

⑧%¥i%

① 6

I ¥

6 2

1-

← to Id

'

%

slide-22
SLIDE 22

Insert for Array

  • based Heap
  • +
. Variables : ar# , size
  • Heap elements

are in

A fo)

. . Afsite
  • I

insert4)I

÷÷÷÷:*:*:*:]

:::±.

while ( v > o

and

AG3 < Alps){

Swap

AG3

and A Cp]

✓ ← p p ← 4- 1) 12J

}

}

site ← site t t ;

slide-23
SLIDE 23

Partially

  • fitted Array Implementation of Binary Heap. : Extract- min
  • I

← ¥

=

¥%HHXN

' 9it V 11

⑥ l④

it

#

6

It

←¥

8¥ to

a # ⑥ €

"

0¥ ¥

11 to
slide-24
SLIDE 24

Extraot.minforArray-basedtte.pt

extract - milk

temp ←

A Lo]

A record

value to return

site ← size
  • I
AC03 ← A Lsieej A move old last leaf to root

i ← 0

while ( ait t

a site )

while i

not a a leaf

child ←

Zit 1

11 the

left child of i

percolate

if (

2 it 2 a site AND A Git 23 L AL2itD){ 11 use the right child if it exists

down}

* xieo! ÷÷::?.

swap A Lehild] and Ali]

[ ← child

} else {

[ ,

3

return temp return temp

.

}

slide-25
SLIDE 25

A small

space

  • for
  • time trade - off

_inExtraot-m

  • Extract
  • min does many comparisons, eg this size) to

check if

i is a

leaf .

  • Suppose

we ensure the array

has site

7 2. size

a¥ there is

a big value,

denoted a

, that can

be

stored in the array

but will never be a key

.

and

every array entry that is not a key is a .

  • Then ,
we can skip the explicit checks for

being

a leaf .

slide-26
SLIDE 26

Extract-mmrariantextract-m.NL

i has

a child

temp ← Alo]

A record

value to return

that

is out of

site ← size - I
  • rder
move # last leaf to

i ← 0

while ( AL2it is a ACD

¥ Akita] < ALII){

peggwlatefg.tl?!II?nII.Isi:IIci

,} it

is the left chid

Setseswap

AL2 it is and Acis}

it

is the right child

is 2it 2

}

" {eturw temp

}

slide-27
SLIDE 27

makingatteapfromase.to

Suppose you have

n keys and want to make a heap with them . .Clearly

can be done in time 04 log n) , with

n inserts .
  • Claim : the following alg
.

does it

in time 0(a) . make -heap ( T) S

XT is a complete bit . with

n keys .

for ( i

= tht -1 down to

a){

call percolate

  • down
  • n node i

}

3

slide-28
SLIDE 28

Howdoesmake-heapwork.7.hn/2J-l

is the

last internal node

  • the algorithm does a

percolate

  • down at

each internal

node , working

bottom - up

.

( percolate

  • down makes

a tree into

a

heap

if

the only node violating the order

property

is the

root )

0 • I ✓\

] N4

5TG

15 16 " ' 8 "9

"

Lnfhj

  • I
= Leith
  • I
= I
slide-29
SLIDE 29

;%

D'D

slide-30
SLIDE 30

tceheapExam

, ④

.
  • .-

# ④ ✓

4 ④

2

¥1

⑤ 708

z 4

* ¥ #a

D= 10

;

Ln I2J - I

= 4

Notice: The exact order of visiting

nodes does not

matter

  • as long
as

we

visit children

before parents

.

[

It follows that it is easy to do

a recursive make - '

heap)

slide-31
SLIDE 31

Make-heaplomplexitf.CI

early

Oln log n)

:

n percolate

  • down calls
,

each 040g n) .

. How can we see it is actually 0(n) ?

Intuition :

mark

a

distinct edge for

ererypossibleswo.pl

Time taken is

bounded by

max

. # of swaps

possible)

I

\

*

slide-32
SLIDE 32

Timelomplexityoftlakeheaplet

sln)

be the max

number of swaps

carried

  • ut

by

make- heap

  • n a

set

  • f

size

n .

We

can bound

Scn

) by

:

541

I

2dL - d)

d-

  • o

b

[The max # of swaps

for

A

/

a

call to percolate

  • down
  • n

percolate

  • down)

t

a node at depth d

is called

, at most ,

there are

is

h-d

  • n each

node at

2d nodes

each depth d

at depth d

from

0 to ht

.
slide-33
SLIDE 33

she

2dL

  • d)

a

A- 0

11 = I = 21h - o) t2Yh-1) t . . .

t [' ( h -th

  • D)
t 2h" (h- Chi))

set

i=h-d

, so D= h - i

and while d ranges

  • ver

0,1 ,

  • .
. , h - I , i will range
  • ver

he;o , h- I ,

. . . h- (in
  • 1)

"

h

1

Now

she iii.ili)

= Etta

.ie?IEiilbenYI)=nIIi.snhEoIit2n-

(£o

= It fit f. + It . . . =It ItIttIIy )
slide-34
SLIDE 34

Lomplexityotmakeheapblork

done by

make - heap is

bounded by

a

constant times the

number of swaps

so

is 0(hogn)T

0 (n) .

forrated

)

slide-35
SLIDE 35

Updatingprioritie.si

Suppose a heap contains an item with priority K , and we execute

update

  • priority( item , j ) .
  • We replace
K with j in the heap , and then restore the
  • rder invariant :

if ja k

, do

percolate

  • up from the

modified

node

if

kaj

,

do

percolate - down from

the modified

node .

X.

13>

I

µ

^ AN

AD

ADMD

Th takes ollogn) time

  • bit howdowe-t.nl
is

the right

node to change ??

Restoring -

  • T
  • do this, we

need an auxiliary data structure .

slide-36
SLIDE 36

End

=

slide-37
SLIDE 37

Correctness of

swapping

in

percolate down

  • b
. Suppose we are percolating down c

A.c

the:c:D!:

III.

µ T

  • e
. If

a > e

and

Ed ,

[

A

D

we

swap

  • c. e

Now :

. we

know

bt.es c

and

be

. es d
  • b

a#e

. so order

is

OK , except

µ

dfhq.ge

possibly

below

a

  • which

[

A

D

we still

have to

look at

.
slide-38
SLIDE 38

Correctness

  • f swapping in

percolate

  • up
  • " suppose
we are percolating

up

a

°b

we know CED , ese , because we

Had

previously swapped

c with dore

µ

Doe

  • we know that be a

[

A D. if cab ,

we

swap e.b

Now :

.

we now know that

  • a

cab se

{↳

and

cc bed

µ

dfh.ge

and

a

< bsa

(

D

D

' so
  • rder

is ok , except

T3

possibly

with ancestors of

C

, which we still must check.