...

/

Breadth First Search (BFS)

Breadth First Search (BFS)

Discover how to traverse graphs using breadth first search.

The second important traversal algorithm for graphs is breadth-first search (BFS). The main concept of BFS is that the closest not yet explored node is explored first. This is exactly the opposite strategy compared to DFS.

Explanation of BFS

Let’s execute the BFS algorithm on an example graph as we did for DFS.

In the end result of a BFS execution, all nodes are marked as finished and all distances to the starting node are recorded.

We’ll only discover nodes reachable from the starting vertex, just like we saw for DFS. If we want to explore the whole graph, running further BFS executions from yet undiscovered vertices is needed.

Edge types in BFS

When discussing DFS, ...