Unfortunately there isn’t many sources for JavaScript when it comes to Data Structures. I have decided to share my knowledge about Graphs, so someone like you could find and learn from it. Firstly, I am assuming that you already have some idea about what Graphs are. I won’t bore you with the definition. Let’s get started with implementation.

Create an ES6 class

We will be implementing Directed Graph (Digraph) with Adjacency List. Also I will be using some ECMA2015+ syntax, which should not be difficult to understand.

In the constructor we create a new instance of Map object and we call it AdjList

Most people know the concept of shallow copy, but they come across deep copy less often. Let’s start by defining what these two terms mean:

What is “shallow copy”?

Before we talk about shallow copy, let’s quickly talk about reference to an object. Let’s use a simple example by declaring a variable that holds values for a one dimensional array.

const array1 = [1, 2, 3, 4, 5, 6, 7, 8]

And then we try to “copy” it to another variable.

const array2 = array1;

Guess what? Arrays are mutable type in Javascript! That means the value of the array can be changed after…

Ziyo Shams

Full Stack Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store