Talk:Breadth-first search

From Wikipedia, the free encyclopedia

Breadth first recursion now redirects here.


[edit] Request for major editing:

I have been looking at this and also depth-first search and the preliminary explaination has the same meaning. It says that it starts at the root node "or a node", and explores each neighboring node of the "root" fully without making a cycle until it finds the goal. Something needs to be done about this. I'm just a student trying to learn this so I don't think I can really help. Maybe I'll ask my professor to fix this.



I think that since BFS uses a Queue, "push" and "pop" are wrong (stacks, LIFO): a queue uses "enqueue" and "dequeue". Edited. --Folletto 16:00, 21 Jun 2005 (UTC)

"Since all nodes discovered so far have to be saved, the space complexity of breadth-first search is O(|V| + |E|)." I don't see it. Where does the |E| come from?

[edit] Pseudocode

The pseudocode looks funny, and a little too close to an actual implementation. I have taken liberty to modify it a little.


[edit] Djisktra's Algorithm vs. BFS

When BFS is used with a priority queue, it is a total algorithm to find the shortest path between any two nodes in a weighted graph. Clearly, therefore, finding the shortest path between two nodes in a weighted graph is an application of BFS.

Somebody deleted this application because they felt that this should be an application of Djisktra's Algorithm instead. What is the logic here? Clearly it is an application of both DA and BFS. DA is an optimization of the backtracing step of BFS, so you don't have to use back-pointers to remember how you got to the destination. The distinction between BFS and DA therefore has little to do with whether or not the graph is weighted. Finally, BFS is much easier to remember than DA, so BFS is undoubtedly the more popular algorithm to find shortest paths in any graph (weighted or unweighted). Therefore this application should be listed.

Hi, I think I was the one who deleted that passage. Why I did it? BFS simply does not find a shortest path in a weighted graph! It finds the path that has the fewest number of edges, but if edges have edge weights, BFS will find an arbitrary expensive path of n edges instead of the cheap path that has n+1 edges. Therefore BFS is not an application for finding a shortest path in a weighted graph simply because it computes absolute nonsense if run on a weighted graph. BFS will always find the path that has the fewest number of nodes which just happens to be the shortest path if all weights are the same. You certainly can modify BFS to use a priority queue instead of a normal queue so that it then really finds a shortest path. But then DFS is the same as BFS just with a stack instead of a queue. So you would have to add the part about shortest paths in the DFS article, too. All three algorithms differ (mainly) only by just the datastructure used to save the nodes in, but their output is absolutely different. --Regnaron 21:14, 26 September 2006 (UTC)


The original formulation of BFS did not restrict the queue to be a FIFO queue. If you use BFS with a FIFO queue on a weighted graph then you're effectively discarding the edge weights. That's why you get a different result from the algorithm with a priority queue versus FIFO queue: you're changing the input. DFS is completely different because you get a result that you won't get merely by changing the inputs. BFS has been used with a priority queue in scheduling simulators long before Djikstra's algorithm got its name. It is not limited to unweighted graphs!

If you use BFS with a FIFO queue on a weighted graph then you're effectively discarding the edge weights. That's why you get a different result from the algorithm with a priority queue versus FIFO queue: you're changing the input. And exactly that result you get is wrong. It is not a shortest path but the path with fewest nodes. If you use it with a priority queue, the result is correct, but then, where is the difference to Dijkstras algorithm? Find the vertex that is first in the queue, update its value and never look at it again. Just the same as Dijkstra. --Regnaron 05:43, 4 October 2006 (UTC)