BFS & DFS Algorithms

advertisement
Data Structures
• Depth First Search (DFS)
• Breadth First Search (BFS)
Data Structures
College of Science in Zulfi
Dr. Yousef Qawqzeh
Topics
1
Graph Search (traversal)
 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)
Data Structures
College of Science in Zulfi
• How do we search a graph?
• At a particular vertices, where shall we go next?
• Two common framework:
2
• 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
a
b
d
c
e
g
h
i
Data Structures
College of Science in Zulfi
Depth-First Search (DFS)
f
j
3
• Vertices initially colored white
• Then colored gray when discovered
• Then black when finished
Data Structures
College of Science in Zulfi
DFS: Color Scheme
4
DFS: Time Stamps
• Finish time f[u]: when backtrack from u
• d[u] < f[u]
Data Structures
College of Science in Zulfi
• Discover time d[u]: when u is first
discovered
5
Data Structures
College of Science in Zulfi
DFS Example
source
vertex
6
source
vertex
d
1 |
|
|
|
|
|
|
Data Structures
College of Science in Zulfi
DFS Example
f
|
7
source
vertex
d
1 |
|
|
2 |
|
|
|
Data Structures
College of Science in Zulfi
DFS Example
f
|
8
source
vertex
d
1 |
3 |
|
2 |
|
|
|
Data Structures
College of Science in Zulfi
DFS Example
f
|
9
DFS Example
d
f
1 |
|
2 |
|
|
3 | 4
|
|
Data Structures
College of Science in Zulfi
source
vertex
10
DFS Example
d
f
1 |
|
2 |
|
|
3 | 4
5 |
|
Data Structures
College of Science in Zulfi
source
vertex
11
DFS Example
d
f
1 |
|
2 |
|
|
3 | 4
5 | 6
|
Data Structures
College of Science in Zulfi
source
vertex
12
DFS Example
d
f
1 |
|
2 | 7
|
|
3 | 4
5 | 6
|
Data Structures
College of Science in Zulfi
source
vertex
13
DFS Example
d
f
1 |
8 |
2 | 7
|
|
3 | 4
5 | 6
|
Data Structures
College of Science in Zulfi
source
vertex
14
DFS Example
d
f
1 |
8 |
2 | 7
|
9 |
3 | 4
5 | 6
|
Data Structures
College of Science in Zulfi
source
vertex
15
DFS Example
d
f
1 |
8 |
2 | 7
|
9 |10
3 | 4
5 | 6
|
Data Structures
College of Science in Zulfi
source
vertex
16
DFS Example
d
f
1 |
8 |11
2 | 7
|
9 |10
3 | 4
5 | 6
|
Data Structures
College of Science in Zulfi
source
vertex
17
DFS Example
d
f
1 |12
8 |11
2 | 7
|
9 |10
3 | 4
5 | 6
|
Data Structures
College of Science in Zulfi
source
vertex
18
DFS Example
d
f
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
|
Data Structures
College of Science in Zulfi
source
vertex
19
DFS Example
d
f
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
14|
Data Structures
College of Science in Zulfi
source
vertex
20
DFS Example
d
f
1 |12
8 |11
2 | 7
13|
9 |10
3 | 4
5 | 6
14|15
Data Structures
College of Science in Zulfi
source
vertex
21
DFS Example
d
f
1 |12
8 |11
2 | 7
13|16
9 |10
3 | 4
5 | 6
14|15
Data Structures
College of Science in Zulfi
source
vertex
22
DFS: Algorithm
DFS(G)
for each vertex u in V,
color[u]=white; p[u]=NIL


time=0;

for each vertex u in V

Data Structures
College of Science in Zulfi

if (color[u]=white)

DFS-VISIT(u)
23
DFS: Algorithm (Cont.)
source
vertex

color[u]=gray;

time = time + 1;

d[u] = time;

for each v in Adj(u) do

if (color[v] = white)

p[v] = u;

DFS-VISIT(v);

color[u] = black;

time = time + 1; f[u]= time;
Data Structures
College of Science in Zulfi
DFS-VISIT(u)
24
DFS: Complexity Analysis
DFS_VISIT is called exactly once for each vertex
Data Structures
College of Science in Zulfi
Initialization complexity is O(V)
And DFS_VISIT scans all the edges which causes cost of O(E)
Overall complexity is O(V + E)
25
• Topological Sort
• Strongly Connected Component
Data Structures
College of Science in Zulfi
DFS: Application
26
• 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
Data Structures
College of Science in Zulfi
Breadth-first Search (BFS)
27
BFS: the Color Scheme
• White vertices have not been discovered
• All vertices start out white
• 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
Data Structures
College of Science in Zulfi
• Grey vertices are discovered but not fully
explored
28
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Data Structures
College of Science in Zulfi
An Example
29
0
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Data Structures
College of Science in Zulfi
A
30
1
A
B
1
E
F
I
J
K
L
M
N
O
P
1
C
D
G
H
Data Structures
College of Science in Zulfi
0
31
A
B
1
E
F
2
I
M
C
1
G
H
J
K
L
N
O
P
1
Data Structures
College of Science in Zulfi
0
D
2
32
1
A
E
B
F
2
I
J
3
M
N
C
1
1
3
G
D
3
3
K
L
O
P
Data Structures
College of Science in Zulfi
0
3
2
H
33
1
A
E
B
F
2
I
J
3
M
N
C
1
1
3
O
D
3
3
G
3
H
4
K
4
L
4
P
Data Structures
College of Science in Zulfi
0
2
34
1
2
3
A
E
I
M
B
F
N
C
1
1
J
3
3
O
G
3
H
4
K
4
L
4
P
5
5
D
3
Data Structures
College of Science in Zulfi
0
2
35
1
2
3
A
E
I
M
B
F
N
C
1
1
J
3
3
O
G
3
H
4
K
4
L
4
P
5
5
D
3
Data Structures
College of Science in Zulfi
0
2
36
1
2
3
A
E
I
M
B
F
N
C
1
1
J
3
3
O
G
3
H
4
K
4
L
4
P
5
5
D
3
Data Structures
College of Science in Zulfi
0
2
37
BFS: Algorithm
BFS(G, s)
 For each vertex u in V – {s},











color[u] = white;
d[u] = infinity;
p[u] = NIL
color[s] = GRAY;
d[s] = 0;
p[s] = NIL;
ENQUEUE(Q,s)
while (Q not empty)
u = DEQUEUE(Q)
for each v  Adj[u]
if color[v] = WHITE
then color[v] = GREY
d[v] = d[u] + 1; p[v] = u
ENQUEUE(Q, v);
color[u] = BLACK;
Q = empty queue
Data Structures
College of Science in Zulfi



38
r
s
t
u








v
w
x
y
Data Structures
College of Science in Zulfi
Example
39
r
s
t
u

0






v
w
x
y
Q: s
Data Structures
College of Science in Zulfi
Example
40
r
s
t
u
1
0



1


v
w
x
y
Q: w
r
Data Structures
College of Science in Zulfi
Example
41
r
s
t
u
1
0
2


1
2

v
w
x
y
Q: r
t
x
Data Structures
College of Science in Zulfi
Example
42
r
s
t
u
1
0
2

2
1
2

v
w
x
y
Q:
t
x
v
Data Structures
College of Science in Zulfi
Example
43
r
s
t
u
1
0
2
3
2
1
2

v
w
x
y
Q: x
v
u
Data Structures
College of Science in Zulfi
Example
44
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q: v
u
y
Data Structures
College of Science in Zulfi
Example
45
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q: u
y
Data Structures
College of Science in Zulfi
Example
46
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q: y
Data Structures
College of Science in Zulfi
Example
47
r
s
t
u
1
0
2
3
2
1
2
3
v
w
x
y
Q: Ø
Data Structures
College of Science in Zulfi
Example
48
• Queuing time is O(V) and scanning all
edges requires O(E)
• Overhead for initialization is O (V)
• So, total running time is O(V+E)
Data Structures
College of Science in Zulfi
BFS: Complexity Analysis
49
• Shortest path problem
Data Structures
College of Science in Zulfi
BFS: Application
50
Download