Professional Documents
Culture Documents
Two common elementary algorithms for tree-searching are Breadth-rst search (BFS), and Depth-rst search (DFS). Both of these algorithms work on directed or undirected graphs. Many advanced graph algorithms are based on the ideas 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.
Breadth-First Search
Let
A breadth-rst 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-rst tree rooted at . That is, a tree whose edges form the shortest paths from to all vertices reachable. Breadth-rst search is so called because it locates all vertices of distance before it locates any of distance .
Let
be a graph.
1 ) 20
@ 8 6 A975
!
BFS Algorithm
Let vertex.
be a graph, and
be some
BFS Example
s 0 b c d v s a b c d e v s a b c d e v s a b c d e v s a b c d e adjacent a,b s,b,c,e s,a,c,e b,a,e e a,b,c,d adjacent a,b s,b,c,e s,a,c,e b,a,e e a,b,c,d adjacent a,b s,b,c,e s,a,c,e b,a,e e a,b,c,d adjacent a,b s,b,c,e s,a,c,e b,a,e e a,b,c,d d 0 inf inf inf inf inf d 0 1 1 inf inf inf d 0 1 1 2 inf 2 d 0 1 1 2 inf 2 p nil nil nil nil nil nil p nil s s nil nil nil p nil s s a nil a p nil s s a nil a color gray white white white white white color black gray gray white white white color black black gray gray white gray color black black black gray white gray
Q=[s]
a s
e 0 b 1 1 0 b 1 1 0 b 1 1 2 c 2 2 c 2 c d
Q=[a,b]
a s
e d
Q=[b,c,e]
a s
e d
Q=[c,e]
Q=[c,e]
a s
e d
Q=[e]
a s
e d
Q=[d]
a s
e d
Q=[]
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-rst tree after we have executed BFS? What is the complexity of BFS? It is not hard to convince yourself that the rst 3 are true, although proving them is a little harder.
` Y
It is not too difcult to see that the complexity is . We wont prove any of these things explicitly.
Depth-First Search
Let
A depth-rst search of will Find every vertex in . Timestamp every vertex with the rst time it was discovered, and the last time it is examined. Construct a depth-rst forest. That is, a partition of into depth-rst trees. The timestamps are used by other graph algorithms. Depth-rst search is so called because it continues along a path until it nds 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.
I I
be a graph.
10
Since both and are guaranteed to be set in the algorithm, we need not initialize them.
% &$
!
, and
11
Notice that
i1
i1
f e)
f e)
s t
f hg)
f hg)
@ 8 6 d975 8 q Urp
b
b
bRU @
f e)
f e)
!
! @
12
DFS Algorithm
Let
DFS-Visit is 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)=black f(u)=time++
be a graph.
13
DFS Example
a
1/
f h e
b a
1/
c f h
2/
b a
c f h
v a p NIL d 1 f c G
b a 2
G W W W W W W
1/
2/
3/
v a p NIL d 1 f c G
b a 2
c 3
G G W W W W W
a
1/
f h
2/
3/
v a p NIL d 1 f c G
b a 2
c b 3
d c 4
G G G W W W W
14
a
1/
f h e
g time=4
4/
b a
2/
3/
1/
f h e
d g time=5
v a p NIL d 1 f c G
b a 2
c b 3
d c 4
G G G W W W W g d 5 h
NIL
5/
b a
2/
3/
4/
1/
f h e
d g time=6
G W
5/6
b a
2/
3/
4/
1/
f h e
d g time=7
5/6
b a
2/
3/
4/7
1/ 8/ 2/
f h
3/
d g time=8
5/6
4/7
15
a
1/ 8/ 2/
f h
3/
g
5/6
time=8
4/7
b a
1/ 8/ 2/
f h
3/
d g time=8
d e f c NIL NIL 4 7 G G B W W b a 2 c b 3 d e f c NIL NIL 4 7 G G B W W b a 2 c b 3 d e f c NIL NIL 4 7 G G B W W b a 2 c b 3 c b 3 10 G B b a 2 b a 2 11 B c b 3 10 B d e f c NIL NIL 4 7 B W W d e f c NIL NIL 4 7 B W W
g h d c 5 8 6 B G g h d c 5 8 6 B G g h d c 5 8 6 9 B B g h d c 5 8 6 9 B B g h d c 5 8 6 9 B B
5/6
b a
4/7
1/ 8/9 2/
f h
3/
d g time=9
5/6
b a
4/7
1/ 8/9 2/
f h
3/10
d g time=10
5/6
b a
4/7
1/ 8/9 2/11
f h
3/10
d g time=11
5/6
4/7
16
a
1/ 8/9 2/11
f h
3/10
g
5/6
time=11
4/7
b a
f h
3/10
d g time=12
v a p NIL d 1 f c G
b a 2 11 B
c b 3 10 B c b 3 10 B c b 3 10 B c b 3 10 B c b 3 10 B
g h d c 5 8 6 9 B B g h d c 5 8 6 9 B B g h d c 5 8 6 9 B B g h d c 5 8 6 9 B B
1/12
5/6
8/9 2/11
b a
4/7
f h
3/10
d g time=13
5/6
13/
e
4/7
b a
f h
d g time=14
14/ 13/
3/10
5/6
e
4/7
b a
f h
d g time=15
14/15 13/
3/10
5/6
e
4/7
d e f g h c NIL e d c 4 13 14 5 8 7 15 6 9 B G B B B
17
f h
g
5/6
14/15 13/
3/10
e
4/7
time=15 d g
5/6
v a b p NIL a d 1 2 f 12 11 c B B
c b 3 10 B
d e f g h c NIL e d c 4 13 14 5 8 7 15 6 9 B G B B B
a
1/12 8/9 2/11
f h
14/15 13/16
3/10
e
4/7
time=16 d
v a b p NIL a d 1 2 f 12 11 c B B
c b 3 10 B
d e f g h c NIL e d c 4 13 14 5 8 7 16 15 6 9 B B B B B
a h
f e
18
DFS Summary
The edges depth-rst forest.
As stated previously, the timestamps are useful in other algorithms. There are several other things that can be said about depth-rst searching, but we wont. Instead, we will discuss one application, the Topological Sort.
19
Topological Sort
Let
A topological sort of is a linear ordering of the vertices of such that if is before , then is not an edge. Example: Consider the following Graph:
D I
20
When DFS is done, the linked list contains the vertices of topologically sorted. Example:
A
1/16
4/5
2/15
E C
17/18
F E
2/15
3/14
A
1/16
F
3/14
at the head of
17/18
11/12
I
8/9
6/13
G G
6/13
H D
11/12
7/10
H
7/10
I
8/9
B
4/5