Depth-First Search
Definition:
Depth-First Search (DFS) is a graph traversal algorithm used to explore all vertices and edges of a graph. It starts from an initial node and explores as far as possible along each branch before backtracking. DFS is commonly used to solve problems like detecting cycles, solving mazes, and connectivity in graphs.
Characteristics:
-
Stack-Based Traversal: DFS relies on an implicit or explicit stack data structure. When implemented recursively, the system call stack is used to keep track of the visited nodes.
-
Backtracking: DFS explores all nodes along a path until it can’t go further, then backtracks to explore other unvisited nodes.
-
Complete Exploration: DFS continues to search until all the nodes of the graph are visited.
Video Explanation

Types of DFS:
-
Recursive DFS: A depth-first traversal using recursive function calls to visit each node.
-
Iterative DFS: Uses an explicit stack to simulate the recursive function, achieving the same result without recursion.