A list or a matrix?
transcript
A maps app has ten million junctions. Each junction meets only a few roads. How do you store that graph? You have two choices.
A matrix has a cell for every pair. Four stations need sixteen cells. Ten million junctions need a hundred trillion. Almost every cell is zero.
A list keeps only real roads. Each station lists its neighbours. Four roads make eight entries. Memory follows the roads, not the pairs.
Few edges? Use a list. Nearly every pair joined? A matrix is fine. A matrix checks an edge in one read. Roads, friends and links are all sparse.
Is there a road between two junctions? A matrix reads exactly one cell. A list scans one short list. Keep a set, and that is one lookup too. So the list loses almost nothing.
Memory follows the edges in a list. It follows the vertices squared in a matrix. Try both in the sandbox. Full breakdown in the lecture.