Recursive Implementation of this idea.
dfs(v) {
D:
for each edge (v,w) do {
S:
if vertex w has not been discovered then
dfs(w)
}
F:
}
The main procedure (function) starts the search by calling dfs(s) from some starting vertex \(s\).
dfs
. Node \(v\) is the parent of node \(w\) if dfs(w) is called from dfs(v).In general, to ensure that the search explores the entire graph, the main procedure is written
for each vertex v do
if v has not been discovered then
dfs(v)
We can test if all vertices of a digraph are reachable from a source vertex \(s\) in time \(O(m+n)\) by DFS.