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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.