Think about memory usage and how many edges are stored.
An adjacency list stores only existing edges, so it uses less memory for sparse graphs. An adjacency matrix always uses space for all possible edges, which is inefficient for sparse graphs.
const graph = [ [0, 1, 0], [1, 0, 1], [0, 1, 0] ]; console.log(graph[0][2]);
Look at the value stored at row 0, column 2.
The adjacency matrix uses 0 to represent no edge and 1 to represent an edge. graph[0][2] is 0, meaning no edge between node 0 and node 2.
const graph: number[][] = [ [1], // neighbors of node 0 [0, 2], // neighbors of node 1 [1] // neighbors of node 2 ]; for (const neighbor of graph[1]) { console.log(neighbor); }
Look at the neighbors stored for node 1.
Node 1 has neighbors 0 and 2, so the loop prints 0 and then 2 on separate lines.
Think about how quickly you can check if an edge exists.
Adjacency matrix allows O(1) time to check if an edge exists between any two nodes, which is useful in dense graphs where many edges exist.
Consider memory usage and average number of connections per user.
Since each user has few friends compared to total users, adjacency list saves memory by storing only actual friendships instead of all possible pairs.