Skip to Content
Learning JavaScript Data Structures and Algorithms - Third Edition
book

Learning JavaScript Data Structures and Algorithms - Third Edition

by Loiane Avancini
April 2018
Beginner to intermediate content levelBeginner to intermediate
426 pages
10h 19m
English
Packt Publishing
Content preview from Learning JavaScript Data Structures and Algorithms - Third Edition

Creating the Graph class

As usual, we will declare the basic structure of our class:

class Graph {  constructor(isDirected = false) {    this.isDirected = isDirected; // {1}    this.vertices = []; // {2}    this.adjList = new Dictionary(); // {3}  }} 

The Graph constructor can receive a parameter to indicate if the graph is directed or not ({1}), and by default, the graph will not be directed. We will use an array to store the names of all the vertices of the graph ({2}), and we will use a dictionary (implemented in Chapter 8, Dictionaries and Hashes) to store the adjacent list ({3}). The dictionary will use the name of the vertex as a key and the list of adjacent vertices as a value.

Next, we will implement two methods: one to add a new vertex to the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning JavaScript Data Structures and Algorithms

Learning JavaScript Data Structures and Algorithms

Loiane Avancini

Publisher Resources

ISBN: 9781788623872Supplemental Content