Introduction
Depth First Search (DFS) is one of the most important and fundamental graph traversal algorithms used in Computer Science, Data Structures, and Artificial Intelligence.
Traversal means visiting all the nodes of a graph in a systematic manner.
DFS explores a graph by going as deep as possible along a path before coming back and trying other paths. Because of this nature, DFS is closely related to backtracking.
Definition
Depth First Search (DFS) is a graph traversal algorithm in which exploration starts from a given source node and proceeds as deep as possible along one branch before backtracking.
It follows the principle “Go deep first, then come back”.
DFS can be applied to graphs as well as trees and is widely used in Artificial Intelligence search problems, path finding, cycle detection, and backtracking-based problems.
In simple words:
DFS goes deep first, not wide.
Basic Idea of DFS
- Start from a source node
- Visit the node and mark it as visited
- Move to one of its unvisited adjacent nodes
- Continue moving deeper until:
- no unvisited adjacent node is left
- Then backtrack to the previous node and explore other paths
This behavior is similar to exploring a maze by choosing one path and continuing until you hit a dead end.
Data Structure Used
DFS uses:
- Stack
- Either an explicit stack
- Or implicit stack using recursion
This is why DFS follows LIFO (Last In First Out) order.
Step-by-Step Working of DFS
- Choose a starting (source) node
- Mark the source node as visited
- Visit one of its unvisited adjacent nodes
- Repeat the process for that node
- Continue until:
- No unvisited adjacent node is left
- Backtrack to the previous node
- Explore remaining unvisited neighbors
- Stop when all nodes are visited
Example of DFS Traversal
Consider the following graph:

NOTE : Upper image is different from the example.
FOR EXAMPLE :
Graph Structure
- Nodes: A, B, C, D, E, F
- Edges connect nodes in multiple paths
DFS Traversal Starting from Node A
- Start at A
- Move to B
- Move to D
- No further unvisited nodes → backtrack to B
- Move to E
- Backtrack to A
- Move to C
- Move to F
A → B → D → E → C → F
DFS Algorithm (Conceptual Explanation)
- DFS begins with the root node
- It marks nodes as visited to avoid repetition
- The algorithm keeps moving deeper until it reaches a dead end
- When a dead end is reached, DFS backtracks
- This process continues until all nodes are explored
Why DFS Uses Backtracking
Backtracking allows DFS to:
- Return to previous nodes
- Explore alternate paths
- Ensure complete traversal
This makes DFS very useful in:
- Puzzle solving
- Decision trees
- Constraint satisfaction problems
Applications of DFS
DFS is widely used in:
- Maze solving problems
- Path existence checking
- Cycle detection in graphs
- Topological sorting
- Finding connected components
- Artificial Intelligence search problems
- Solving puzzles like Sudoku, N-Queens
Advantages of DFS
- Requires less memory compared to BFS
- Easy to implement using recursion
- Suitable for problems where:
- Solutions are deep
- Efficient for sparse graphs
Disadvantages of DFS
- Does not guarantee shortest path
- May get stuck in infinite depth
- Not suitable for:
- Very deep or infinite graphs without limits
- May explore unnecessary long paths
Time and Space Complexity
- Time Complexity:
O(V+E)
where:- V = number of vertices
- E = number of edges
- Space Complexity:
O(V) due to recursion stack
Conclusion
Depth First Search is a fundamental graph traversal algorithm that explores nodes by going deep into a path before backtracking. It is memory efficient and easy to implement, making it suitable for backtracking and deep search problems. However, it does not guarantee optimal solutions and may fail in infinite or very deep search spaces. Despite its limitations, DFS forms the foundation for many advanced algorithms in computer science and artificial intelligence.


Pingback: Breadth First Search (BFS) – STUDIONYX