I had a question regarding BFS. After expanding nodes whether it be a graph or a tree what path will BFS take as the solution from the starting point to the goal? Does it take into account the cost of moving from one node to the other and takes the lowest cost route or does it take the path with the least amount of nodes needed to get to the goal?
Asked
Active
Viewed 352 times
0
-
A simple BFS will find the path with the least amount of nodes. To find the path with least cost, try [Dijkstra's algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm), which is a modified BFS that uses a priority queue instead of a plain queue. – user3386109 Apr 12 '21 at 19:42
1 Answers
0
The classical breadth-first search algorithm does not take the weight of the edges into account. In each iteration, you simply put the direct reachable neighbors of the current node in the queue without any checks. You can find the shortest path between two nodes A and B in the sense of a minimal amount of "steps" that are necessary to reach node B from node A.

DMaLeZ
- 31
- 3
-
Does this mean that there can be more than one possible solution path? Assuming there are multiple paths with the same amount of nodes to the goal. – J458 Apr 12 '21 at 23:46
-
It depends on your implementation. The original breadth-first search algorithm, as the name implies, is a search algorithm that terminates immediately when the destination node is found. In this case, the founded path is the shortest path from the starting node to the desired node. You can implement the algorithm a bit differently and not terminate if the node is found, accumulating all paths if there are multiple. – DMaLeZ Apr 13 '21 at 10:56