Breadth first search

Breadth first search (BFS) is a type of traversal that focuses on visiting the nodes of the same level (or neighbors) before going deeper into the graph (to the neighbors of the neighbors).

Another condition to take into account is that we should visit each node just once. Let's see an example with the following graph:

Breadth first search

BFS example

In step 1, we visit node A. Then we pass to the first child of A: node B. We visit all the children of B: C, D, and E, before going into its grandchildren: F.

We have already implemented Vertex, Edge, and AdjacencyList structs. We are going to make an example of BFS based on a graph built with classes ...

Get Swift Data Structure and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.