Elementary Graph Algorithms Comp 122, Fall 2004
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] [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] [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.
Example (BFS) Comp 122, Fall 2004 0 r s t u v w x y Q: s 0
Example (BFS) Comp 122, Fall r s t u v w x y Q: w r 1 1
Example (BFS) Comp 122, Fall 2 t u x y Q: r t x r s v w
Example (BFS) Comp 122, Fall 2004 2 u v y Q: t x v t r s x w
Example (BFS) Comp 122, Fall 2004 3 u y Q: x v u t r s x w 2 v
Example (BFS) Comp 122, Fall y Q: v u y t r s 2 3 u x w v
Example (BFS) Comp 122, Fall 2004 Q: u y y 2 2 t r s 2 3 u x w v
Example (BFS) Comp 122, Fall 2004 Q: y 3 3 y 2 2 t r s 2 3 u x w v
Example (BFS) Comp 122, Fall 2004 Q: 3 y 2 2 t r s 2 3 u x w v
Example (BFS) Comp 122, Fall r s t u v w x y BFS Tree
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 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 f[u] time DFS-Visit(u) 1.color[u] GRAY 2.time time 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 f[u] time
Example (DFS) Comp 122, Fall / u v w x y z
Example (DFS) Comp 122, Fall / 2/ u v w x y z
Example (DFS) Comp 122, Fall / 3/ 2/ u v w x y z
Example (DFS) Comp 122, Fall / 4/ 3/ 2/ u v w x y z
Example (DFS) Comp 122, Fall / 4/ 3/ 2/ u v w x y z B
Example (DFS) Comp 122, Fall / 4/5 3/ 2/ u v w x y z B
Example (DFS) Comp 122, Fall / 4/5 3/6 2/ u v w x y z B
Example (DFS) Comp 122, Fall / 4/5 3/6 2/7 u v w x y z B
Example (DFS) Comp 122, Fall / 4/5 3/6 2/7 u v w x y z B F
Example (DFS) Comp 122, Fall /8 4/5 3/6 2/7 u v w x y z B F
Example (DFS) Comp 122, Fall /8 4/5 3/6 2/7 9/ u v w x y z B F
Example (DFS) Comp 122, Fall /8 4/5 3/6 2/7 9/ u v w x y z B F C
Example (DFS) Comp 122, Fall /8 4/5 3/6 10/ 2/7 9/ u v w x y z B F C
Example (DFS) Comp 122, Fall /8 4/5 3/6 10/ 2/7 9/ u v w x y z B F C B
Example (DFS) Comp 122, Fall /8 4/5 3/6 10/11 2/7 9/ u v w x y z B F C B
Example (DFS) Comp 122, Fall /8 4/5 3/6 10/11 2/7 9/12 u v w x y z B F C B