Adjacency List
An adjacency list represents a graph as an array of linked lists. Each index in the array represents a vertex, and the linked list at that index contains all the vertices connected to it by an edge.
Implementation Details
In most modern languages, we use a Hash Map or a List of Lists to implement an adjacency list.
type Graph = Map<number, number[]>;
const graph: Graph = new Map();
graph.set(1, [2, 3]);
graph.set(2, [1]);
graph.set(3, [1]);
Advantages
- Space Efficient: It only stores the edges that actually exist.
- Easy to Traverse: Quick to find all neighbors of a given vertex.