Graph BFS
Strip a tree of its no-cycles guarantee, or a grid of its coordinate math, and what's left is a general graph. The engine never changes: pull from a queue, process the node, push its neighbors. Across trees, grids, and general graphs only two things move: how you find a node's neighbors, and whether you have to guard against revisiting. Trees have no cycles, so no visited set. A grid is a graph too, with each cell's neighbors being the adjacent cells. General graphs spell their edges out and can loop back on themselves, so drop the visited set and BFS runs until the queue eats all your memory.
- 1 problem, worked step by step
- about 25 min
Premium lessonData Structures & Algorithms
Keep going with Premium.
All 78 lessons and 103 worked problems in this course, plus what 100 companies ask, pulled from 20,000+ candidate reports.
100 companies. Checked every month.
Unlock the full course$49 a year · less than $1 a week · 30-day money-back guarantee