Theory

Density: measured, not argued

In lesson 2 you worked out that 206,875 slice headers would cost transit almost five megabytes in pointers alone. And that lesson's gotcha said plainly:

This is a design note, not a measurement. Step 2's table is a measurement, this paragraph is an argument. Both are useful; only one of them is evidence.

Now you have a graph, and you can measure that question.

$ algo dense
nodes 1000, edges 744

                                  bytes            cells           used
  adjacency list                  29952                -              -
  adjacency matrix               125000          1000000        0.1488%

  the matrix is 4.2x larger

  visiting every neighbour of every node:
  adjacency list                   1488           0s
  adjacency matrix              1000000        527µs

The matrix is packed into bits, not bytes — otherwise the comparison would be unfair against it. And even so: 1,000,000 cells for 1,488 real edges. 0.1488% occupied.

And the gap is not a constant

$ for n in 1000 10000 100000; do algo gen -n $n -seed 3; algo dense; done

       n    edges     list bytes   matrix bytes      ratio         used
    1000      744          29952         125000       4.2x      0.1488%
   10000     7497         299976       12500000      41.7x      0.0150%
  100000    74998        2999984     1250000000     416.7x      0.0015%

Every tenfold increase in n multiplies the gap by ten as well. That is not a coincidence: the list grows as O(V + E) and the matrix as O(V²).

At 100,000 books the matrix would take 1.25 GB where the list needs 3 MB.

And the cost of a scan

The last lines matter more than the memory. To traverse a graph you need every node's neighbours:

  adjacency list                   1488
  adjacency matrix              1000000

672× more steps, of which 998,512 say "there is no edge". In a matrix every node has V candidate neighbours and all of them have to be checked — never mind that in reality there are one and a half.

That is why BFS and DFS on a matrix cost O(V²) rather than O(V + E). The cost written down in step 2 holds only for the list.

When a matrix is right after all

Not never. A matrix wins when:

  • the graph is dense — enough edges that E approaches , and then the matrix wastes nothing;
  • the common question is "is there an edge A–B?" — one step for a matrix, while a list has to scan A's neighbours;
  • V is small and fixed — a 50-city route table is 2,500 cells and there is no question to ask.

Your network's mean degree is 1.49. A dense graph at V = 1000 would need several hundred. The difference is not a matter of degree.

The representation is chosen by the question and the density, not by the graph. And both can be measured — which is what you just did.