What is Tree/Graph Traversal?
Traversal means visiting all nodes in a tree or graph structure, usually to perform operations like searching, pathfinding, or calculating values.
Traversal helps explore the structure systematically — whether we go deep (DFS) or wide (BFS) depends on the goal and constraints.
1. Breadth-First Search (BFS)
What is BFS? Breadth-First Search (BFS) is a method used to traverse or search through a graph or tree. The main idea behind BFS is to start from a given node (often called the source or root) and then explore all its immediate neighbors (nodes directly connected to it). After visiting all neighbors at the current level, it moves to the next level of neighbors — hence the name breadth-first.
Why BFS is Important: BFS is useful when you need to find the shortest path in an unweighted graph, or when you want to visit all nodes level by level, like in a tree. It is also the basis for many real-world applications like GPS navigation systems, friend suggestion features on social media, and even solving puzzles or mazes.
How Does BFS Work?
BFS uses a queue data structure to keep track of the nodes to visit next. A queue follows the First-In-First-Out (FIFO) principle — just like a line at a ticket counter. This ensures that nodes are visited in the correct order: first those closest to the start node, then those further away.
Pseudocode
This pseudocode outlines the basic steps of BFS in a very readable way:
function BFS(graph, start):
create empty queue // This queue will keep track of nodes to visit
mark start as visited // We remember that we’ve already visited this node
enqueue start // Add the starting node to the queue
while queue not empty: // While there are still nodes to process
node = dequeue // Remove the front node from the queue
process(node) // Do something with this node (like print it)
for each neighbor of node: // Check each connected node (neighbor)
if not visited: // If we haven’t visited it yet
mark visited // Mark it as visited to avoid revisiting
enqueue neighbor // Add it to the queue to visit later
Step-by-step Example: Imagine a graph where Node A is connected to B and C, and B is connected to D.
- Start at A → visit A, enqueue B and C
- Visit B → enqueue D
- Visit C → no more new neighbors
- Visit D → end
The order of traversal: A → B → C → D
Example Use Cases:
- Shortest path in unweighted graphs: BFS ensures the shortest number of edges to reach a node.
- Level-order traversal in trees: Visit all nodes level by level (first level, then second, and so on).
- Web crawlers, friend recommendations: BFS helps find nearby pages/friends in a network.
Time and Space Complexity:
- Time Complexity: O(V + E)
You visit each Vertice (node) and each Edge once. So it's very efficient.
- Space Complexity: O(V)
You store visited nodes and the queue — both grow with the number of vertices.
- BFS explores a graph/tree in layers (breadth-wise).
- It uses a queue to manage the order of node visits.
- Perfect when you want the shortest path or need to visit nodes level-by-level.
- Easy to implement and very common in real-world applications.