DFS BFS
DFS BFS
Depth-First Search
• Depth-first search starts a graph’s traversal at an arbitrary vertex by
marking it as visited.
• On each iteration, the algorithm proceeds to an unvisited vertex that is
adjacent to the one it is currently in. This process continues until a dead
end—a vertex with no adjacent unvisited vertices is encountered.
• At a dead end, the algorithm backs up one edge to the vertex it came
from and tries to continue visiting unvisited vertices from there. The
algorithm eventually halts after backing up to the starting vertex, with
the latter being a dead end.
• By then, all the vertices in the same connected component as the starting
vertex have been visited. If unvisited vertices still remain, the depth-first
search must be restarted at any one of them.
• It is convenient to use a stack to trace the operation of depth-first
search.
• We push a vertex onto the stack when the vertex is reached for the first
time and we pop a vertex off the stack when it becomes a dead end.
DFS Forest
• It is also very useful to accompany a depth-first search traversal by
constructing a depth-first search forest.
• The starting vertex of the traversal serves as the root of the first tree in such
a forest.
• Whenever a new unvisited vertex is reached for the first time, it is attached
as a child to the vertex from which it is being reached. Such an edge is
called a tree edge because the set of all such edges forms a forest. The
algorithm may also encounter an edge leading to a previously visited vertex
other than its immediate predecessor (i.e., its parent in the tree).
• Such an edge is called a back edge because it connects a vertex to its
ancestor, other than the parent, in the depth-first search forest.
ALGORITHM DFS(G)
mark each vertex in V with 0 as a mark of being “unvisited”
count ←0
for each vertex v in V do
if v is marked with 0
dfs(v)
dfs(v)
count ←count + 1; mark v with count
for each vertex w in V adjacent to v do
if w is marked with 0
dfs(w)
Efficiency of DFS
• DFS is quite efficient since it takes just the time proportional to the
size of the data structure used for representing the graph in question.
• DFS yields two orderings of vertices: the order in which the vertices
are reached for the first time, and the order in which the vertices
become dead ends.
Applications of DFS
1. Checking Connectivity:
Perform DFS from an arbitrary vertex.
If all vertices are visited, the graph is connected; otherwise, it is not.
To identify all connected components, initiate a DFS from each
unvisited vertex and mark all reachable vertices.
2. Checking Acyclicity:
Examine the DFS forest for back edges.
If no back edges are present, the graph is acyclic.
The presence of a back edge indicates a cycle in the graph.
bfs(v)
count ←count + 1; mark v with count and initialize a queue with v
while the queue is not empty do
for each vertex w in V adjacent to the front vertex do
if w is marked with 0
count ←count + 1; mark w with count
add w to the queue
remove the front vertex from the queue
BFS efficiency