Search⌘ K
AI Features

Adjacency List

Explore how to represent graphs using adjacency lists in C++. This lesson helps you understand storing neighbors for each node with vectors, improving efficiency for graph algorithms compared to adjacency matrices. You will learn the structure and implementation details behind adjacency lists to apply in various graph problems.

The second option to represent graph edges in code is the adjacency list.

Adjacency list

In this data structure, we store a list of each node’s neighbors.

Let’s look at our example graph again. We use the same integer encoding of nodes that we did for the adjacency matrix.

g 2 2 0 0 2->0 3 3 2->3 1 1 0->1 1->2 1->0 1->3
The example graph with integer node names

The node with index 11 has three neighbors: 00 ...