Breadth First Search

When there's relationship b/w nodes on the same level logically.

Basic method:

  1. Data structure: Maintain a FIFO queue, put in all traversed nodes that have not been expanded, and then the other nodes on the same level.

  2. Initial state: start node.

  3. Expand the node, visit/print its value.

  4. Generate its neighbor nodes, reach out.

  5. Termination condition: queue.isEmpty()

  6. Optionally deduplicate visited nodes for graphs. Each node could be generated multiple times, but should only be expanded once.

  7. Explicit graph vs. Implicit graph

Last updated

Was this helpful?