Transcript V - Go into The Algorithm
Elementary Graph Algorithms Heejin Park
College of Information and Communications Hanyang University
Breadth-first search Content Depth-first search
Breadth-first search
Distance
Distance from
u
to
v
The number of edges in the shortest path from
u
to
v
.
The distance from
s
to
v
is 2.
r s t u v w x y
Breadth-first search Breadth-first search
Given a graph
G
= (
V, E
) and a
source
vertex
s,
it explores the edges of
G
to "discover" every reachable vertex from
s.
It discovers vertices in the increasing order of distance from the source. It first discovers all vertices at distance 1, then 2, and etc.
r s t u
∽ 0 ∽ ∽ ∽
v
∽
w
∽
x
∽
y
Breadth-first search Breadth-first search
Given a graph
G
= (
V, E
) and a
source
vertex
s,
it explores the edges of
G
to "discover" every reachable vertex from
s.
It discovers vertices in the increasing order of distance from the source. It first discovers all vertices at distance 1, then 2, and etc.
r s t u
1 0 ∽ ∽ ∽
v
1
w
∽
x
∽
y
Breadth-first search Breadth-first search
Given a graph
G
= (
V, E
) and a
source
vertex
s,
it explores the edges of
G
to "discover" every reachable vertex from
s.
It discovers vertices in the increasing order of distance from the source. It first discovers all vertices at distance 1, then 2, and etc. 1
r s
0 2
t u
∽ 2
v
1
w
2
x
∽
y
Breadth-first search Breadth-first search
Given a graph
G
= (
V, E
) and a
source
vertex
s,
it explores the edges of
G
to "discover" every reachable vertex from
s.
It discovers vertices in the increasing order of distance from the source. It first discovers all vertices at distance 1, then 2, and etc.
r s t u
1 0 2 3 2
v
1
w
2
x
3
y
Breadth-first search Breadth-first search
It also computes the distance of vertices from the source:
d
[
u
] = 3 the predecessor of vertices: π[
u
] =
t
1
r
0
s
2
t u
3 2
v
1
w
2
x
3
y
Breadth-first search
The
V π
predecessor subgraph
of
G
as
G π
= {
v
∈
V
:
π
[
v
] ≠ NIL} U {
s
}
E π
= {(
π
[
v
],
v
) :
v
∈
V π
- {
s
}}.
= (
V π
,
E π
), 2
v
1
r
1
w
0
s
2
x
2
t
3
y u
3
Breadth-first search
The predecessor subgraph
G π
is a
breadth-first tree.
since it is connected and |
E π
| = |
V π
| -1.
The edges in
E π
are called
tree edges
.
2
v
1
r
1
w
0
s
2
x
2
t
3
y u
3
Breadth-first search
BFS(
G
,
s
) 1
for
each vertex
u V
[
G
] - {
s
} 2 do color [
u
] ← WHITE 3
d
[
u
] ← ∞ 4
π
[
u
] ← NIL 5
color
[
s
] ← GRAY 6
d
[
s
] ← 0 7
π
[
s
] ← NIL 8
Q
← Ø 9 ENQUEUE(
Q
,
s
) 10 while Q ≠ Ø 11 do u ← DEQUEUE(
Q
) 12
for
each
v
∈
Adj
[
u
] 13 do if color [
v
] = WHITE 14 then color [
v
] ← GRAY 15
d
[
v
] ←
d
[
u
] + 1 16
π
[
v
] ←
u
17 ENQUEUE(
Q
,
v
) 18
color
[
u
] ← BLACK
Breadth-first search
r
∽ 0
s
∽
v
Q
s
0 ∽
w t
∽ ∽
x u
∽ ∽
y u v w x y r s t s r u t r s t u v w w x t u x x y x x y
Breadth-first search
1
r
∽
v
Q
w r
1 1 1
w
0
s t
∽ ∽
x u
∽ ∽
y u v w x y r s t
white: not discovered (not entered the Q) gray: discovered (in the Q) black: finished (out of the Q)
s r u t r s t u v w w x t u x x y x x y
Breadth-first search
1
r s
0 ∽
v
1
w
Q
r t x
1 2 2 2
x
2
t u
∽ ∽
y u v w x y r s t s r u t r s t u t u x v w w x x y x x y
Breadth-first search
1
r s
0 2
v
1
w
Q
t x v
2 2 2 2
x
2
t u
∽ ∽
y u v w x y r s t s r u t r s t u t u x v w w x x y x x y
Breadth-first search
1
r s
0 2
v
1
w
Q
x v u
2 2 3 2
x
2
t u
3 ∽
y u v w x y r s t s r u t r s t u t u x v w w x x y x x y
Breadth-first search
1
r s
0 2
v
1
w
Q
v u y
2 3 3 2
x
2
t
3
y u
3
u v w x y r s t s r u t r s t u t u x v w w x x y x x y
Breadth-first search
1
r
2
v
Q
u y
3 3 1
w s
0 2
x
2
t
3
y u
3
u v w x y r s t s r u t r s t u t u x v w w x x y x x y
Breadth-first search
1
r
2
v
Q
y
3 1
w s
0 2
x
2
t
3
y u
3
u v w x y r s t s r u t r s t u t u x v w w x x y x x y
Breadth-first search
1
r
2
v
Q ¢ 3 1
w s
0 2
x
2
t
3
y u
3
u v w x y r s t s r u t r s t u t u x v w w x x y x x y
Breadth-first search
BFS(
G
,
s
) 1
for
each vertex
u V
[
G
] - {
s
} 2 do color [
u
] ← WHITE 3
d
[
u
] ← ∞ 4
π
[
u
] ← NIL 5
color
[
s
] ← GRAY 6
d
[
s
] ← 0 7
π
[
s
] ← NIL 8
Q
← Ø 9 ENQUEUE(
Q
,
s
) 10 while Q ≠ Ø 11 do u ← DEQUEUE(
Q
) 12
for
each
v
∈
Adj
[
u
] 13 do if color [
v
] = WHITE 14 then color [
v
] ← GRAY 15
d
[
v
] ←
d
[
u
] + 1 16
π
[
v
] ←
u
17 ENQUEUE(
Q
,
v
) 18
color
[
u
] ← BLACK
Analysis
Running time Initialization
: Θ(
V
)
Exploring the graph
:
O
(
E
) An enque operation for an edge exploration. #deque operations = #enque operations An edge is explored at most once.
Overall: O (
V
+
E
)
Breadth-first search
Content Depth-first search
Depth-first search
Colors of vertices
Each vertex is initially
white
. (not discovered) The vertex is
grayed
when it is
discovered.
The vertex is
blackened
when it is
finished
, that is, when its adjacency list has been examined completely.
u v w x y z
Depth-first search
Timestamps
Each vertex
v
has two timestamps.
d
[
v
]:
discovery time
(when
v
is grayed)
f
[
v
]:
finishing time
(when
v
is blacken)
w u
1/ 4/5
x v
2/ 3/
y z
u
1/
v
Depth-first search
w u
1/
x u
1/
x y
(a)
v
2/ 3/
y
(c)
w z z v
2 /
w x u
1/ 4/
x y
(b)
v
2/ 3/
y
(d)
z w z
u
1/ 4/
x u
1/ 4/5
x
3/
y
(e)
v
2/
v
2/
Depth-first search
w u
1/
v
2/
z w
4/5
x u
1/ 3/
y
(f)
v
2/7 3/6
y
(g)
z
4/5
x
3/6
y
(h)
z w w z
u
1/8 4/5
x u
1/ 4/5
x v
2/7
Depth-first search
w u
1/8
v
2/7 3/6
y
(i)
v
2/7
z w
9/ 4/5
x u
1/8 3/6
y
(j)
v
2/7 3/6
y
(k)
z
4/5
x
3/6
y
(l)
z w
9/
w z
u
1/8 4/5
x u
1/8 4/5
x v
2/7
Depth-first search
w
9/
u
1/8
v
2/7 3/6
y
(m)
v
2/7 10/
z w
9/ 4/5
x u
1/8 3/6
y
(n)
v
2/7 3/6
y
(o) 10/11
z
4/5
x
3/6
y
(p)
w
9/ 10/
z w
9/12 10/11
z
Depth-first search
The
predecessor subgraph
is a
depth-first forest
.
u v y x w z
Depth-first search Parenthesis theorem (for gray time)
Inclusion
: The ancestor includes the descendants.
Disjoint
: Otherwise.
u w v y x z u v y w z
1 (
u
(
v
2 3 (
y
4 (
x x
5
x
) 6
y
) 7
v
) 8 9
u
) (
w
10 (
z
11
z
) 12
w
)
Depth-first search Classification of edges
Tree edges
Back edges
Forward edges
Cross edges
u v
B
y
F C
w z x
Depth-first search
Tree edges:
Edges in the depth-first forest.
Back edges
: Those edges (
u
,
v
) connecting a vertex
u
to an ancestor
v
in a depth-first tree. Self-loops are considered to be back edges.
Forward edges
: Those edges (
u
,
v
) connecting a vertex
u
to a descendant
v
in a depth-first tree.
Cross edges
: All other edges. They can go between vertices in the same depth-first tree, as long as one vertex is not an ancestor of the other, or they can go between vertices in different depth-first trees.
Depth-first search
Classification by the DFS algorithm Each edge (
u
,
v
) can be classified by the color of the vertex
v
that is reached when the edge is first explored: WHITE indicates a tree edge, GRAY indicates a back edge, and BLACK indicates a forward or cross edge.
Depth-first search
u v w z y
1 (
u
(
v
2 3 (
y
4 (
x x
5
x
) 6
y
) 7
v
) 8 9
u
) (
w
10 (
z
11
z
) 12
w
)
u v
B
y
F C
w z x
u
1/ B 4/
x v
2/
u
1/ B 4/5
x
3/6
y
(g) 3/
y
(e)
v
2/
Depth-first search
w u
1/ B
v
2/
z
4/5
x
3/
y
(f)
w u
1/
v
2/7 B
z
4/5
x
3/6
y
(h)
z w w z
F
u
1/ B 4/5
x v
2/7 3/6
y
(i)
u
1/8 F 4/5
x
B
v
2/7 3/6
y
(k)
Depth-first search
w z u
1/8 F B 4/5
x v
2/7 3/6
y
(j)
w
9/
z u
1/8 F B 4/5
x v
2/7 3/6
y
(l) C
w
9/
w z z
u
1/8 F B 4/5
x
F
u
1/8 B 4/5
x v
2/7 3/6
y
(m) C
Depth-first search
w
9/ 10/
z
F
u
1/8 B 4/5
x v
2/7 3/6
y
(o)
w
9/ C 10/11
z
B F
u
1/8 B 4/5
x v
2/7
w
9/ C B 3/6
y
(n)
v
2/7 C 10/
z w
9/12 3/6
y
(p) 10/11
z
B
Depth-first search
In a depth-first search of an
undirected graph
, every edge of G is either a
tree edge
or a
back edge
.
Forward edge?
Cross edge?