Beruflich Dokumente
Kultur Dokumente
Topics
Depth First Search (DFS)
Breadth First Search (BFS)
1.1
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)
1.2
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
c e
d f
i
1.3
Vertices initially colored white Then colored gray when discovered Then black when finished
1.4
discovered
Finish time f[u]: when backtrack from
u
d[u] < f[u]
1.5
DFS Example
source vertex
1.6
DFS Example
source vertex
d
1 |
f
| |
1.7
DFS Example
source vertex
d
1 |
f
| |
2 |
1.8
DFS Example
source vertex
d
1 |
f
| |
2 |
3 |
1.9
DFS Example
source vertex
d
1 |
f
| |
2 |
3 | 4
1.10
DFS Example
source vertex
d
1 |
f
| |
2 |
3 | 4
5 |
1.11
DFS Example
source vertex
d
1 |
f
| |
2 |
3 | 4
5 | 6
1.12
DFS Example
source vertex
d
1 |
f
| |
2 | 7
3 | 4
5 | 6
1.13
DFS Example
source vertex
d
1 |
f
8 | |
2 | 7
3 | 4
5 | 6
1.14
DFS Example
source vertex
d
1 |
f
8 | |
2 | 7
9 |
3 | 4
5 | 6
1.15
DFS Example
source vertex
d
1 |
f
8 | |
2 | 7
9 |10
3 | 4
5 | 6
1.16
DFS Example
source vertex
d
1 |
f
8 |11 |
2 | 7
9 |10
3 | 4
5 | 6
1.17
DFS Example
source vertex
f
8 |11 |
1 |12
2 | 7
9 |10
3 | 4
5 | 6
1.18
DFS Example
source vertex
f
8 |11 13|
1 |12
2 | 7
9 |10
3 | 4
5 | 6
1.19
DFS Example
source vertex
f
8 |11 13|
1 |12
2 | 7
9 |10
3 | 4
5 | 6
14|
1.20
DFS Example
source vertex
f
8 |11 13|
1 |12
2 | 7
9 |10
3 | 4
5 | 6
14|15
1.21
DFS Example
source vertex
f
8 |11 13|16
1 |12
2 | 7
9 |10
3 | 4
5 | 6
14|15
1.22
DFS: Algorithm
time=0;
for each vertex u in V if (color[u]=white) DFS-VISIT(u)
1.23
if (color[v] = white)
p[v] = u; DFS-VISIT(v);
color[u] = black;
time = time + 1; f[u]= time;
Data Structure and Algorithm
1.24
1.25
DFS: Application
Topological Sort
1.26
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
1.27
explored
They may be adjacent to white vertices
explored
They are adjacent only to black and gray vertices
An Example
A B C D
M
Data Structure and Algorithm
N
1.29
M
Data Structure and Algorithm
N
1.30
B 1
M
Data Structure and Algorithm
N
1.31
B 1
C 2
M
Data Structure and Algorithm
N
1.32
B 1
C 2
F 3
M
Data Structure and Algorithm
3
1.33
B 1
C 2
F 3
M
Data Structure and Algorithm
3
1.34
B 1
C 2
F 3
M
Data Structure and Algorithm
3
1.35
B 1
C 2
F 3
M
Data Structure and Algorithm
3
1.36
B 1
C 2
F 3
M
Data Structure and Algorithm
3
1.37
BFS: Algorithm
BFS(G, s) For each vertex u in V {s},
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
1.38
Example
1.39
Example
0
w
Q: s
Data Structure and Algorithm
1.40
Example
1
v
0 1
w
Q: w
Data Structure and Algorithm
r
1.41
Example
1
v
0 1
w
2 2
x
Q: r
Data Structure and Algorithm
x
1.42
Example
1 2
v
0 1
w
2 2
x
Q:
Data Structure and Algorithm
v
1.43
Example
1 2
v
0 1
w
2 2
x
3
y
Q: x
Data Structure and Algorithm
u
1.44
Example
1 2
v
0 1
w
2 2
x
3 3
y
Q: v
Data Structure and Algorithm
y
1.45
Example
1 2
v
0 1
w
2 2
x
3 3
y
Q: u
Data Structure and Algorithm
y
1.46
Example
1 2
v
0 1
w
2 2
x
3 3
y
Q: y
Data Structure and Algorithm
1.47
Example
1 2
v
0 1
w
2 2
x
3 3
y
Q:
Data Structure and Algorithm
1.48
edges requires O(E) Overhead for initialization is O (V) So, total running time is O(V+E)
1.49
BFS: Application
1.50