You are on page 1of 20

BFS and DFS

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-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.

BFS and DFS

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 special vertex called the source.

be a graph.

BFS and DFS

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.
( ' ! % &$ # ! " !             

1 ) 20

@ 8 6 A975

! 

The algorithm starts with , , and for , and , , and


% &$ #       4 3      

BFS and DFS

How BFS Works Continued


When the algorithm begins, is on the queue. The algorithm proceeds as follows: Remove the rst element, , from the queue ( ). For each neighbor of If is white. Color gray ( ). Let the distance of be one more than the distance of ( ). Let be the predecessor of ( ). Place at the end of the queue ( ). If is gray or black, do nothing. Color black. Repeat until the queue is empty. Now lets look at the whole algorithm.
  @ SRU   @   @ 8 6 AT75     @ !RU  @ SRQ @   @ RX  @  B  B 1 F 1 F HWD 1 F 1 F D HGE1 @ @  V  C @ @ P P P P  I I  B

Gray vertices are stored in a queue,

BFS and DFS

BFS Algorithm
 

Let vertex.

be a graph, and

The breadth-rst 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)=u Enqueue(Q,v) color(u) = black

be some

BFS and DFS

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]

BFS and DFS

BFS Example Continued


s 0 b 1 1 0 b 1 1 0 b 1 1 0 b 1 1 2 c 2 2 c 2 3 2 c 2 3 2 c 2 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 1 1 2 inf 2 d 0 1 1 2 inf 2 d 0 1 1 2 3 2 d 0 1 1 2 3 2 p nil s s a nil a p nil s s a nil a p nil s s a e a p nil s s a e a color black black black gray white gray color black black black black white gray color black black black black grey black color black black black black black black

Q=[c,e]

a s

e d

Q=[e]

a s

e d

Q=[d]

a s

e d

Q=[]

BFS and DFS

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.

BFS and DFS

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.

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 nal timestamp of ( 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. .
        a         

The algorithm starts with for all


    a   1 ) Xc  ( ' b 

Since both and are guaranteed to be set in the algorithm, we need not initialize them.

% &$

!

, and

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 ( ). Set discover time of to the current time ( ). ). Increment current time ( For all adjacent to If is white, Set to be the predecessor of ( ). Recursively visit node . Color black ( ). Set nish time of to the current time ( ). ). Increment current time ( We will now see the algorithm in its entirety.
1 f  e)  

Notice that

has to be a global variable.

 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)

!

 

!  @

BFS and DFS

12

DFS Algorithm
Let

The depth-rst algorithm is as follows:


DFS(G) ForAll u in V c(u)=white p(u)=NIL time=0 ForAll u in V If c(u)=white DFS-Visit(u)

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++

An example should help make things clearer.

be a graph.

BFS and DFS

13

DFS Example
a
1/

f h e

g time=1 d g e time=2 d g e time=3 d g e time=4


4/

b a
1/

c f h
2/

v a b c d e f g h p NIL NIL NIL NIL NIL NIL NIL NIL d 1 f c G W W W W W W W

b a

c f h

v a p NIL d 1 f c G

b a 2

NIL NIL NIL NIL NIL NIL

G W W W W W W

1/

2/

3/

v a p NIL d 1 f c G

b a 2

c 3

b NIL NIL NIL NIL NIL

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

NIL NIL NIL NIL

G G G W W W W

BFS and DFS

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

NIL NIL NIL NIL

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

v a b c d e f p NIL a b c NIL NIL d 1 2 3 4 f c G G G G W W v a p NIL d 1 f c G v a p NIL d 1 f c G v a p NIL d 1 f c G b a 2 c b 3 d c 4 e f

G W

5/6

b a

2/

3/

4/

1/

f h e

d g time=7

g h NIL NIL d NIL 5 6 G G G W W B W 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 g h d NIL 5 6 B W g h d c 5 8 6 B G

5/6

b a

2/

3/

4/7

1/ 8/ 2/

f h
3/

d g time=8

5/6

4/7

BFS and DFS

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

v a p NIL d 1 f c G v a p NIL d 1 f c G v a p NIL d 1 f c G v a p NIL d 1 f c G v a p NIL d 1 f c G

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

BFS and DFS

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

d e f c NIL NIL 4 7 B W W d e f c NIL NIL 4 7 B W W d e f c NIL NIL 4 13 7 B G W d e f c NIL e 4 13 14 7 B G 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 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

v a b p NIL a d 1 2 f 12 11 c B B v a b p NIL a d 1 2 f 12 11 c B B v a b p NIL a d 1 2 f 12 11 c B B v a b p NIL a d 1 2 f 12 11 c B B

1/12 8/9 2/11

5/6

13/

e
4/7

b a

f h

d g time=14

1/12 8/9 2/11

14/ 13/
3/10

5/6

e
4/7

b a

f h

d g time=15

1/12 8/9 2/11

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

BFS and DFS

17

DFS Example Finish


a
1/12 8/9 2/11

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

The depth-rst forest for this example is

a h

f e

BFS and DFS

18

DFS Summary
The edges depth-rst forest.
  

are the edges in the

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.

The running time of DFS is


 X

BFS and DFS

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

Here is one possible topological sort:

Notice that no edges go from right to left.

be a directed acyclic graph.

BFS and DFS

20

Topological Sort Algorithm


Let

To topologically sort , we use a slightly modied version of DFS.


F

At the end of DFS-Visit( ), place a linked list.


F

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

be a directed acyclic graph.

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

You might also like