Download presentation
Presentation is loading. Please wait.
Published byRosaline Bradford Modified over 9 years ago
1
Elementary Graph Algorithms Comp 122, Fall 2004
4
BFS(G,s) 1.for each vertex u in V[G] – {s} 2do color[u] white 3 d[u] 4 [u] null 5color[s] gray 6d[s] 0 7 [s] null 8Q 9enqueue(Q,s) 10while Q 11do u dequeue(Q) 12for each v in Adj[u] 13do if color[v] = white 14then color[v] gray 15 d[v] d[u] + 1 16 [v] u 17 enqueue(Q,v) 18color[u] black BFS(G,s) 1.for each vertex u in V[G] – {s} 2do color[u] white 3 d[u] 4 [u] null 5color[s] gray 6d[s] 0 7 [s] null 8Q 9enqueue(Q,s) 10while Q 11do u dequeue(Q) 12for each v in Adj[u] 13do if color[v] = white 14then color[v] gray 15 d[v] d[u] + 1 16 [v] u 17 enqueue(Q,v) 18color[u] black Comp 122, Fall 2004 white: undiscovered gray: discovered black: finished Q: a queue of discovered vertices color[v]: color of v d[v]: distance from s to v [u]: predecessor of v Example: animation.
5
Example (BFS) Comp 122, Fall 2004 0 r s t u v w x y Q: s 0
6
Example (BFS) Comp 122, Fall 2004 1 0 1 r s t u v w x y Q: w r 1 1
7
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
8
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 x w
9
Example (BFS) Comp 122, Fall 2004 3 u y Q: x v u 2 2 3 2 2 t 1 0 1 r s x w 2 v
10
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 x w v
11
Example (BFS) Comp 122, Fall 2004 Q: u y 3 3 3 y 2 2 t 1 0 1 r s 2 3 u x w v
12
Example (BFS) Comp 122, Fall 2004 Q: y 3 3 y 2 2 t 1 0 1 r s 2 3 u x w v
13
Example (BFS) Comp 122, Fall 2004 Q: 3 y 2 2 t 1 0 1 r s 2 3 u x w v
14
Example (BFS) Comp 122, Fall 2004 1 0 1 2 3 2 3 2 r s t u v w x y BFS Tree
15
DFS DFS(G) 1. for each vertex u V[G] 2. do color[u] white 3. [u] NULL 4. time 0 5. for each vertex u V[G] 6. do if color[u] = white 7. then DFS-Visit(u) DFS(G) 1. for each vertex u V[G] 2. do color[u] white 3. [u] NULL 4. time 0 5. for each vertex u V[G] 6. do if color[u] = white 7. 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
16
Example (DFS) Comp 122, Fall 2004 1/ u v w x y z
17
Example (DFS) Comp 122, Fall 2004 1/ 2/ u v w x y z
18
Example (DFS) Comp 122, Fall 2004 1/ 3/ 2/ u v w x y z
19
Example (DFS) Comp 122, Fall 2004 1/ 4/ 3/ 2/ u v w x y z
20
Example (DFS) Comp 122, Fall 2004 1/ 4/ 3/ 2/ u v w x y z B
21
Example (DFS) Comp 122, Fall 2004 1/ 4/5 3/ 2/ u v w x y z B
22
Example (DFS) Comp 122, Fall 2004 1/ 4/5 3/6 2/ u v w x y z B
23
Example (DFS) Comp 122, Fall 2004 1/ 4/5 3/6 2/7 u v w x y z B
24
Example (DFS) Comp 122, Fall 2004 1/ 4/5 3/6 2/7 u v w x y z B F
25
Example (DFS) Comp 122, Fall 2004 1/8 4/5 3/6 2/7 u v w x y z B F
26
Example (DFS) Comp 122, Fall 2004 1/8 4/5 3/6 2/7 9/ u v w x y z B F
27
Example (DFS) Comp 122, Fall 2004 1/8 4/5 3/6 2/7 9/ u v w x y z B F C
28
Example (DFS) Comp 122, Fall 2004 1/8 4/5 3/6 10/ 2/7 9/ u v w x y z B F C
29
Example (DFS) Comp 122, Fall 2004 1/8 4/5 3/6 10/ 2/7 9/ u v w x y z B F C B
30
Example (DFS) Comp 122, Fall 2004 1/8 4/5 3/6 10/11 2/7 9/ u v w x y z B F C B
31
Example (DFS) Comp 122, Fall 2004 1/8 4/5 3/6 10/11 2/7 9/12 u v w x y z B F C B
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.