Breadth First Search
When there's relationship b/w nodes on the same level logically.
Basic method:
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.
Initial state: start node.
Expand the node, visit/print its value.
Generate its neighbor nodes, reach out.
Termination condition: queue.isEmpty()
Optionally deduplicate visited nodes for graphs. Each node could be generated multiple times, but should only be expanded once.
Explicit graph vs. Implicit graph
Last updated
Was this helpful?