Upload
rosaline-bradford
View
233
Download
3
Tags:
Embed Size (px)
Citation preview
Elementary Graph Algorithms
Comp 122, Fall 2004
BFS(G,s)1. for each vertex u in V[G] – {s}2 do color[u] white3 d[u] 4 [u] null5 color[s] gray6 d[s] 07 [s] null8 Q 9 enqueue(Q,s)10 while Q 11 do u dequeue(Q)12 for each v in Adj[u]13 do if color[v] = white14 then color[v]
gray15 d[v] d[u] + 116 [v] u17 enqueue(Q,v)18 color[u] black
BFS(G,s)1. for each vertex u in V[G] – {s}2 do color[u] white3 d[u] 4 [u] null5 color[s] gray6 d[s] 07 [s] null8 Q 9 enqueue(Q,s)10 while Q 11 do u dequeue(Q)12 for each v in Adj[u]13 do if color[v] = white14 then color[v]
gray15 d[v] d[u] + 116 [v] u17 enqueue(Q,v)18 color[u] black
Comp 122, Fall 2004
white: undiscoveredgray: discoveredblack: finished
Q: a queue of discovered verticescolor[v]: color of vd[v]: distance from s to v[u]: predecessor of v
Example: animation.
Example (BFS)
Comp 122, Fall 2004
0
r s t u
v w x y
Q: s 0
Example (BFS)
Comp 122, Fall 2004
1 0
1
r s t u
v w x y
Q: w r 1 1
Example (BFS)
Comp 122, Fall 2004
2
2 t u
x y
Q: r t x 1 2 2
1 0
1
r s
v w
Example (BFS)
Comp 122, Fall 2004
2
u
v y
Q: t x v 2 2 2
2
2
t
1 0
1
r s
xw
Example (BFS)
Comp 122, Fall 2004
3
u
y
Q: x v u 2 2 3
2
2
t
1 0
1
r s
xw
2
v
Example (BFS)
Comp 122, Fall 2004
3
y
Q: v u y 2 3 3
2
2
t
1 0
1
r s
2
3
u
xwv
Example (BFS)
Comp 122, Fall 2004
Q: u y 3 3
3
y
2
2
t
1 0
1
r s
2
3
u
xwv
Example (BFS)
Comp 122, Fall 2004
Q: y 3
3
y
2
2
t
1 0
1
r s
2
3
u
xwv
Example (BFS)
Comp 122, Fall 2004
Q:
3
y
2
2
t
1 0
1
r s
2
3
u
xwv
Example (BFS)
Comp 122, Fall 2004
1 0
1 2 3
2 3
2
r s t u
v w x y
BFS Tree
DFSDFS(G)1. for each vertex u V[G]2. do color[u] white3. [u] NULL4. time 05. for each vertex u V[G]6. do if color[u] = white7. then DFS-Visit(u)
DFS(G)1. for each vertex u V[G]2. do color[u] white3. [u] NULL4. time 05. for each vertex u V[G]6. do if color[u] = white7. then DFS-Visit(u)
Comp 122, Fall 2004
Uses a global timestamp time.
DFS-Visit(u)
1. color[u] GRAY
2. time time + 1
3. d[u] time
4. for each v Adj[u]
5. do if color[v] = WHITE
6. then [v] u
7. DFS-Visit(v)
8. color[u] BLACK
9. time time + 1
10. f[u] time
DFS-Visit(u)
1. color[u] GRAY
2. time time + 1
3. d[u] time
4. for each v Adj[u]
5. do if color[v] = WHITE
6. then [v] u
7. DFS-Visit(v)
8. color[u] BLACK
9. time time + 1
10. f[u] time
Example (DFS)
Comp 122, Fall 2004
1/
u v w
x y z
Example (DFS)
Comp 122, Fall 2004
1/ 2/
u v w
x y z
Example (DFS)
Comp 122, Fall 2004
1/
3/
2/
u v w
x y z
Example (DFS)
Comp 122, Fall 2004
1/
4/ 3/
2/
u v w
x y z
Example (DFS)
Comp 122, Fall 2004
1/
4/ 3/
2/
u v w
x y z
B
Example (DFS)
Comp 122, Fall 2004
1/
4/5 3/
2/
u v w
x y z
B
Example (DFS)
Comp 122, Fall 2004
1/
4/5 3/6
2/
u v w
x y z
B
Example (DFS)
Comp 122, Fall 2004
1/
4/5 3/6
2/7
u v w
x y z
B
Example (DFS)
Comp 122, Fall 2004
1/
4/5 3/6
2/7
u v w
x y z
BF
Example (DFS)
Comp 122, Fall 2004
1/8
4/5 3/6
2/7
u v w
x y z
BF
Example (DFS)
Comp 122, Fall 2004
1/8
4/5 3/6
2/7 9/
u v w
x y z
BF
Example (DFS)
Comp 122, Fall 2004
1/8
4/5 3/6
2/7 9/
u v w
x y z
BF C
Example (DFS)
Comp 122, Fall 2004
1/8
4/5 3/6 10/
2/7 9/
u v w
x y z
BF C
Example (DFS)
Comp 122, Fall 2004
1/8
4/5 3/6 10/
2/7 9/
u v w
x y z
BF C
B
Example (DFS)
Comp 122, Fall 2004
1/8
4/5 3/6 10/11
2/7 9/
u v w
x y z
BF C
B
Example (DFS)
Comp 122, Fall 2004
1/8
4/5 3/6 10/11
2/7 9/12
u v w
x y z
BF C
B