Graph Traversal

Systematically visit all reachable nodes in a graph using BFS or DFS, tracking visited state to avoid duplicates.

Systematically visit all reachable nodes in a graph using BFS or DFS, tracking visited state to avoid duplicates.

The visited set contains exactly the nodes that have been enqueued or entered — no node is processed more than once.

A visited set reduces repeated traversal from O(V²) to O(V + E), regardless of whether BFS or DFS drives it.

BFS vs DFS affects traversal order and memory usage but not correctness for full reachability.