inf421, lecture 2 queuesliberti/teaching/... · course objective: to teach you some data structures...

36
INF421, Lecture 2 Queues Leo Liberti LIX, ´ Ecole Polytechnique, France INF421, Lecture 2 – p. 1

Upload: others

Post on 23-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

INF421,Lecture

2Queues

Le

oL

ibe

rti

LIX

,E

co

leP

oly

tech

niq

ue,F

ran

ce

INF

421,

Lectu

re2

–p.

1

Page 2: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Course

Objective:

tote

ach

you

som

edata

str

uctu

res

and

associa

ted

alg

ori

thm

s

Evaluation:

TP

noté

en

salle

info

le16

septe

mbre

,C

ontr

ôle

àla

fin.

Note

:max(C

C,3 4CC

+1 4TP)

Organization:

fri26/8

,2/9

,9/9

,16/9

,23/9

,30/9

,7/1

0,

14/1

0,

21/1

0,

am

phi1030-1

2(A

rago),

TD

1330-1

530,

1545-1

745

(SI3

1,3

2,3

3,3

4)

Books:

1.

Ph.

Baptiste

&L.M

ara

nget,

ProgrammationetAlgorithmique,

Ecole

Poly

techniq

ue

(Poly

copié

),2006

2.

G.D

ow

ek,Lesprincipesdeslangagesdeprogrammation,

Editio

ns

de

l’X,2008

3.

D.

Knuth

,TheArtofComputerProgramming,A

ddis

on-W

esle

y,1997

4.

K.M

ehlh

orn

&P.S

anders

,AlgorithmsandData

Structures,S

pri

nger,

2008

Website:www.enseignement.polytechnique.fr/informatique/INF421

Contact:[email protected]

(e-m

ail

subje

ct:

INF

421)

INF

421,

Lectu

re2

–p.

2

Page 3: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Lecture

summary

Am

otiva

tin

gexa

mp

le

Qu

eu

es

Bre

ad

th-F

irst

Se

arc

h(B

FS

)

Imp

lem

en

tatio

n

INF

421,

Lectu

re2

–p.

3

Page 4: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Motivatingexample

INF

421,

Lectu

re2

–p.

4

Page 5: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Busnetwork

withtimetables

A

1h:0

0

2h:1

0

3h:3

0

B

1h:0

0

4h:2

0

5h:4

0

C

2h:1

0

3h:2

0

5h:3

0

D

4h:2

0

5h:4

0

6h:5

0

E

2h:0

5

5h:1

0

6h:3

0

F

3h:2

5

4h:3

0

6h:4

0

Fin

da

conve

nie

nt

itin

era

ryfr

om

1to

6,

leavin

gat

h:0

0?

INF

421,

Lectu

re2

–p.

5

Page 6: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Theeventgraph

redarrows:waitingfornextbus

1/0

0

2/1

0

A/1

0

4/2

0

B/2

0

2/0

5

5

5/1

0E

/5

3/2

0C

/10

3/3

0

A/2

03

/25

5

5/3

0

C/1

0

5

4/3

0

F/5

10

5/4

0

B/2

0

D/2

0

6/4

0

F/1

0

20

6/3

0

E/2

0

10

6/5

0

D/1

0

10

10 IN

F421,

Lectu

re2

–p.

6

Page 7: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Findingagooditinerary

1.

Sta

rtw

ithu=

1/100

2.

Le

tQ

be

the

se

to

fv∼

u(i.e

.Q

={2/10,4/20}

)

3.

Pic

kn

extu

inQ

(i.e

.u=

2/10

)

4.

Ifo

ne

ofv∼

uis

eq

ua

lto

t,sto

p

5.

Ad

dv∼

uto

Q(i.e

.Q

={4/20,3/20,3/30})

6.

Re

pe

atfr

om

Ste

p3

getitinerary

1→

6arrivingath:40:

(1/1

00

,4

/20

,4

/30

,6

/40

)

INF

421,

Lectu

re2

–p.

7

Page 8: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Retrievingthepath

Pre

vio

us

me

tho

do

nly

giv

es

us

the

du

ratio

n,n

ot

the

actu

alp

ath

Ateachiteration,store

nodesoutofqueuewithpredecessors

pre

dn

od

e

-1

/00

1/0

02

/10

1/0

04

/20

2/1

03

/20

2/1

03

/30

4/2

04

/30

4/3

06

/40

Now

retr

ieve

pa

thb

ackw

ard

s:

6/4

0→

4/3

0→

4/2

0→

1/0

0

an

dfin

ally

inve

rtth

ep

ath

:1

/00→

4/2

0→

4/3

0→

6/4

0IN

F421,

Lectu

re2

–p.

8

Page 9: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Thisain’tthefastest

Su

pp

ose

the

reis

abu

sG

with

tim

eta

ble

3/2

5→

6/3

0

1/0

0

2/1

0

4/2

0

2/0

5

5/1

0

3/2

0

3/3

0

3/2

5

5/3

0

4/3

0

5/4

0

6/4

0

6/3

0

6/5

0

A/1

0

B/2

0

5

E/5

C/1

0

A/2

0

5

C/1

0

5F/510

B/2

0

D/2

0

F/1

0

20

E/2

0

10

D/1

0

10

10

G/5

3/2

5is

still

inth

equeue

(5/4

03/2

55/3

06/4

0)

at

term

ination,can’t

reach

6/3

0 INF

421,

Lectu

re2

–p.

9

Page 10: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Whatdid

wefind?

Ino

rde

rto

fin

dth

efastestitin

era

ry,

mu

stch

an

ge

term

ina

tio

nco

nd

itio

ninsteadofstoppingwhenthearrivalnodeis

found,

letτ∗

be

the

arr

ivaltim

eof

best

solu

tion

so

far

letτ′

be

the

min

imum

tim

eof

the

nodes

inth

equeue

if(τ

′≥

τ∗)then

term

inate

endif

Wh

at

are

the

pro

pe

rtie

so

fo

ur

itin

era

ry?

We

fou

nd

the

itinerary

withfewestchanges

where

“bus,

waitin

g,bus”

counts

as

two

changes,

notone

Ino

rde

rto

pro

veth

ese

two

sta

tem

en

ts,

we

ne

ed

tofo

rma

lize

ou

rm

eth

od

into

an

alg

ori

thm

So,w

en

ee

dto

de

scri

be

ou

r“q

ue

ue

”a

sa

da

tastr

uctu

reIN

F421,

Lectu

re2

–p.10

Page 11: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Queues

INF

421,

Lectu

re2

–p.11

Page 12: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Queueoperations

Ino

ur

exa

mp

le,w

en

ee

de

do

ur

“qu

eu

e”

tob

ea

ble

top

erf

orm

the

follo

win

go

pe

ratio

ns:

insert

anelementattheendofthequeue

retrieveanddelete

theelementatthebeginningofthequeue

testifthequeueis

empty

findthesizeofthequeue

Sin

ce

the

se

op

era

tio

ns

we

rere

pe

ate

do

fte

n,

we

ne

ed

the

mto

beO(1)

Co

uld

imp

lem

en

tth

ese

usin

g:

arr

ays

Ne

ed

tosim

ula

tein

se

rt/d

ele

teu

sin

gp

oin

ters

lists

Ea

ch

inse

rt/d

ele

tein

volv

es

me

mo

rya

lloca

tio

n

INF

421,

Lectu

re2

–p.12

Page 13: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Circulararrays

Imp

lem

en

tatio

nu

sin

ga

circulararrayq

[Mehlh

orn

&S

anders

’book]

Use

sm

od

ula

ra

rith

me

tic

(usu

ally

pre

tty

fast)

d0

d1

d2

d3

q 0

q h

q tn

qh=

d0

(firs

t

ele

mentofqueue)

qt=

⊥delim

its

the

end

ofth

e

queue

qi

isunused

for

0≤

i<

hand

t<

i≤

n

actu

al

imple

men-

tation

of

acircula

r

arr

ay

issim

ply

an

arr

ay;

but

the

be-

havio

ur

will

be

dif-

fere

nt

INF

421,

Lectu

re2

–p.13

Page 14: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Readbeginningofqueue

first(){

return

q h;

}

d0

d1

d2

d3

q 0

q h

q tn

INF

421,

Lectu

re2

–p.14

Page 15: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Deletebeginningofqueue

popFront(){

p=

q h;

h=

(h+1)

mod(n

+1)

;return

p;

}

d0

d1

d2

d3

q 0

q h

q tn

INF

421,

Lectu

re2

–p.15

Page 16: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Insert

attheendofqueue

pushBack(d){

assert(size()

<n)

q t=

d;

t=

(t+1)

mod(n

+1)

;q t

=⊥

;

}

d0

d1

d2

d3

d

q 0

q h

q t

n

INF

421,

Lectu

re2

–p.16

Page 17: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Insert

attheend(case

t=n)

d0

d1

d2

d3

d4

d5

d6

d7

d8

q 0

q h

t=

n

d0

d1

d2

d3

d4

d5

d6

d7

d8

d

t=

0

q h

q n

t=

(t+1)

mod(n

+1)

witht=

nim

plie

st=

0

INF

421,

Lectu

re2

–p.17

Page 18: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Sizeandem

ptiness

isEmpty()

:if(t

=h)thenreturn

true;elsereturn

false;

size()

:return

(t−h+n+1)

mod(n

+1)

;

q 0

h=

t

q n

em

ptiness

d3

d4

⊥d0

d1

d2

q 0

q tq h

q n

siz

e

INF

421,

Lectu

re2

–p.18

Page 19: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

BFS

INF

421,

Lectu

re2

–p.19

Page 20: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

TheBFSalgorithm

Input:

se

tV

,b

ina

ryre

latio

n∼

onV

,a

nds6=

t∈V

1:(Q

,<)={s}

;L={s}

2:whileQ6=

∅do

3:

u=

min

<Q

;4

:Q←

Qr{u};

5:

forv∈V

(v∼

u∧v6∈L)do

6:

ifv=

tthen

7:

return

tre

ach

able

;8

:endif

9:

Q←

Q∪{v},se

tv=

max

<Q

;1

0:

L←

L∪{v};

11

:endfor

12

:endwhile

13

:return

tu

nre

ach

able

;

INF

421,

Lectu

re2

–p.20

Page 21: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Theorder

onQ

Th

eo

rde

red

se

tQ

isim

ple

me

nte

da

sa

qu

eu

e

Eve

ryv∈V

en

ters

Qa

sth

emaxim

um

ele

me

nt

(i.e

.,

the

last)

We

on

lyre

ad

(an

dre

move

)th

eminim

um

ele

me

nto

fQ

(i.e

.th

efirs

t)

Eve

ryo

the

re

lem

en

to

fQ

isn

eve

rto

uch

ed

Th

ere

lative

ord

er

ofa

co

nse

cu

tive

su

bse

qu

en

ce

u1,...,u

ho

fQ

isu

nch

an

ge

d

Als

o,

by

the

testv6∈L

at

Ste

p5

,w

eh

ave

:T

hm

.1

No

ele

me

nt

ofV

en

ters

Qm

ore

tha

no

nce

INF

421,

Lectu

re2

–p.21

Page 22: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Anodehierarchy

Co

nsid

er

afu

nctio

nα:V→

Nd

efin

ed

as

follo

ws:

at

Ste

p1

,le

tα(s)=

0

at

Ste

p9

,le

tα(v)=

α(u)+1

Th

isra

nks

the

ele

me

nts

ofV

by

dis

tan

ce

fro

ms

inte

rms

of

rela

tio

np

airs

E.g

.ifs∼

u,

the

nu’s

dis

tan

ce

fro

ms

is1

ifs∼

u∼

v,v’s

dis

tan

ce

fro

ms

is2

INF

421,

Lectu

re2

–p.22

Page 23: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

TheBFS,again

1:(Q

,<)={s}

;L={s}

2:

α(s)=

0

3:whileQ6=

∅do

4:

u=

min

<Q

;5

:Q←

Qr{u};

6:

forv∈V

(v∼

u∧v6∈L)do

7:

α(v)=

α(u)+1

8:

ifv=

tthen

9:

return

tre

ach

able

;1

0:

endif

11

:Q←

Q∪{v},se

tv=

max

<Q

;1

2:

L←

L∪{v};

13

:endfor

14

:endwhile

15

:return

tu

nre

ach

able

;IN

F421,

Lectu

re2

–p.23

Page 24: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Basicresults

We

have

the

follo

win

gre

su

lts

(try

an

dp

rove

the

m):

Th

m.

2

If(s,v

1,...,v

k)

isa

ny

itin

era

ryfo

un

dby

BF

S,α(v

k)=

k

Th

m.

3

Ifα(u)<

α(v),

the

nu

en

ters

Qb

efo

rev

do

es

Th

m.

4

No

itin

era

ryfo

un

dby

BF

Sh

as

rep

ea

ted

ele

me

nts

Th

m.

5

Th

efu

nctio

isw

ell

de

fin

ed

INF

421,

Lectu

re2

–p.24

Page 25: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Few

estchanges

Aim

top

rove

tha

tBFSfindsanitinerary

withfewestchanges

Remark

:th

enu

mb

er

of

ch

an

ge

sin

an

itin

era

ryis

the

sa

me

as

the

nu

mb

er

of

no

de

sin

tha

titin

era

ry,

he

nce

:T

hm

.

BF

Sfin

ds

ash

ort

est

itin

era

ry

Ideaofproof:

found b

y B

FS

short

est

01

ℓh−

1ℓ+1<h

ℓ+

1=

h

s

uℓ

v h−1

v h

α

INF

421,

Lectu

re2

–p.25

Page 26: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Theproof

Th

m.

BF

Sfin

ds

ash

ort

est

itin

era

ry(in

term

so

fnu

mb

er

of

no

de

s)

Pro

of

Le

tR

=(s,v

1,...,v

k=

t)b

eth

eitin

era

ryfo

un

dby

BF

S:

su

pp

ose

itis

no

tsh

ort

est.

Le

th≤

kb

eth

esm

alle

stin

dex

su

ch

tha

tR

′=

(s,...,v

h−1,v

h)

isn

otsh

ort

estfr

om

s→

v h.

By

Th

m.

2,α(v

h)=

h.

Sin

ce

this

itin

era

ryis

no

tsh

ort

est,

the

rem

ust

be

ad

iffe

ren

titin

era

ryP

=(s,u

1,...,u

ℓ,v

h)

wh

ich

issh

ort

est:

the

nn

ece

ssa

rily

we

have

ℓ+1<

h.

By

Th

m.

2a

ga

inw

eh

ave

α(u

ℓ)=

ℓ.S

inceℓ<

h,uℓ

en

ters

Qb

efo

rev h

do

es

(Th

m.3

),so

the

itin

era

ryP

isfo

un

dby

BF

Sb

efo

reR

′.

Now

uℓ∼

v h+1

yie

ldsα(v

h)=

ℓ+1<

h=

α(v

h),

wh

ich

isa

co

ntr

ad

ictio

n.

INF

421,

Lectu

re2

–p.26

Page 27: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Findingallshortestitineraries

De

lete

Ste

ps

8-1

0

All

ele

me

nts

inV

en

ter

an

dexitQ

Fin

ds

sh

ort

est

itin

era

rie

sfr

om

sto

all

ele

me

nts

ofV

WARNING

:B

FS

will

not

fin

dsh

ort

est

pa

ths

ina

we

igh

ted

gra

ph

un

less

all

the

arc

co

sts

are

1

INF

421,

Lectu

re2

–p.27

Page 28: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Whataboutfastest?

Eve

rytim

ew

ein

se

rtv

at

the

en

do

fQ

,w

ea

lso

reco

rdth

ea

rriv

altim

ea

tv

usin

gth

etr

ave

llin

gin

form

atio

na

bo

utth

ere

latio

nu∼

v

We

ke

ep

tra

ck

ofth

eminim

um

timeτ′ove

ra

lle

lem

en

tso

fQ W

he

neve

rw

eextr

act

the

arr

iva

ln

od

et

fro

mQ

,w

eh

ave

ap

ossib

leitin

era

rys→

t;a

mo

ng

the

se

itin

era

rie

s,w

eke

ep

tra

ck

be

st

arr

iva

ltim

eτ∗

tot

so

far

We

up

da

teτ∗

wh

en

eve

rw

efin

dn

ew

itin

era

rie

ss→

t

As

so

on

asτ′≥

τ∗,

we

kn

ow

we

have

ash

ort

est

itin

era

ry(w

hy?

)

INF

421,

Lectu

re2

–p.28

Page 29: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Implementation

INF

421,

Lectu

re2

–p.29

Page 30: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Apossibleim

plementation

1:L

initia

lize

dto{s}

;2

:Q

isa

ne

mp

tyq

ue

ue

;3

:α(s)=

0;4

:Q.pushBack(s);

5:while¬(Q

.isEmpty())do

6:

u=

Q.popFront()

;7

:forv∈V

(v∼

u∧v6∈L)do

8:

α(v)=

α(u)+1

9:

ifv=

tthen

10

:return

α(v);

11

:endif

12

:Q.pushBack(v);

13

:L.pushBack(v);

14

:endfor

15

:endwhile

16

:return

tu

nre

ach

able

;IN

F421,

Lectu

re2

–p.30

Page 31: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Itsproblems

How

effi

cie

ntly

ca

nyo

uim

ple

me

nt

Ste

p7

?

forv∈V

(v∼

u∧v6∈L)do

Lo

op

ing

ove

rV

:w

ors

tca

seO(|V|)

Fo

re

achv,

ch

eck

wh

eth

erv∼

u

with

aja

gged

arr

ay,

theu

-th

row

can

have

siz

eat

wors

tO(|V|)

,if

eve

ryv

isin

rela

tion

withu

ifw

ekeep

am

apA

:V×

V→{0,1}

such

thatA(u,v)=

1

wheneve

ru∼

vand

0oth

erw

ise,w

ere

duce

this

toO(1)

Fo

re

achv,

als

och

eck

wh

eth

erv6∈L

:w

ors

tca

se

O(|V|)

(wh

en

mo

st

no

de

so

fV

have

be

en

pu

tin

toL

)

Aw

ors

tca

se

co

mp

lexity

ofO(|V|(1+|V|))=

O(|V|2)

Re

pe

ate

din

the

exte

rna

llo

op

:g

etO(|V|3)

ove

rall:

ugh!

INF

421,

Lectu

re2

–p.31

Page 32: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Amore

efficientalternative

We

initia

lize

the

no

de

ran

kin

gfu

nctio

so

tha

tα(u)=|V|+

1fo

ra

llu∈Vr{s}

be

fore

Ste

p5

Re

ma

rkth

at,

wh

en

eve

rα(v)

isu

pd

ate

da

tS

tep

8,

its

valu

eis

,by

ind

uctio

nfr

om

α(s)=

0,a

lways≤|V|

Sin

cev

isin

se

rte

din

toL

afte

rα(v)

isu

pd

ate

da

tS

tep

8,

for

ea

chv∈V

we

have

tha

tv∈L

ifa

nd

on

lyif

α(v)≤|V|

Ch

an

ge

loo

pa

tS

tep

7a

sfo

llow

s:

forv∈V

(v∼

u∧α(v)=|V|+

1)do

Now

wo

rst-

ca

se

co

mp

lexity

isO(|V|)

(ta

ble

loo

k-u

pin

O(1))

He

nce,

we

have

O(|V|2)

ove

rall

INF

421,

Lectu

re2

–p.32

Page 33: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Abetterworst-case

analysis

The

inte

rnallo

op fo

rv∈V

(v∼

u∧α(v)=|V|+

1)do

only

loops

ove

rre

lation

pairs(u,v)

Becauseu

isneve

rconsid

ere

dm

ore

than

once

by

Thm

.1,it

follo

ws

that

no

pair(u,v)

iseve

rconsid

ere

dm

ore

than

once

ove

rall

itera

tions

w.r

.t.

both

loops

At

wors

t,th

ein

str

uctionQ.pushBack(v)

can

only

be

repeate

das

many

tim

es

as

there

are

pairs

inth

ere

lation∼

More

ove

r,w

eexe

cute

the

body

of

the

oute

rlo

op

atle

ast|V|tim

es

Asym

pto

tically

,w

ecannot

com

pare

n,m

:it

really

depends

on

the

gra

ph

⇒T

he

wors

t-case

com

ple

xity

of

BF

Sis

thenO(|V|+|∼|)

INF

421,

Lectu

re2

–p.33

Page 34: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

BFS:aFrench

publicationrecord

INF

421,

Lectu

re2

–p.34

Page 35: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

History

ofBFS

BF

Sw

as

“dis

cove

red

”by

seve

ralp

eo

ple

,a

nd

no

-on

eq

uite

kn

ow

sw

ho

wa

sth

efirs

t

Th

efirs

tofficialpublication

BF

Sis

usu

ally

po

inte

do

utto

be

ap

ap

er

wri

tte

nby

E.F

.M

oo

re,

ca

lled

Theshortestpath

throughamaze,w

hic

ha

pp

ea

red

inth

ep

roce

ed

ing

so

fa

19

59

co

nfe

ren

ce

Infa

ct,

the

bo

ok

Th

éo

rie

de

sgra

ph

es

etse

sa

pp

lica

tio

ns

by

Cla

ud

eB

erg

e,

pu

blis

he

din

19

58

,co

nta

ins

ad

escri

ptio

no

fB

FS

ap

plie

dto

fin

din

gsh

ort

est

pa

ths

INF

421,

Lectu

re2

–p.35

Page 36: INF421, Lecture 2 Queuesliberti/teaching/... · Course Objective: to teach you some data structures and associated algorithms Evaluation: TP noté en salle info le 16 septembre, Contrôle

Berge’streatm

ent

INF

421,

Lectu

re2

–p.36