Graphs Breadth First Search & Depth First Search: by Shailendra Upadhye
Graphs Breadth First Search & Depth First Search: by Shailendra Upadhye
It is so named because
Animation:
http://en.wikipedia.org/wiki/Image:Animated_BFS.gif
DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 time ← time +1
3 d[u] time // record the discovery time
4 for each v ∈ Adj[u] ▹Explore edge(u, v).
5 do if color[v] = WHITE
6 then π[v] ← u // set the parent value
7 DFS-VISIT(v) // recursive call
8 color[u] BLACK ▹ Blacken u; it is finished.
9 f [u] ▹ time ← time +1
Applications of DFS
Topologically sorting a directed acyclic graph.
List the graph elements in such an order that all the nodes are listed
before nodes to which they have outgoing edges.
Finding the strongly connected components of a directed graph.
List all the subgraphs of a strongly connected graph which
themselves are strongly connected.
References
Data structures with C++ using STL by Ford,
William; Topp, William; Prentice Hall.
Introduction to Algorithms by Cormen,
Thomas et. al., The MIT press.
http://en.wikipedia.org/wiki/Graph_theory
http://en.wikipedia.org/wiki/Depth_first_search
Working example for BFS
Everett
North Bend
Seattle