I know in Depth-First search we always go with the left-most child, I was wondering if when we use BFS do we also have to go left to right or it doesn't matter ? thank you for your time.
-
It depends on what you're doing. Generally, it doesn't matter in both DFS and BFS. The only thing that changes is the order the nodes are visited.user9254539– user92545392018-06-24 20:56:31 +00:00Commented Jun 24, 2018 at 20:56
-
So in a binary tree where S is the start state and A is its left child and B is its right child and we have E and F(goal state) as left and right children of B is it right to go SBF in a DFS algorithm , or for BFS can we go SBEF or SBF ?? thank you for your helpBassel Al-assaad– Bassel Al-assaad2018-06-24 21:04:49 +00:00Commented Jun 24, 2018 at 21:04
1 Answer
The difference between both algorithms does not depend on where you start searching. Instead it depends on when you start searching.
In depth-first search, you always explore the children of the first child found until there are no more children (this could mean leftmost, rightmost, centermost, etc. depending on the application of the algorithm). You don't start searching the next child of a node until after exploring the children of the previous node.
In breadth-first search, you first identify all children in the order that they are given before going ahead and exploring the first child that you have identified. For example, if you are getting children in a left-to-right fashion, then you would "start from the left" and work to the right, and then you would go down to find a root.
Here is a great website that will allow you to play with bfs and dfs so that what I just said makes sense to you: https://visualgo.net/en/dfsbfs