Depth First Search (DFS) - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Depth First Search (DFS)

Description:

DFS: Color Scheme. Vertices initially colored white. Then colored gray when discovered ... BFS: the Color Scheme. White vertices have not been discovered. All ... – PowerPoint PPT presentation

Number of Views:106
Avg rating:3.0/5.0
Slides: 51
Provided by: marily199
Category:
Tags: dfs | color | depth | first | scheme | search

less

Transcript and Presenter's Notes

Title: Depth First Search (DFS)


1
Lecture 8
Topics
  • Depth First Search (DFS)
  • Breadth First Search (BFS)

Reference Introduction to Algorithm by
Cormen Chapter 23 Elementary Graph Algorithm
2
Graph Search (traversal)
  • How do we search a graph?
  • At a particular vertices, where shall we go next?
  • Two common framework
  • the depth-first search (DFS)
  • the breadth-first search (BFS) and
  • In DFS, go as far as possible along a single path
    until reach a dead end (a vertex with no edge out
    or no neighbor unexplored) then backtrack
  • In BFS, one explore a graph level by level away
    (explore all neighbors first and then move on)

3
Depth-First Search (DFS)
  • The basic idea behind this algorithm is that it
    traverses the graph using recursion
  • Go as far as possible until you reach a deadend
  • Backtrack to the previous path and try the next
    branch
  • The graph below, started at node a, would be
    visited in the following order a, b, c, g, h,
    i, e, d, f, j

4
DFS Color Scheme
  • Vertices initially colored white
  • Then colored gray when discovered
  • Then black when finished

5
DFS Time Stamps
  • Discover time du when u is first discovered
  • Finish time fu when backtrack from u
  • du lt fu

6
DFS Example
sourcevertex
7
DFS Example
sourcevertex
d f
1







8
DFS Example
sourcevertex
d f
1


2




9
DFS Example
sourcevertex
d f
1


2



3
10
DFS Example
sourcevertex
d f
1


2



3 4
11
DFS Example
sourcevertex
d f
1


2


5
3 4
12
DFS Example
sourcevertex
d f
1


2


5 6
3 4
13
DFS Example
sourcevertex
d f
1


2 7


5 6
3 4
14
DFS Example
sourcevertex
d f
1
8

2 7


5 6
3 4
15
DFS Example
sourcevertex
d f
1
8

2 7
9

5 6
3 4
16
DFS Example
sourcevertex
d f
1
8

2 7
9 10

5 6
3 4
17
DFS Example
sourcevertex
d f
1
8 11

2 7
9 10

5 6
3 4
18
DFS Example
sourcevertex
d f
1 12
8 11

2 7
9 10

5 6
3 4
19
DFS Example
sourcevertex
d f
1 12
8 11
13
2 7
9 10

5 6
3 4
20
DFS Example
sourcevertex
d f
1 12
8 11
13
2 7
9 10
14
5 6
3 4
21
DFS Example
sourcevertex
d f
1 12
8 11
13
2 7
9 10
1415
5 6
3 4
22
DFS Example
sourcevertex
d f
1 12
8 11
1316
2 7
9 10
1415
5 6
3 4
23
DFS Algorithm
  • DFS(G)
  • for each vertex u in V,
  • coloruwhite puNIL
  • time0
  • for each vertex u in V
  • if (coloruwhite)
  • DFS-VISIT(u)

24
DFS Algorithm (Cont.)
  • DFS-VISIT(u)
  • colorugray
  • time time 1
  • du time
  • for each v in Adj(u) do
  • if (colorv white)
  • pv u
  • DFS-VISIT(v)
  • coloru black
  • time time 1 fu time

25
DFS Complexity Analysis
Initialization complexity is O(V)
DFS_VISIT is called exactly once for each vertex
And DFS_VISIT scans all the edges which causes
cost of O(E)
Overall complexity is O(V E)
26
DFS Application
  • Topological Sort
  • Strongly Connected Component

27
Breadth-first Search (BFS)
  • Search for all vertices that are directly
    reachable from the root (called level 1 vertices)
  • After mark all these vertices, visit all vertices
    that are directly reachable from any level 1
    vertices (called level 2 vertices), and so on.
  • In general, level k vertices are directly
    reachable from a level k 1 vertices

28
BFS the Color Scheme
  • White vertices have not been discovered
  • All vertices start out white
  • Grey vertices are discovered but not fully
    explored
  • They may be adjacent to white vertices
  • Black vertices are discovered and fully explored
  • They are adjacent only to black and gray vertices
  • Explore vertices by scanning adjacency list of
    grey vertices

29
An Example
A
D
C
B
E
F
G
H
L
K
J
I
O
N
M
P
30
A
D
C
B
0
E
F
G
H
L
K
J
I
O
N
M
P
31
A
D
C
B
0
1
E
F
G
H
1
1
L
K
J
I
O
N
M
P
32
A
D
C
B
0
2
1
E
F
G
H
1
1
L
K
J
I
2
O
N
M
P
33
A
D
C
B
0
3
2
1
E
F
G
H
1
1
3
3
L
K
J
I
2
O
N
M
P
3
3
34
A
D
C
B
0
3
2
1
E
F
G
H
1
1
3
4
3
L
K
J
I
2
4
4
O
N
M
P
3
3
35
A
D
C
B
0
3
2
1
E
F
G
H
1
1
3
4
3
L
K
J
I
2
4
4
5
O
N
M
P
3
3
5
36
A
D
C
B
0
3
2
1
E
F
G
H
1
1
3
4
3
L
K
J
I
2
4
4
5
O
N
M
P
3
3
5
37
A
D
C
B
0
3
2
1
E
F
G
H
1
1
3
4
3
L
K
J
I
2
4
4
5
O
N
M
P
3
3
5
38
BFS Algorithm
  • BFS(G, s)
  • For each vertex u in V s,
  • coloru white
  • du infinity
  • pu NIL
  • colors GRAY ds 0 ps
    NIL Q empty queue
  • ENQUEUE(Q,s)
  • while (Q not empty)
  • u DEQUEUE(Q)
  • for each v ? Adju
  • if colorv WHITE
  • then colorv GREY
  • dv du 1 pv u
  • ENQUEUE(Q, v)
  • coloru BLACK

39
Example
r
s
t
u
?
?
?
?
?
?
?
?
v
w
x
y
40
Example
r
s
t
u
?
0
?
?
?
?
?
?
v
w
x
y
s
Q
41
Example
r
s
t
u
1
0
?
?
?
1
?
?
v
w
x
y
w
Q
r
42
Example
r
s
t
u
1
0
2
?
?
1
2
?
v
w
x
y
r
Q
t
x
43
Example
r
s
t
u
1
0
2
?
2
1
2
?
v
w
x
y
Q
t
x
v
44
Example
r
s
t
u
1
0
2
3
2
1
2
?
v
w
x
y
Q
x
v
u
45
Example
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q
v
u
y
46
Example
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q
u
y
47
Example
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q
y
48
Example
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q
Ø
49
BFS Complexity Analysis
  • Queuing time is O(V) and scanning all edges
    requires O(E)
  • Overhead for initialization is O (V)
  • So, total running time is O(VE)

50
BFS Application
  • Shortest path problem
Write a Comment
User Comments (0)
About PowerShow.com