Skip to content

List vs Matrix

Lesson 2 of 16 · 5 min

List vs Matrix

Step 1 of 11

One graph, two ways to write it down. Four positions, four directed passes.

The Idea

Two ways to store the same graph. An adjacency list keeps, per node, only the neighbours it actually has — small when edges are scarce. An adjacency matrix reserves a cell for every possible pair, so "is A joined to B?" is one lookup, but blanks dominate.

Real-World Example

Football analysts log who passed to whom. Storing an eleven-by-eleven grid is fine, and instant to query, but scale it to every player in a league and almost every cell reads zero. Per-player pass lists hold the same information in a fraction of the space.

The Code

Your turn

Fill in the blank.

adj = {"MF": [], "FW": []}
adj["MF"].___("FW")     # record the pass MF -> FW
print(adj["MF"])        # ['FW']

Mini quiz

1 / 3

For many nodes and few edges, which representation wins?