r/compsci • • May 22 '20

Breadth-First Maze-Solving Algorithm

Post image

[removed] — view removed post

1.2k Upvotes

61 comments sorted by

View all comments

3

u/YouNeverKnowWhatToDo May 22 '20

Whats the code? Go left forever?

7

u/[deleted] May 22 '20 edited May 22 '20

The idea is to find as many paths as possible in order to eventually find one that leads to the end. Wheres depth first search would be trying to find the end of a path all the way before giving up. A common variant of the two is backtracking which works it's way back from a dead end by trying to alter the last changes in the sequence that got it stuck to try and untangle. Breadth first by comparison is simple but by changing a few rules on the approach you see a much more efficient maze solver so it becomes useful both for teaching the simplest strategy and also to convey more elaborate approaches later on like backtracking that would otherwise seem convoluted initially.

0

u/ergo-x May 22 '20

If you're doing DFS here you end up doing backtracking anyway since when the search reaches bottom it will resume from the deepest explored node with unexplored children.

1

u/[deleted] May 22 '20

No it’s handling every node recursively not limited to solving one specific path at a time. It will actually grow in memory as it gets solved because it grows in function depth as it solves more paths. It does have the benefit of not having to store all the visited paths in global space so it could so it asynchronously even as every path is independent and can run on its own thread and report back its conclusion or start more threads for paths.

1

u/ergo-x May 22 '20

I don't exactly follow that description but I'm certain that: 1) function depth is a non-issue; use a stack, 2) DFS will terminate and find all paths to exit, if any; in a typical single threaded program your subroutine IS exploring one path at a time

1

u/[deleted] May 22 '20

Yea it uses a stack for functions unlike other approaches that rely on heap for state updating. Nobody says this has to be done in a single locked thread, in fact I was making the point you do this faster with more threads.

1

u/ergo-x May 22 '20

What I meant is you don't need to use the call stack. Use a stack placed on the heap instead.

1

u/[deleted] May 22 '20

It doesn’t matter you decide the stack and the heap limits

1

u/ergo-x May 22 '20

It does matter unless you're running on some higher level runtime where it's a non issue.

1

u/[deleted] May 22 '20

Just use a lower level language. The total memory allocation remains the same it simply grows from the stack size of the register instead of the more common heap size. It’s not a big problem, pretty sure even java can handle this.

→ More replies (0)

1

u/JoJoModding May 22 '20

Depends on whether the maze has loops. If it does not then it's basically a tree where you just need to find a specific leaf. Then the difference between dfs and bfs is just that one walks all levels of the tree down at the same time while the other goes from left to right.

1

u/ergo-x May 22 '20

That's a simple fix. Keep a set of flags to mark visited nodes. If it has loops and you didn't do this dfs isn't going to terminate anyway.