0

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.

2
  • 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. Commented 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 help Commented Jun 24, 2018 at 21:04

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.