Breadth first search(BFS) is a very powerful and useful algorithm for exploring graph like structures. A BFS searches it’s nearest neighbors first before moving on. It’s main use is to find a path using the fewest hops. Here we use it to explore a grid that can be modified by a user. In this grid, we have verticies (the grid boxes), and edges (the box north, south, east and west).
A plain english example would be as
- Create a queue with your starting element.
- while the queue is not empty
- shift out the first element.
- does that element meet the goal?
- If so quit with success
- Otherwise for all neighbors of that element add them to the queue if they haven’t been seen yet. Make a note of where we came from.
- Otherwise fail.
How To Use
- Change your grid size of anything between 4 and 40 elements wide. Hit the reset grid button.
- Set your start point using the “place start point” button. There must be exactly one starting point.
- Place some finish points using the “place finish point” button. There can be many finish points. In a regular BFS these are your target points.
- Add some constraints by drawing some walls.
- Hit either “Run” or “Step” to start the BFS
- The active node will be surounded with a gold border.
- Discovered nodes are surrounded with a dark blue border.
- The nodes in the queue are surrounded with a light blue border.
- After the goal is found it will draw the path back to the starting node. It will be shown in light green
You can view and explore the source code on GitHub. Made with TypeScript because I can.