visits a graph node and explores its neighbors before going on to any of its child nodes. (going wide first before going deep)
an iterative algorithm and makes use of a queue.
// code
// pseudo codebfs(Graph G, GraphNode root) {let q be new Queueroot.visited =true// mark root as visitedq.enqueue(root) // add root to the queuewhile (q is not empty) {let current be q.dequeue() // remove front element in the queuefor(neighbors n of current inG) {if (n is not yet visited) {// add to the queue - so you can explore its neighbors tooq.enqueue(n)n.visited =true } } }}