Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is breadth-first search useful for?

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.

like image 539
Jason Baker Avatar asked Nov 01 '09 13:11

Jason Baker


People also ask

What is Breadth First Search?

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.

What can be the applications of Breadth First Search used in data structure?

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.


2 Answers

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.

like image 134
sepp2k Avatar answered Oct 12 '22 10:10

sepp2k


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.

like image 45
ziggystar Avatar answered Oct 12 '22 11:10

ziggystar