Usually when I've had to walk a graph, I've always used depth-first search because of the lower space complexity. I've honestly never seen a situation that calls for a breadth-first search, although my experience is pretty limited.
When does it make sense to use a breadth-first search?
UPDATE: I suppose my answer here shows a situation where I've used a BFS (because I thought was a DFS). I'm still curious to know though, why it was useful in this case.
Breadth-first search, also known as BFS, finds shortest paths from a given source vertex to all other vertices, in terms of the number of edges in the paths.
GPS Navigation systems: Breadth First Search is used to find all neighboring locations. Broadcasting in Network: In networks, a broadcasted packet follows Breadth First Search to reach all nodes. In Garbage Collection: Breadth First Search is used in copying garbage collection using Cheney's algorithm.
When you want to reach a node by traversing as few edges as possible, i.e. when you want to find the shortest path in an unweighted graph.
Also the space complexity of a depth first search can be higher than that of a breadth first search when e.g. each node has only one child node, i.e. when the graph is deep but not very broad.
If your search domain is infinite, depth-first-search doesn't guarantee to terminate/find a solution, even if a finite solution does exist.
Also you can see more elaborate algorithms like A* to be a special subtype of breadth-first-search.
In general, bfs is both optimal and complete (with finite branching factor) while dfs isn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With