Presentation On Depth First Search Algorithm Morshed Arifin
Presentation On Depth First Search Algorithm Morshed Arifin
ON DEPTH FIRST
SEARCH
ALGORITHM
Name: Morshed Arafin
ID: 20191010010
Whattitle
Click to edit Master is “style
Depth First Search Algorithm
Depth-first search (DFS) is an algorithm for
traversing or searching tree or graph data
structures.
The algorithm starts at the root
node (selecting some arbitrary node as the
root node in the case of a graph) and
explores as far as possible along each
branch before backtracking.
2 2
Click to edit Master title style
The basic of Depth First Search
The recursive nature of DFS can be implemented using stacks.
The basic idea is as follows:
• Pick a starting node and push all its adjacent nodes into a stack.
• Pop a node from stack to select the next node to visit and push all its
adjacent nodes into a stack.
• Repeat this process until the stack is empty. However, ensure that the
nodes that are visited are marked. This will prevent you from visiting
the same node more than once. If you do not mark the nodes that are
visited and you visit the same node more than once, you may end up
in an infinite loop.
3 3
Click to editExample
Master titleofstyle
Depth First Search Algorithm
Let's see how the Depth First Search algorithm works with an example. We use an undirected graph
with 5 vertices.
4 4
Click to edit Master title style
We start from vertex 0, the DFS algorithm starts by putting it in the Visited list and
putting all its adjacent vertices in the stack.
5 5
Click to edit Master title style
Next, we visit the element at the top of stack i.e. 1 and go to its adjacent nodes. Since 0 has
already been visited, we visit 2 instead.
6 6
Click to edit Master title style
Vertex 2 has an unvisited adjacent vertex in 4, so we add that to the top of the stack
and visit it.
7 7
Click to edit Master title style
8 8
Click to edit Master title style
After we visit the last element 3, it doesn't have any unvisited adjacent nodes, so we have
completed the Depth First Traversal of the graph.
9 9
Click to edit Master title styleof Depth First Search Algorithm
Applications
• For finding the path
• To test if the graph is bipartite
• For finding the strongly connected components of
a graph
• For detecting cycles in a graph
1010
Click to edit Master title style
Thank You
11