Computer Science Canada BFS algorithm in grids/mazes 
Author:  Panphobia [ Sun Nov 18, 2012 11:03 pm ]  
Post subject:  BFS algorithm in grids/mazes  
Ok so I know the concept of a BFS, but the thing is I still do not know exactly how to implement it. I am just asking how would I lets say with a BFS, in either java or python, doesnt matter really, get the shortest path from AB with this grid/maze and the # are walls

Author:  DemonWasp [ Mon Nov 19, 2012 1:15 am ]  
Post subject:  RE:BFS algorithm in grids/mazes  
A BreadthFirst Search works like this:
There's some complication to computing the path, but it works like this:  when you add neighbours(closest), assign "closest" as the "parent" of each neighbour  then the "parent" of each node points to the node it was discovered from  by following one parent to the next, you can get from B to A  reverse that path to get from A to B 
Author:  Panphobia [ Mon Nov 19, 2012 1:18 am ] 
Post subject:  RE:BFS algorithm in grids/mazes 
So nodetosearch is a set? 
Author:  mirhagk [ Mon Nov 19, 2012 9:11 am ] 
Post subject:  RE:BFS algorithm in grids/mazes 
Hey DemonWasp, I think you're confusing dijisktras with BFS. Breadth First Search is nearly identical to Depth First Search, the difference being which node you check next. With DFS you check the last node you discovered whereas with BFS you check the first one you discovered. Essentially you get a queue (which is something that you add items to the end, but get items from the front, so the items you get are the first items you get out. It's FIFO unlike stacks which are LIFO). Every time you discover a new node you add it to the queue, and each time in the while loop you grab the first thing from the queue, and check if it's the end, and if not you add new neighbours to the queue. 
Author:  mirhagk [ Mon Nov 19, 2012 9:31 am ]  
Post subject:  Re: BFS algorithm in grids/mazes  
As you can see from this updated example (from the one I sent you). BFS is very similar to DFS, with only the order you check nodes changed. The example map has been changed to show that DFS and BFS often find very different solutions, and that BFS allows finds a path that has the shortest number of nodes, whereas DFS does not. You could also create many optimizations from this, but you need to make sure you understand the algorithm before you do so. 
Author:  Panphobia [ Mon Nov 19, 2012 9:51 am ] 
Post subject:  RE:BFS algorithm in grids/mazes 
so this is a BFS right? not a DFS because your method tells me its DFS O: 
Author:  Panphobia [ Mon Nov 19, 2012 9:53 am ] 
Post subject:  RE:BFS algorithm in grids/mazes 
never mind lol, i will try to convert the bfs to java 
Author:  mirhagk [ Mon Nov 19, 2012 9:54 am ] 
Post subject:  RE:BFS algorithm in grids/mazes 
The code includes both a BFS and a DFS for comparison. The 2nd method is BFS, the first is DFS 
Author:  Panphobia [ Mon Nov 19, 2012 10:20 am ]  
Post subject:  Re: BFS algorithm in grids/mazes  
This is my current code edited from yours, how does it not work

Author:  mirhagk [ Mon Nov 19, 2012 10:28 am ]  
Post subject:  RE:BFS algorithm in grids/mazes  
Change A to * in your map. The starting point should be determined by the caller, not the map. If the starting point isn't * then it stops searching instantly. You've also really messed up the DrawMap part. It should look like

Author:  Panphobia [ Mon Nov 19, 2012 10:38 am ] 
Post subject:  RE:BFS algorithm in grids/mazes 
Yea but its an error at print(end=) and i had to take it out 
Author:  DemonWasp [ Mon Nov 19, 2012 10:50 am ] 
Post subject:  Re: RE:BFS algorithm in grids/mazes 
mirhagk @ Mon Nov 19, 2012 9:11 am wrote: ...confusing dijisktras with BFS...
Nope. Dijkstra's algorithm is completely different. For one, it uses edge weights, which BFS ignores. The pseudocode I wrote out is almost exactly identical to what's displayed on Wikipedia: http://en.wikipedia.org/wiki/Breadthfirst_search 
Author:  Panphobia [ Mon Nov 19, 2012 11:19 am ] 
Post subject:  RE:BFS algorithm in grids/mazes 
I got it to work, now the problem is, it takes like 5 seconds to execute, is there a quicker algorithm because dwite it needs to be less than 2.5 seconds 
Author:  mirhagk [ Mon Nov 19, 2012 11:54 am ] 
Post subject:  RE:BFS algorithm in grids/mazes 
Oh yeah there are lots better algorithms. Both DFS and BFS are brute force solutions. Depending on your problem Djikstras or A* could be very beneficial. Basically if you can come up with an estimate of how close something is to the answer you can use A*. In general most of the problems require semiunique solutions, because brute forcing is very easy in some languages (imperative languages especially), and they are meant to require at least some thought. @DemonWasp Sorry I was confused by your variable names, nodestosearch and closest. It seemed like you were doing some sort of sorting of the queue, which I now see you aren't. Your pseudocode isn't really specific on what node to grab next, which is the only difference between many of these graph searching algorithms (In fact djikstras is just that pseudo code where you grab the node that has the least sum of the edge nodes, and A* is just the same except you add a heuristic to the calcuation etc etc). The pseudo code is really more suited to a generic method for searching. 
Author:  Panphobia [ Mon Nov 19, 2012 12:11 pm ] 
Post subject:  RE:BFS algorithm in grids/mazes 
hmmmm i just searched dijkstra on wikipedia, and the pseudocode is making me very sad lol. Could you code up a quick example using dijkstra or A*, and also is Lee's Algorithm good for this situation? 
Author:  mirhagk [ Mon Nov 19, 2012 3:15 pm ] 
Post subject:  RE:BFS algorithm in grids/mazes 
If the edge sizes are the same then Dijkstra is the exact same as BFS. If A* has a heuristic of 0 (ie you don't have a way to estimate how close you are) then it's the same as Dijkstra. Because of the above 2 properties neither A* nor Dijkstra helps any with the example I've been using. We can't guess how far away we are from the end because we don't know where the end is. Therefore our A* is Dijkstra. Every movement we make is the same distance (it takes just as long to go left as it does to go right etc). That means that every edge size is the same, so any Dijkstra would just be BFS. 
Author:  Panphobia [ Mon Nov 19, 2012 3:53 pm ] 
Post subject:  RE:BFS algorithm in grids/mazes 
So if dijkstra just be BFS would it execute any faster? If not which algorithm would execute faster, and is lee's algorithm good? 
Author:  mirhagk [ Mon Nov 19, 2012 4:59 pm ] 
Post subject:  RE:BFS algorithm in grids/mazes 
since the algorithms operate equivalently, the simplest one would be the fastest, so BFS would be the fastest. Lee's algorithm looks interesting, but it just looks like a different (maybe more inefficient) way to do BFS. I don't really see a difference in the algorithm itself, but I don't know (I'm just going off of the wikipedia page) 
Author:  Panphobia [ Mon Nov 19, 2012 5:23 pm ] 
Post subject:  RE:BFS algorithm in grids/mazes 
thing is, the BFS runs in 45 seconds when it needs to be < 2.5s, soooo which algorithm would you recommend 
Author:  mirhagk [ Mon Nov 19, 2012 5:41 pm ] 
Post subject:  RE:BFS algorithm in grids/mazes 
what is the exact problem? 
Author:  Panphobia [ Mon Nov 19, 2012 6:32 pm ]  
Post subject:  Re: BFS algorithm in grids/mazes  
So here is my code for a bfs from A to B in a grid,I checked some other code and edited it to fit mine, is it alright, and is it efficient enough?

: 