Adjacency list
A way to store a graph with a list of neighbors for each vertex. It only stores edges that exist: it takes O(n + m).
Example
If 1 is joined to 2, 3 and 5, its list is [2, 3, 5]. With 1 million users and 200 friends each, about 200 million entries instead of 1 trillion.
Test yourself: Adjacency list
Graphs
What does an adjacency list store?
If 1 is joined to 2, 3 and 5, its list is [2, 3, 5]. It doesn't store the 0s for pairs with no edge.
Graph operations
With adjacency lists, adding a new vertex is O(1).
It's creating 1 empty list for it. The other 999 lists of a 1,000-vertex graph aren't touched.
Graph operations
Where are adjacency lists kept so a vertex's list is found in O(1)?
With 10,000 vertices, going to "Ana"'s list is 1 step with a hash; in an unindexed list, up to 10,000.
0/3
â–¶ Play