Bassel Al-assaad
Bassel Al-assaad

Reputation: 3

Breadth-First Search

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.

Upvotes: 0

Views: 389

Answers (1)

Andres Salgado
Andres Salgado

Reputation: 243

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

Upvotes: 3

Related Questions