Algorithm


Depth-First Search (DFS) Algorithm:

  1. Initialize a stack and mark the starting node as visited.
  2. Push the starting node onto the stack.
  3. While the stack is not empty:
    1. Pop a node from the top of the stack.
    2. Process the popped node (e.g., print or store it).
    3. For each unvisited adjacent node of the popped node:
      1. Mark the adjacent node as visited.
      2. Push the adjacent node onto the stack.

Example:

Depth-First Search Example

Fig. 1: Step-by-Step Process of Depth-First Search

Note: DFS is particularly useful for exploring all possible paths in a graph and for solving problems like maze traversal and topological sorting.