Docsity

Proof Breadth First Search Algorithm-Advance Analysis Design-Lecture Slides, Slides of Design and Analysis of Algorithms

Partial preview of the text

Download Proof Breadth First Search Algorithm-Advance Analysis Design-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Lecture No. 29

Proof (Breadth First Search Algorithm)

Depth First Search

Statement

Let

G

V

,^ E

) be a directed or undirected graph,

and suppose that BFS is run on

G

from a given

source vertex

s

V

. Then, during its execution,

BFS discovers every vertex

v

V

that is reachable

from the source

s

, and upon termination

d [ v

] =

δ

( s

,^

v ) for all

v

V

Moreover, for any vertex

v

s

that is reachable from

s , one of the shortest paths from

s

to

v

is a shortest

path from

s

to

π

[ v

] followed by edge (

π

[ v

],^

v ).

Theorem (Correctness of BFS)

–^

Let

u

be the vertex immediately preceding

v

on a

shortest path from

s

to

v

, so that

δ ( s

,^

v ) =

δ

( s

,^

u ) + 1.

  • Because

δ

( s

,^

u ) <

δ

( s

,^ v

), and because of how

we chose

v

, we have

d

[ u

] =

δ

( s

,^

u ).

  • Putting these properties together, we have

d [ v

] >

δ

( s

,^

v ) =

δ

( s

,^ u

d

[ u

] +

  • Now consider the time when BFS chooses to

dequeue vertex

u

from

Q

in line 11.

Theorem (Correctness of BFS)

  • At this time, vertex

v

is, white, gray, or black.

  • We shall show that in each of these cases, we

derive a contradiction to inequality (22.1).

  • If

v

is white, then line 15 sets

d

[ v

] =

d

[ u

] + 1,

contradicting inequality (22.1).

  • If

v

is black, then it was already removed from the queue and, by Corollary 22.4, we have

d

[ v

]^

≤^

d [

u

],

again contradicting inequality (22.1).

  • If

v

is gray, then it was painted gray upon dequeuing some vertex

w

, which was removed

from

Q

earlier than

u

and,

d

[ v

] =

d

[ w

] + 1.

Theorem (Correctness of BFS)

•^

The predecessor subgraph of a depth-first search forms a depth-first forest

composed of several

depth-first trees

defined as

G

π^

V

, π

E

), where π

E

π^

π [

v ],

v

v

V

and

π

[ v

]^

≠^

NIL}

the edges in

E

π^ are called

tree edges.

•^

Each vertex is initially white–

It is grayed when it is

discovered

in the search, and

–^

It is blackened when it is

finished

, that is, when its

adjacency list has been examined completely.

Depth First Search

•^

It guarantees that each vertex ends up in exactly onedepth-first tree, so that these trees are disjoint.

-^

It

timestamps

each vertex

–^

the first timestamp

d

[ v

] records when

v

is first

discovered (and grayed), and

-^

the second timestamp

f^

[ v

] records when the search

finishes examining

v

's adjacency list (and blackens

v

•^

For every vertex

u

d[u] < f[u]

Discovery and Finish Times

u^

v^

w

x^

y^

z

Depth First Search

For each vertex u

V(G)

color

[

u ]

WHITE

π^

[u]

NIL

time

^0

u 1/

v^

w

x^

y^

z

Considering white vertex

u

color

[ u

]^ 

GRAY

d[u]

time

  • 1 = 0 + 1 = 1

Adj[u] = v, xcolor

[ v

] = WHITE π [

v ]

←

u

DFS-VISIT (

v )

Depth First Search

u^

v^

w

x^

y^

z

color

[ y

]^ 

GRAY

d[y]

time

  • 1 = 2 + 1 = 3

Adj[y] = xcolor

[ x

] = WHITE π [

x ]

←

y

DFS-VISIT (

x )

Depth First Search

u^

v^

w

x^

y^

z

color

[

x ]

GRAY

d[x]

time

Adj[x] = vcolor

[

v ]

WHITE

Depth First Search

u^

v^

w

x^

y^

z

B

The vertex

x

is finished.

color

[

x ]

BLACK

f[x]

time

Depth First Search

u^

v^

w

x^

y^

z

B

The vertex

y is finished.

color

[ y

]^ 

BLACK

f[y]

time

  • 1 = 5 + 1 = 6

Depth First Search

u^

v^

w

x^

y^

z

B

F

The edge

(u, x)

is a forward

edge that is a non tree edgeand is labeled as F

Depth First Search

u^

v^

w

x^

y^

z

B

F

The vertex

u

is finished.

color

[

u ]

BLACK

f[u]

time

Depth First Search