bfsanddfs (1) bfs dfs
Post on 14-Apr-2018
244 Views
Preview:
TRANSCRIPT
-
7/27/2019 BFSandDFS (1) bfs dfs
1/20
BFS and DFS 1
BFS and DFS
For many applications, one must systematically
search through a graph to determine the structure
of the graph.
Two common elementary algorithms for
tree-searching are
Breadth-first search (BFS), and
Depth-first search (DFS).
Both of these algorithms work on directed or
undirected graphs.
Many advanced graph algorithms are based on theideas of BFS or DFS.
Each of these algorithms traverses edges in the
graph, discovering new vertices as it proceeds.
The difference is in the order in which each
algorithm discovers the edges.
Instead of talking about the differences, we will
just look at each algorithm in turn.
-
7/27/2019 BFSandDFS (1) bfs dfs
2/20
BFS and DFS 2
Breadth-First Search
Let
be a graph.
Let
be a special vertex called the source.
A breadth-first search of
will:
Determine all vertices reachable from .
Determine the distance (that is, the fewest
number of edges) from to all vertices
reachable.
Determine a shortest path from to all vertices
reachable. Construct a breadth-first tree rooted at .
That is, a tree whose edges form the shortest
paths from to all vertices reachable.
Breadth-first search is so called because it locates
all vertices of distance
before it locates any of
distance .
-
7/27/2019 BFSandDFS (1) bfs dfs
3/20
BFS and DFS 3
How BFS Works
For each node in the graph we will need to store
the list of adjacent vertices,
the distance from to (
),
predecessor of (
), and
the color of (
).
The color of a vertex indicates the status of a
vertex:
A white vertex has not been discovered.
A gray vertex has been discovered, but it may
have undiscovered neighbors.
A black vertex has been discovered, and it has
no undiscovered neighbors.
The algorithm starts with
"
,
! # $ %
, and
! ' (
) 1
for 3
, and
4
,
# $ %
, and
5 6 8 @
.
-
7/27/2019 BFSandDFS (1) bfs dfs
4/20
BFS and DFS 4
How BFS Works Continued
Gray vertices are stored in a queue, B .
When the algorithm begins, is on the queue.
The algorithm proceeds as follows:
Remove the first element, , from the queue
( C
1 D F 1 F 1
B
).
For each neighbor@
of I
If@
is white.P Color
@
gray ( @ 5 6 8 @
).P Let the distance of
@
be one more than the
distance of
(
@
).P Let
be the predecessor of
@
(
@
).
P Place@
at the end of the queue
( V
D F 1 F 1
B
@
).I
If@
is gray or black, do nothing.
Color black.
Repeat until the queue is empty.
Now lets look at the whole algorithm.
-
7/27/2019 BFSandDFS (1) bfs dfs
5/20
BFS and DFS 5
BFS Algorithm
Let
be a graph, and
be some
vertex.
The breadth-first search algorithm is as follows:
BFS(G,s)
ForAll u in V-{s}
c(u)=white
d(u)=Max_Int
p(u)=NIL
c(s)=gray
d(s)=0
p(s)=NIL
Enqueue(Q,s)while(NotEmpty(Q))
u=Dequeue(Q)
ForAll v adjacent to u
if c(v) = white then
c(v)=gray
d(v)=d(u)+1
p(v)=uEnqueue(Q,v)
color(u) = black
-
7/27/2019 BFSandDFS (1) bfs dfs
6/20
BFS and DFS 6
BFS Example
e
s
a
d
cb
0
Q=[s] b s,a,c,e inf nil white
a s,b,c,e inf nil white
d e inf nil white
e a,b,c,d inf nil white
c b,a,e inf nil white
adjacent dp
colorv
e
s
a
d
cb
0adjacent d p colorv
1
1
2
2
a s,b,c,e 1 s black
c b,a,e 2 a gray
e a,b,c,d 2 a gray
e
s
a
d
cb
0adjacent d p colorv
1
1
2
2
a s,b,c,e 1 s black
Q=[b,c,e]
c b,a,e 2 a gray
e a,b,c,d 2 a gray
e
s
a
d
cb
0
d e inf nil white
e a,b,c,d inf nil white
c b,a,e inf nil white
adjacent d p colorv
1
1Q=[a,b]
a s,b,c,e 1 s gray
Q=[c,e]
d e inf nil white
d e inf nil white
b s,a,c,e 1 s gray
b s,a,c,e 1 s gray
b s,a,c,e 1 s black
s a,b 0 nil gray
s a,b 0 nil black
s a,b 0 nil black
s a,b 0 nil black
-
7/27/2019 BFSandDFS (1) bfs dfs
7/20
BFS and DFS 7
BFS Example Continued
e
s
a
d
cb
0 adjacent dp
colorv
1
1
2
2
a s,b,c,e 1 s black
c b,a,e 2 a gray
e a,b,c,d 2 a gray
Q=[c,e] b s,a,c,e 1 s black
e
s
a
d
cb
0
adjacent d p colorv
1
1
2
2
a s,b,c,e 1 s black
e a,b,c,d 2 a gray
c b,a,e 2 a black
b s,a,c,e 1 s black
e
s
a
d
cb
0adjacent d p colorv
1
1
2
2
a s,b,c,e 1 s black
b s,a,c,e 1 s black
3
e a,b,c,d 2 a black
c b,a,e 2 a black
e
s
a
d
cb
0adjacent d p colorv
1
1
2
2
a s,b,c,e 1 s black
b s,a,c,e 1 s black
3
e a,b,c,d 2 a black
c b,a,e 2 a black
Q=[d]
Q=[]
Q=[e]
d e inf nil white
d e 3 e grey
d e 3 e black
d e inf nil white
s a,b 0 nil black
s a,b 0 nil black
s a,b 0 nil black
s a,b 0 nil black
-
7/27/2019 BFSandDFS (1) bfs dfs
8/20
BFS and DFS 8
BFS Summary
There are several things we should ask about BFS:
Why does BFS discover every vertex
reachable from ?
Why does BFS give the distance from
to
,for every reachable vertex ?
Why/How does BFS give a shortest path from
to , for every reachable vertex ?
How do we construct a breadth-first tree after
we have executed BFS?
What is the complexity of BFS?
It is not hard to convince yourself that the first 3
are true, although proving them is a little harder.
It is not too difficult to see that the complexity isY
.
We wont prove any of these things explicitly.
-
7/27/2019 BFSandDFS (1) bfs dfs
9/20
BFS and DFS 9
Depth-First Search Let
be a graph.
A depth-first search of
will
Find every vertex in
.
Timestamp every vertex withI
the first time it was discovered, andI
the last time it is examined.
Construct a depth-first forest. That is, a
partition of
into depth-first trees.
The timestamps are used by other graph
algorithms.
Depth-first search is so called because it continues
along a path until it finds no new vertices, at
which point it backtracks.
Unlike BFS, DFS has no source vertex.
DFS starts searching from every vertex of the
graph eventually. We will see what this means.
-
7/27/2019 BFSandDFS (1) bfs dfs
10/20
BFS and DFS 10
How DFS Works
For each node in the graph we will need to store
the list of adjacent vertices,
the discover timestamp of (
).
the final timestamp of ( a
).
predecessor of
(
), and the color of (
).
The color of a vertex indicates the status of a
vertex:
A white vertex has not been discovered.
A gray vertex has been discovered, but it mayhave undiscovered neighbors.
A black vertex has been discovered, and it has
no undiscovered neighbors.
The algorithm starts with
! # $ %
, and
b ' (
) 1 for all
.
Since both
and a
are guaranteed to be set
in the algorithm, we need not initialize them.
-
7/27/2019 BFSandDFS (1) bfs dfs
11/20
BFS and DFS 11
More of How DFS Works
Unlike BFS, DFS is recursive.
When DFS visits a white node
, it does
the following:
Color gray (
5 6 8 @
).
Set discover time of
to the current time
(
) f 1 ).
Increment current time ( ) f 1
) f 1 ).
For all@
adjacent to I
If@
is white,P Set to be the predecessor of
@
(
@
).P Recursively visit node
@
.
Color
black (
p q 8
s).
Set finish time of to the current time
( a
) f 1 ).
Increment current time ( ) f 1
) f 1 ).
Notice that ) f 1 has to be a global variable.
We will now see the algorithm in its entirety.
-
7/27/2019 BFSandDFS (1) bfs dfs
12/20
BFS and DFS 12
DFS Algorithm
Let
be a graph.
The depth-first algorithm is as follows:
DFS(G)
ForAll u in V
c(u)=white
p(u)=NILtime=0
ForAll u in V
If c(u)=white
DFS-Visit(u)
DFS-Visitis as follows:
DFS-Visit(u)c(u)=gray
d(u)=time++
ForAll v adjacent to u
If c(v)=white
p(v)=u
DFS-Visit(v)
c(u)=blackf(u)=time++
An example should help make things clearer.
-
7/27/2019 BFSandDFS (1) bfs dfs
13/20
BFS and DFS 13
DFS Example
e
f g
NIL NIL NIL NIL
W W W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
2/
2
G G
1 3
4/3/
e
f g
W
NIL NIL NIL NIL NIL
W W W W
vp
c
d
f
a b c d e f g h
a
b c d
h
1/NIL
2/
2
a
G
b
1 3
3/
4a b c
e
f g
W W
NILNIL NIL NIL NIL NIL
W W W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
1
2/
2
a
e
f g
time=1
W W W
NIL NILNIL NIL NIL NIL NIL
W W W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
1
G
time=2
time=3
time=4
G
G
G
G
G
G
-
7/27/2019 BFSandDFS (1) bfs dfs
14/20
BFS and DFS 14
e
f g
NIL NIL NIL NIL
W W W W
v
p
c
df
a b c d e f g ha
b c d
h
1/
NIL
2/
2
G G
1 3
4/3/
4
a b c
time=4
G G
e
f g
NIL NIL NIL
W W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
2/
2
G G
1 3
4/3/
4
a b c
G
5/
time=5 5
d
G G
e
f g
NIL NIL NIL
W W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
2/
2
G G
1 3
4/3/
4
a b c
G
5
d
5/6
6
G
time=6
B
e
f g
NIL NIL NIL
W W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
2/
2
G G
1 3
3/
4
a b c
G
5
d
5/6
6
B4/7
time=7
7
B
e
f g
NIL NIL
W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
2/
2
G G
1 3
3/
4a b c
G
5d
5/6
6
B
7
B4/7
8/ time=8
c
8
G
-
7/27/2019 BFSandDFS (1) bfs dfs
15/20
BFS and DFS 15
e
f g
NIL NIL
W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
2/
2
G G
1 3
3/
4
a b c
G
5
d
5/6
6
B
7
B4/7
8/ time=8
c
8
G
e
f g
NIL NIL
W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
2/
2
G G
1 3
3/
4
a b c
G
5
d
5/6
6
B
7
B4/7
c
88/9 time=99
B
e
f g
NIL NIL
W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
2/
21 3 4
a b c
5
d
5/6
67
4/7
c
88/99
time=10
3/10 B
10
B B BGG
e
f g
NIL NIL
W W
v
p
c
df
a b c d e f g ha
b c d
h
1/
NIL
2/
2
G G
1 3
3/
4
a b c
G
5
d
5/6
6
B
7
B4/7
8/ time=8
c
8
G
e
f g
NIL NIL
W W
v
p
c
d
f
a b c d e f g ha
b c d
h
1/
NIL
21 3 4a b c
5d
5/6
67
4/7
c
88/99
3/10 B
10
B B BG
time=11
2/11 B
11
-
7/27/2019 BFSandDFS (1) bfs dfs
16/20
BFS and DFS 16
e
f g
NIL NIL
W W
v
p
c
df
a b c d e f g ha
b c d
h
1/
NIL
21 3 4
a b c
5
d
5/6
67
4/7
c
88/99
3/10 B
10
B B BG
time=11
2/11 B
11
e
f g
NIL NIL
W W
v
p
c
d
f
a b c d e f g ha
b c d
hNIL
21 3 4
a b c
5
d
5/6
67
4/7
c
88/99
3/10 B
10
B B BB
11time=12
B2/11
1/12
12
f g
NIL NIL
W
v
p
c
d
f
a b c d e f g ha
b c d
hNIL
21 3 4
a b c
5
d
5/6
67
4/7
c
88/99
3/10 B
10
B B BB
11
B2/11
1/12
12
13e
time=1313/
G
f g
NIL
v
p
c
d
f
a b c d e f g ha
b c d
hNIL
21 3 4a b c
5d
5/6
67
4/7
c
88/99
3/10 B
10
B B BB
11
B2/11
1/12
12
13e
13/
G
14time=15
14/15
B
15
f g
NIL
v
p
c
d
f
a b c d e f g ha
b c d
hNIL
21 3 4
a b c
5
d
5/6
67
4/7
c
88/99
3/10 B
10
B B BB
11
B2/11
1/12
12
13e
13/
14/
G G
14time=14
e
e
-
7/27/2019 BFSandDFS (1) bfs dfs
17/20
BFS and DFS 17
DFS Example Finish
f g
NIL
v
p
c
d
f
a b c d e f g ha
b c d
hNIL
21 3 4
a b c
5
d
5/6
67
4/7
c
88/99
3/10 B
10
B B BB
11
B2/11
1/12
12
13e
14
14/15
B
15
time=1616
13/16
f g
NIL
v
p
c
d
f
a b c d e f g ha
b c d
hNIL
21 3 4
a b c
5
d
5/6
67
4/7
c
88/99
3/10 B
10
B B BB
11
B2/11
1/12
12
13e
13/
G
14time=15
14/15
B
15
e
e
B
The depth-first forest for this example is
f ga
b c d
h e
-
7/27/2019 BFSandDFS (1) bfs dfs
18/20
BFS and DFS 18
DFS Summary
The running time of DFS isY
.
The edges
are the edges in the
depth-first forest.
As stated previously, the timestamps are useful in
other algorithms.
There are several other things that can be saidabout depth-first searching, but we wont.
Instead, we will discuss one application, the
Topological Sort.
-
7/27/2019 BFSandDFS (1) bfs dfs
19/20
BFS and DFS 19
Topological Sort
Let
be a directed acyclic graph.
A topological sort of
is a linear ordering of the
vertices of
such that if F is before u , then
u
F
is not an edge.
Example: Consider the following Graph:
A B C D
E F G H
I
Here is one possible topological sort:
C EA F H IG D B
Notice that no edges go from right to left.
-
7/27/2019 BFSandDFS (1) bfs dfs
20/20
BFS and DFS 20
Topological Sort Algorithm
Let
be a directed acyclic graph.
To topologically sort
, we use a slightly
modified version of DFS.
At the end ofDFS-Visit( F ), place F at the head of
a linked list.
When DFS is done, the linked list contains thevertices of
topologically sorted.
Example:
1/16
2/15 3/14
17/18
7/10
8/9
4/5
17/18
C
1/16
A
2/15
E
3/14
F
7/10
H
8/9
I
4/5
B
A B C D
I
GFE H6/13
11/12
11/126/13
G D
top related