O'Reilly logo

Hands-On Data Structures and Algorithms with JavaScript by Kashyap Mukkamala

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required