How do we implement data structure graphs in JavaScript | Sololearn: Learn to code for FREE!

+2

How do we implement data structure graphs in JavaScript

JavaScript graphs

4/18/2021 11:03:58 AM

Enoch Asanda Hall

1 Answer

New Answer

+4

It sounds like you're referring to graph data structures or graph theory and nothing like bar graphs, line graphs or charts. Graph data structures can be represented many ways in JavaScript which are similar to other programming languages. You could have an adjacency matrix representation. Basically a 2 dimensional square array with every vertex associated with every other vertex. Every value in the Array would indicate if it is connected in the associated direction. Another way is something called an adjacency list. A JavaScript object could represent each vertex and you just add all vertices that node has a directed edge to. The object could be an Array or an object with key-value pairs. Undirected edges can be thought of as just 2 directed edges pointing in both directions. The only difference between this and what you might do in c, c++, or Java is you don't need a node or vertex class/struct defined in JavaScript. All data structures described here can be made in JavaScript: https://www.geeksforgeeks.org/comparison-between-adjacency-list-and-adjacency-matrix-representation-of-graph/