Graph Representation

March 31, 2026
#graph #adjacency-matrix #adjacency-list #data-structures #interviewing

Introduction

Before you can run BFS, DFS, or any other graph algorithm, you need to decide how to store the graph in memory. That choice affects the time and space complexity of every operation you perform on it.

There are two standard representations: the adjacency list and the adjacency matrix. This tutorial covers both, when to use each, and how to handle directed, undirected, and weighted graphs. You should be familiar with Graphs Introduction and Arrays before reading this.

Quick reference

Consider this graph:

    A --- B
    |     |
    C --- D --- E

Vertices: A, B, C, D, E — Edges: A-B, A-C, B-D, C-D, D-E

As an adjacency list

A → [B, C]
B → [A, D]
C → [A, D]
D → [B, C, E]
E → [D]

Each vertex stores a list of its neighbors.

As an adjacency matrix

    A  B  C  D  E
A [ 0, 1, 1, 0, 0 ]
B [ 1, 0, 0, 1, 0 ]
C [ 1, 0, 0, 1, 0 ]
D [ 0, 1, 1, 0, 1 ]
E [ 0, 0, 0, 1, 0 ]

A 2D grid where matrix[i][j] = 1 means there’s an edge from vertex i to vertex j.

Adjacency List

Adjacency Matrix

Weighted graphs

For weighted edges, store the weight instead of just 1:

Weighted adjacency list

class WeightedGraph {
  constructor() {
    this.adjacencyList = new Map();
  }

  addVertex(vertex) {
    if (!this.adjacencyList.has(vertex)) {
      this.adjacencyList.set(vertex, []);
    }
  }

  addEdge(v1, v2, weight) {
    this.addVertex(v1);
    this.addVertex(v2);
    this.adjacencyList.get(v1).push({ node: v2, weight });
    this.adjacencyList.get(v2).push({ node: v1, weight });
  }
}

const g = new WeightedGraph();
g.addEdge('A', 'B', 4);
g.addEdge('A', 'C', 2);
g.addEdge('B', 'D', 3);
// A → [{ node: 'B', weight: 4 }, { node: 'C', weight: 2 }]

Weighted adjacency matrix

// Store weights instead of 0/1
// Use Infinity for no edge
const matrix = [
  //    A     B     C     D
  [    0,    4,    2,  Infinity], // A
  [    4,    0, Infinity,  3   ], // B
  [    2, Infinity, 0, Infinity], // C
  [Infinity, 3, Infinity,  0   ], // D
];

Weighted graphs are used in algorithms like Dijkstra’s shortest path, where edge weights represent distances or costs.

Directed graphs

In a directed graph (digraph), edges have a direction. The only change is that addEdge adds the connection in one direction only:

// Directed: A → B does NOT mean B → A
addEdge(from, to) {
  this.addVertex(from);
  this.addVertex(to);
  this.adjacencyList.get(from).push(to);
  // no reverse edge
}

For an adjacency matrix, matrix[i][j] = 1 means there’s an edge from i to j, but matrix[j][i] might be 0.

Adjacency list vs adjacency matrix

Operation Adjacency List Adjacency Matrix
Space O(V + E) O(V²)
Add edge O(1) O(1)
Remove edge O(E) O(1)
Check edge exists O(degree) O(1)
Get all neighbors O(degree) O(V)
Add vertex O(1) O(V²) — rebuild matrix

Where V = vertices, E = edges, and degree = number of edges on a vertex.

When to use which

Adjacency list when:

  • The graph is sparse (E « V²) — most real-world graphs
  • You frequently iterate over a vertex’s neighbors
  • Memory matters

Adjacency matrix when:

  • The graph is dense (E ≈ V²)
  • You need fast O(1) edge existence checks
  • The vertex count is small and fixed

Edge list

A third, simpler representation — just store edges as pairs:

const edges = [
  ['A', 'B'],
  ['A', 'C'],
  ['B', 'D'],
  ['C', 'D'],
  ['D', 'E'],
];

// Weighted version
const weightedEdges = [
  ['A', 'B', 4],
  ['A', 'C', 2],
  ['B', 'D', 3],
];

Edge lists are compact and useful for algorithms like Kruskal’s minimum spanning tree that process edges directly. But checking if a specific edge exists requires scanning the entire list — O(E).

Complexity summary

Representation Space Check Edge Get Neighbors Add Edge
Adjacency List O(V + E) O(degree) O(degree) O(1)
Adjacency Matrix O(V²) O(1) O(V) O(1)
Edge List O(E) O(E) O(E) O(1)

Choose your representation based on the operations your algorithm needs most. For BFS and DFS, adjacency lists are almost always the right choice since both algorithms iterate over neighbors repeatedly.

Thanks for visiting
We are actively updating content to this site. Thanks for visiting! Please bookmark this page and visit again soon.
Sponsor