





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






























Let
) be a directed or undirected graph,
and suppose that BFS is run on
from a given
source vertex
s
. Then, during its execution,
BFS discovers every vertex
v
that is reachable
from the source
s
, and upon termination
d [ v
δ
( s
v ) for all
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 ).
Let
u
be the vertex immediately preceding
v
on a
shortest path from
s
to
v
, so that
δ ( s
v ) =
δ
( s
u ) + 1.
δ
( s
u ) <
δ
( s
,^ v
), and because of how
we chose
v
, we have
d
[ u
δ
( s
u ).
d [ v
δ
( s
v ) =
δ
( s
,^ u
d
[ u
dequeue vertex
u
from
in line 11.
v
is, white, gray, or black.
derive a contradiction to inequality (22.1).
v
is white, then line 15 sets
d
[ v
d
[ u
contradicting inequality (22.1).
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).
v
is gray, then it was painted gray upon dequeuing some vertex
w
, which was removed
from
earlier than
u
and,
d
[ v
d
[ w
The predecessor subgraph of a depth-first search forms a depth-first forest
composed of several
depth-first trees
defined as
π^
, π
), where π
π^
π [
v ],
v
v
and
π
[ v
the edges in
π^ 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.
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]
u^
v^
w
x^
y^
z
For each vertex u
color
u ]
π^
[u]
time
v^
w
x^
y^
z
Considering white vertex
u
color
[ u
]^
GRAY
d[u]
time
Adj[u] = v, xcolor
[ v
] = WHITE π [
v ]
←
u
DFS-VISIT (
v )
u^
v^
w
x^
y^
z
color
[ y
]^
GRAY
d[y]
time
Adj[y] = xcolor
[ x
] = WHITE π [
x ]
←
y
DFS-VISIT (
x )
u^
v^
w
x^
y^
z
color
x ]
d[x]
time
Adj[x] = vcolor
v ]
u^
v^
w
x^
y^
z
B
The vertex
x
is finished.
color
x ]
f[x]
time
u^
v^
w
x^
y^
z
B
The vertex
y is finished.
color
[ y
]^
BLACK
f[y]
time
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
u^
v^
w
x^
y^
z
B
F
The vertex
u
is finished.
color
u ]
f[u]
time