⚠️ Please use a computer for the best experience

Graph Traversal Algorithms

Interactive visualization of Breadth-First Search (BFS) and Depth-First Search (DFS)

Controls

Graph Visualization

Step: 0 / 0

Current
Visited
In Queue
Unvisited

Queue State

Queue is empty

Visited Nodes (0)

No nodes visited yet

Breadth-First Search (BFS)

Level-by-level exploration: BFS explores nodes in layers, visiting all neighbors at the current depth before moving to the next level.

Queue data structure: Uses a FIFO (First-In-First-Out) queue to maintain the order of exploration.

Time Complexity: O(V + E) where V is vertices and E is edges.

Space Complexity: O(V) for the queue and visited set.

  1. Start at the source node and mark it as visited
  2. Add the starting node to the queue
  3. While queue is not empty:
    • Dequeue a node from the front
    • Process/visit the node
    • Enqueue all unvisited neighbors to the back

Use cases: Shortest path in unweighted graphs, level-order traversal, finding connected components.