Graphs in Javascript

Daniel C Reyes
2 min readApr 11, 2021

Intro to Graphs in Javascript

What Is a Graph?

A graph is a collection of nodes with edges, arcs, or lines between them. Graphs are used to represents relationships and hierarchies and are composed of nodes and edges.For example if node A is connected to node B with an edge or line, we say node A is adjacent to node B. A node represents the objects in the graph. If Twitter is the graph, users and tweets are the objects. An edge or line represents the connections between the objects, nodes are connected by edges. A graph consists of a finite set of nodes and a set of edges connecting these nodes. Graphs can be directed or undirected. There are three ways to represent a graph. Most commonly an adjacency list or edge list, and a grid.

Directed Graph

A graph where an edge between nodes goes in one direction. On Twitter, John can follow Jane without Jane following John.They can be imagined like a one-way street.

directed graph : one way

Undirected Graph

A graph where an edge between nodes goes in both directions. On Facebook, if John is friends with Jane, Jane must also be friends with John. Think of a two way street.

undireected graph: two way

Applications of graphs

  • Used to represent social media networks. Each user is a node, and when users connect they create an edge.
  • Used to represent web pages and links by search engines. Web pages on the internet are linked to each other by hyperlinks. Each page is a node and the hyperlink between two pages is an edge.
  • Used to represent locations and routes in GPS. Locations are nodes and the routes connecting locations are edges. Used to calculate the shortest route between two locations.

Advantages and Disadvantages


  • Can quickly convey visuals over text
  • Usable to model a diverse number of subjects so long as they contain a relational structure


  • At a higher level, text can be time-consuming to convert to an image.
  • It can be difficult to see the existing edges or how many edges a given node has connected to it