0% found this document useful (0 votes)
2 views10 pages

Unit-3 DFS

Depth-first search (DFS) is an algorithm for traversing tree or graph structures that explores as far as possible along each branch before backtracking, using a stack for implementation. It has advantages such as low memory usage and faster goal node reach compared to breadth-first search, but it can lead to infinite loops and may revisit states. The time complexity is O(n^d) and space complexity is O(V), with DFS being complete for finite search trees but not optimal.

Uploaded by

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

Unit-3 DFS

Depth-first search (DFS) is an algorithm for traversing tree or graph structures that explores as far as possible along each branch before backtracking, using a stack for implementation. It has advantages such as low memory usage and faster goal node reach compared to breadth-first search, but it can lead to infinite loops and may revisit states. The time complexity is O(n^d) and space complexity is O(V), with DFS being complete for finite search trees but not optimal.

Uploaded by

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

Depth-first search

• 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.
• It uses last in- first-out strategy and hence it is implemented
using a stack.
• As DFS traverses the tree “deepest node first”, it would
always pick the deeper branch until it reaches the solution (or
it runs out of nodes, and goes to the next branch).
Algorithm
• Create a recursive function that takes the index
of the node and a visited array.
• Mark the current node as visited and print the
node.
• Traverse all the adjacent and unmarked nodes
and call the recursive function with the index of
the adjacent node.

2
Example of DFS

3
Advantages
•It requires very less memory as it only needs to store a stack of
the nodes on the path from root node to the current node.
•It takes less time to reach to the goal node than BFS algorithm.
Disadvantage
•There is the possibility that many states keep re-occurring and
there is no guarantee of finding the solution.
•DFS Algorithm goes for deep down searching and sometimes it
may go to the infinite loop.

4
Time complexity: Equivalent to the
number of nodes traversed in DFS. T(n) = 1
+ n^2 + n^3 + ... + n^d = O(n^d)
d = the depth of the search tree = the
number of levels of the search tree.
n^i = number of nodes in level i
Space complexity:DFS has a space
complexity of O(V) due to the recursion
stack
5
• Completeness: DFS is complete if the search
tree is finite, meaning for a given finite search
tree, DFS will come up with a solution if it
exists.
Optimality: DFS is not optimal, meaning the
number of steps in reaching the solution, or
the cost spent in reaching it is high.

6
7
8
9
10

You might also like