Breadth First Search

  • Initialize a queue with a starting vertex s and mark that vertex.
    • A queue is a list that has two operations: enqueue (a.k.a. addLast) and dequeue (a.k.a. removeFirst).
    • Let’s call this the queue our fringe.
  • Repeat until queue is empty:
    • Remove vertex v from the front of the queue.
    • For each unmarked neighbor n of v:
      • Mark n.
      • Set edgeTo[n] = v (and/or distTo[n] = distTo[v] + 1).
      • Add n to end of queue.