Implementing BFS

To implement the previously discussed pseudo code, we will first simplify our data a little bit. There are two ways to do this, as follows:

  1. Create an adjacency matrix of the graph data that indicates the graph as a two-dimensional array of size m x m, which consists of 1s and 0s. 1s indicates a connection between the mrow node with mcolumn and 0s indicates no connection.
  2. We simplify the dataset and only extract the nodes as a map in which the key is the node and the value is the list of nodes that it is connected to.

Although both approaches are fine ways of approaching the problem, the first option is usually preferred and is popular because the second option is of a higher code complexity because of all the overhead of ...

Get Hands-On Data Structures and Algorithms with JavaScript now with O’Reilly online learning.

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