Depth-First Search

Depth-First Search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before backtracking. It uses a stack (or recursion) and is useful for topological sorting, detecting cycles, and solving maze problems.

Depth-First Search Visualization

How to use the DFS Visualizer

  1. Create nodes: click on the graph canvas to add vertices.
  2. Connect nodes: click one node, then click another to create an edge.
  3. Start the algorithm: click Start Step or Run to begin the DFS traversal.
  4. Follow the process: watch the queue and logs to understand each step.

Complexity

Time Complexity: O(V + E)
Space Complexity: O(V)

Pseudocode

function DFS(graph, start, visited):
  visited.add(start)
  
  for each neighbor in graph[start]:
    if neighbor not in visited:
      DFS(graph, neighbor, visited)