Practice Discrete Math

Algorithms / Queue Fifo Bfs Simulation

Least You Need to Know: Queues, FIFO Order, and Level-by-Level Simulation

Queues model situations where the earliest discovered or earliest arrived item should be processed first. That makes queues the natural frontier structure for breadth-first search and many step-by-step simulations.

The least you need to know

Key notation

enqueue add an item at the back of the queue
dequeue remove the oldest item from the front
frontier currently discovered but not yet processed states

Tiny worked example

  • In BFS from a source node, enqueue the source first.
  • Repeatedly dequeue one node, then enqueue its unseen neighbors.
  • Because earlier layers were enqueued first, they are processed before deeper layers.
  • That yields shortest-path distances in unweighted graphs.

Common mistakes

How to recognize this kind of problem

Next recommended lesson

Continue through this topic with Least You Need to Know: Recursion Trees and Recurrence Intuition.

Least You Need to Know: Recursion Trees and Recurrence Intuition

Related lessons

Keep going with nearby lessons in the same topic.

More ways to explore

Start practice