A graph's Breadth-First Traversal (or Search) is comparable to a tree's Breadth-First Traversal. The one catch is that, unlike trees, graphs can contain cycles, allowing us to return to the same node. To prevent processing a node several times, we separate the vertices into two groups:
Visited and
Not visited.
A boolean visited array marks the visited vertices. For the sake of simplicity, all vertices are believed to be reachable from the initial vertex. For traversal, BFS employs a queue data structure.
Depth First Search (DFS) is an edge-based technique. It employs the Stack data structure and operates in two stages: first, visited vertices are pushed into the stack, and then if no vertices are found, visited vertices are popped.