0% found this document useful (0 votes)
49 views23 pages

DFS BFS

DFS BFS

Uploaded by

hima bindu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views23 pages

DFS BFS

DFS BFS

Uploaded by

hima bindu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

DFS and 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.

• Thus, for the adjacency matrix representation, the traversal time is in


Θ(|V |2), and

• For the adjacency list representation, it is in Θ(|V | + |E|) where |V | and


|E| are the number of the graph’s vertices and edges, respectively.
DFS forest
• A DFS traversal of a graph results in a structure called as a DFS forest.
It is actually the original graph with edges classified into two
categories:
 Tree Edges: Edges used to reach unvisited vertices during traversal.
 Back Edges: Edges connecting a vertex to an ancestor other than its
parent.
Orderings of DFS:

• 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.

3. Finding articulation points of a graph.


Breadth-First Search
• In breadth-first search, traversal proceeds in a concentric manner by
visiting first all the vertices that are adjacent to a starting vertex, then
all unvisited vertices two edges apart from it, and so on, until all the
vertices in the same connected component as the starting vertex are
visited.
• If there still remain unvisited vertices, the algorithm has to be restarted
at an arbitrary vertex of another connected component of the graph.
• It is convenient to use a queue to trace the operation of breadth-first
search.
• The queue is initialized with the traversal’s starting vertex, which is
marked as visited.
• On each iteration, the algorithm identifies all unvisited vertices that
are adjacent to the front vertex, marks them as visited, and adds them
to the queue; after that, the front vertex is removed from the queue.
• It is useful to accompany a BFS traversal by constructing the so-called
breadth-first search forest.
• The traversal’s starting vertex serves as the root of the first tree in such
a forest.
• Whenever a new unvisited vertex is reached for the first time, the
vertex is attached as a child to the vertex it is being reached from with
an edge called a tree edge.
• If an edge leading to a previously visited vertex other than its
immediate predecessor (i.e., its parent in the tree) is encountered, the
edge is noted as a cross edge.
ALGORITHM BFS(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
bfs(v)

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

• Breadth-first search has the same efficiency as depth-first search: it is


in Θ(|V |2) for the adjacency matrix representation and in Θ(|V | + |E|)
for the adjacency list representation.
BFS Ordering:

• Unlike depth-first search, it yields a single ordering of vertices


because the queue is a FIFO (first-in first-out) structure and hence the
order in which vertices are added to the queue is the same order in
which they are removed from it.
BFS forest
• BFS forest of an undirected graph can also have two kinds of edges:
tree edges and cross edges.
• Tree edges are the ones used to reach previously unvisited vertices.
• Cross edges connect vertices to those visited before, but, unlike back
edges in a DFS tree, they connect vertices either on the same or
adjacent levels of a BFS tree.
Applications of BFS
1. To check connectivity of a graph
 Perform BFS 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. To check acyclicity of a graph
 Examine the BFS forest for cross edges.
 If no cross edges are present, the graph is acyclic.
 The presence of a back edge indicates a cycle in the graph.
3. To find a path with the fewest number of edges between two given vertices.
 To do this, we start a BFS traversal at one of the two vertices and stop it as soon
as the other vertex is reached. The simple path from the root of the BFS tree to
the second vertex is the path sought. For example, path a − b − c – g in the graph
given below has the fewest number of edges among all the paths between
vertices a and g.

You might also like