CS With NehalCS With Nehal

BFS (Breadth First Search) Simulator

Build interactive graphs and watch BFS explore networks level-by-level using a visual queue.

Start:A▼
Nodes (V): 7
Edges (E): 6
BFS Queue
Empty
Start
A
B
C
D
E
F
G
Animation Speed — 1000ms

About This Simulation

This BFS simulator lets you draw your own graph, pick a start node and watch Breadth First Search visit it level by level. The queue is shown on screen, so you can see exactly which node is added and removed at every step.

Features

  • Add nodes and edges on an interactive canvas
  • Choose the start node and load ready-made example graphs
  • Live BFS queue with enqueue and dequeue highlighted
  • Step forward, step back, play and pause with adjustable speed
  • Visited, in-queue and finished nodes shown in different colours

Explanation

Breadth First Search (BFS) is a graph traversal algorithm that explores all neighbours of a node before moving one level deeper. It uses a queue (first in, first out), so nodes are visited in order of their distance, in edges, from the start node.

How it works

  1. Mark the start node as visited and put it in the queue.
  2. Remove the node at the front of the queue and process it.
  3. For each neighbour of that node that has not been visited, mark it visited, record its parent and add it to the back of the queue.
  4. Repeat until the queue is empty. Every reachable node is visited exactly once.
  5. Following the parent links back from any node gives a shortest path (fewest edges) from the start node.

Why it matters

BFS finds shortest paths in unweighted graphs and is the basis of level-order tree traversal, connected-component counting, bipartite checking, web crawlers and social-network 'degrees of separation'. It is a core topic in data structures and algorithms courses such as CSE220 and CSE221.

Frequently Asked Questions

What data structure does BFS use?

BFS uses a queue. The node that was discovered first is processed first, which is what makes the search expand level by level.

What is the time complexity of BFS?

O(V + E) with an adjacency list, where V is the number of vertices and E the number of edges, because every vertex is enqueued once and every edge is checked once (twice in an undirected graph). With an adjacency matrix it is O(V²).

Does BFS find the shortest path?

Yes, but only in unweighted graphs (or graphs where every edge has the same weight). For weighted graphs use Dijkstra's algorithm or Bellman-Ford.

What is the difference between BFS and DFS?

BFS uses a queue and explores level by level, so it finds the shortest path in edges. DFS uses a stack (or recursion) and goes as deep as possible before backtracking. Both run in O(V + E).

Topics covered

  • Breadth First Search
  • Graph Traversal
  • Queue (FIFO)
  • Shortest Path in Unweighted Graphs
  • BFS Tree
  • Time Complexity O(V + E)

Tags

  • bfs
  • queue
  • shortest path unweighted

Related Simulations

Acknowledgements

The Person Behind This Simulation

This simulation would not have been possible without his valuable contributions and support. We sincerely appreciate his time, effort, and dedication to the project.

Tahsin Uddin Labib

Tahsin Uddin Labib

  • 4th Year CS Undergraduate Student | BRAC University