Search⌘ K

Representation of Graphs

Explore the two main methods to represent graphs in JavaScript: adjacency matrices and adjacency lists. This lesson helps you understand how to map vertices and edges efficiently, preparing you to implement and manipulate graph data structures for coding interviews and real-world applications.

Ways to Represent a Graph

The two most common ways to represent a graph are:

  1. Adjacency Matrix
  2. Adjacency List

Adjacency Matrix

The adjacency matrix is a two-dimensional matrix where each cell can contain a 0 or a 1.​ The row and column headings represent the vertices.

If a cell contains 1, there exists an edge between the corresponding vertices e.g., Matrix[0][1]=1Matrix[0][1]=1 ...