Depth-First Search (DFS)
Definition:
Depth-First Search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before backtracking. DFS starts from a source vertex and explores each branch or path before moving to a new one. It is commonly used for traversing or searching tree or graph data structures.
Video Explanation

Characteristics:
-
Recursive or Stack-Based Traversal:
- DFS can be implemented using recursion or a stack data structure. It explores a node, then recursively explores its neighbors before backtracking to explore other branches.
-
Preorder Traversal:
- DFS naturally performs a preorder traversal of the graph, visiting a node before any of its neighbors. This makes it useful for tasks like topological sorting, detecting cycles, or pathfinding in specific types of graphs.
-
Backtracking:
- DFS backtracks when it reaches a node with no unvisited neighbors. It then returns to previous nodes to explore unvisited paths.