BFS/DFS Example

White = walkable fields, Black = Wall

You can switch fields by clicking on them and thus trying out many different mazes.














How this problem is solved

For our maze, we can use BFS or DFS to find a path if such a path exists. If a path exists, it is guaranteed that BFS/DFS will find a solution. With BFS, it is even guaranteed that we will get the shortest path. We interpret the whole maze as a graph where each (walkable) cell is a node in the graph. We then want to traverse this graph from a start Node to an end Node. We also keep track of the nodes that we have already visited. This search process then yields a tree
the maze itself is a graph (potentially with cycles), and it’s the search process (with visited-marking and parent pointers) that “yields a tree.”

This is how BFS works

Animation of the Breadth-First Search algorithm
Breadth-First Search Animation
© 2009 Mre
Licensed under CC BY-SA 3.0 Unported
No modifications were made.

This is how DFS works

Animation of Depth-First Search algorithm
Depth-First Search Animation
© 2009 Mre
Licensed under CC BY-SA 3.0 Unported
No modifications were made.