Skip to content

storing a graph, grid or lists

shapes of data

what it is

A program keeps a map as a grid of yes and no, or as a list of roads for each place. This page counts what each costs, in room and in looks.

The map below has 5 places and 6 roads, each running both ways. A program cannot keep a drawing. It keeps the map in one of two ways: as a grid, or as lists.

A2B3C2D2E3
A map of 5 places, A to E, and 6 roads, each running both ways. Above each place, its degree.

The grid has a row and a column for each place. The cell in row A, column B says yes when a road joins A and B, and no when none does. Books call it an adjacency matrix.

AABBCCDDEE
The map's grid. A dot is yes, a plain cell no. The shaded corner line is each place against itself. Row A has a yes under B and under D, its two roads. The road A to B, in red, is two yeses, row A, column B and row B, column A, mirror images across the shaded line.

A road both ways is two yeses, so the grid is its own mirror across its corner line. That line, each place against itself, is all no: no road runs from a place to itself.

rowyesno
A23
B32
C23
D23
E32
all1213
Each row's yeses are its place's degree. The 12 yeses are twice the 6 roads. 12 and 13 make the 25 cells.

The lists keep, for each place, the places its roads lead to. Each place has a box of its own, so even a place with no roads has a box to start from. After that box, one box for each place its roads lead to: A's list is B and D. Books call them adjacency lists. The road A to B ends at A and at B, so it is B in A's list and A in B's. After its own box, a list is as long as its place's degree.

ABDBACECBEDAEEBCD
The map's lists. Each place, edged heavy, then the places its roads lead to. The road A to B, in red, is two boxes, B in A's list and A in B's.

On one-way roads, a road is one yes, in the row of the place it leaves, and one box, in that place's list. The example below stores this map.

P1Q1R2S1
A map of 4 places and 5 one-way roads, each running toward its head. Above each place, its roads out.

Each way answers some questions fast and others slowly. Is there a road from A to B? The grid looks once, at row A, column B. The lists read A's box, then walk along A's list. Which roads leave A? The grid reads all of row A. The lists read only A's box and A's roads. Which roads does the whole map have? A search, a program finding its way across the map, asks this one, as it reads every road it meets.

AABBCCDDEE
ABDBACECBEDAEEBCD
Every road leaving A, framed in dashes. The grid reads all 5 cells of row A. The lists read A's own box, then B and D, 3 boxes.
on the map at the topgridlists
a road from A to B?1 look: yes2 looks: A, B, yes
a road from A to E?1 look: no3 looks: A, B, D, no
every road leaving A5 looks3 looks: A, B, D
every road of the map25 looks17 looks
The three questions on the map at the top, in looks at a cell or a box. The lists stop at a yes, but a no reads all of A's list.
roads leavinggrid lookslists' looks
A53
B54
C53
D53
E54
all2517
Every road leaving each place, then all of them added up. The grid reads a whole row, 5 cells, each time. The lists read a place's own box and its roads, 1 more than its degree.
questiongridlists
a road from A to B?1 lookat most 1 + A's degree
every road leaving Ann looks1 + A's degree
every road of the mapn2n^2 looksn+2mn + 2m looks
The same questions for nn places and mm roads both ways. Add the middle row over every place, and you get the last. Every cell or box is read once, so the last row is the room questions 1 and 2 below count.

Room matters too. For nn places, the grid always holds nn times nn cells, however few the roads. The lists hold a box for each place, then two for each road both ways, or one for each one-way road. Questions 1 to 3 below count both.

where the formulas come from

Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about nn, or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.

question 1A map has nn places. How many cells does its grid hold?
  1. the move: count row by row

    the line:
    G=n+n+⋯+nG = n + n + \dots + n

    why it is allowed: Let GG be the cells. Each of the nn rows has a cell for each of the nn places. So GG is nn added nn times. For 5 places, 5 rows of 5.

  2. the move: add them up

    the line:
    G=n2G = n^2

    why it is allowed: nn added nn times is nn times nn, written n2n^2. For 5 places, 25 cells.

  3. the move: read it

    the line:
    6 roads or none:still 25 cells\begin{gathered} \text{6 roads or none:} \\ \text{still } 25 \text{ cells} \end{gathered}

    why it is allowed: A road both ways turns two nos into yeses and takes no room of its own. The map at the top fills 12 of its 25 cells.

try it with a small nn: at n=5n = 5, line 1 gives G=25G = 25, which you can work out by hand

question 2A map has nn places and mm roads, each both ways. How many boxes do its lists hold?
  1. the move: count each road twice

    the line:
    T=2+2+⋯+2T = 2 + 2 + \dots + 2
    ABDBACECBEDAEEBCD
    The lists of the map at the top. Each place's own box first, then its list. 6 roads, 12 boxes after the places' own.

    why it is allowed: Let TT be the boxes after the places' own. A road joining A and B puts B in A's list and A in B's, 2 boxes. So mm roads give mm twos.

  2. the move: add the twos

    the line:
    T=2mT = 2m

    why it is allowed: mm twos make twice mm. The map at the top has 6 roads, so 12 boxes.

  3. the move: add a box for each place

    the line:
    n places:n+2m boxes in all\begin{gathered} n \text{ places:} \\ n + 2m \text{ boxes in all} \end{gathered}

    why it is allowed: Each place adds its own box, nn more. For the map at the top, 5 + 12 = 17.

  4. the move: one way, one box a road

    the line:
    one way:1+1+⋯+1\begin{gathered} \text{one way:} \\ 1 + 1 + \dots + 1 \end{gathered}

    why it is allowed: A one-way road is one box, in the list of the place it leaves, as the opening says. So line 1's twos are ones, mm of them.

  5. the move: add the ones

    the line:
    one way:m boxes after the places\begin{gathered} \text{one way:} \\ m \text{ boxes after the places} \end{gathered}

    why it is allowed: mm ones make mm.

  6. the move: add a box for each place

    the line:
    one way:n+m boxes in all\begin{gathered} \text{one way:} \\ n + m \text{ boxes in all} \end{gathered}

    why it is allowed: As in line 3, the places add nn. For the one-way map above, 4 + 5 = 9.

try it with a small mm: at m=6m = 6, line 1 gives T=12T = 12, which you can work out by hand

question 3nn places and 3n3n roads, both ways, 3 times as many roads as places. Which holds less, the grid or the lists?
  1. the move: put in 3n roads

    the line:
    L=n+2⋅3nL = n + 2 \cdot 3n
    placesmost roads, each place times one fewer, halved3 times the places
    51015
    61518
    72121
    82824
    104530
    The most roads, from graphs and their words, question 2, against 3 times the places. Halving one fewer reaches 3 just when one fewer is at least 6, so too few below 7 and enough from 7 on.

    why it is allowed: Let LL be the lists' boxes. Question 2 counts n+2mn + 2m, and here mm is 3n3n. Below 7 places, no map holds that many roads, as the table shows.

  2. the move: add them up

    the line:
    L=7nL = 7n
    placesgrid cellslist boxes
    74949
    20400140
    10010,000700
    1,0001,000,0007,000
    The grid's n2n^2 cells against the lists' 7n7n boxes, on maps of 3 times as many roads as places. Equal at 7, far apart at 1,000.

    why it is allowed: 2 times 3n3n is 6n6n, and nn and 6n6n make 7n7n.

  3. the move: set it against the grid

    the line:
    grid: n⋅nlists: 7⋅n\begin{gathered} \text{grid: } n \cdot n \\ \text{lists: } 7 \cdot n \end{gathered}
    cells or boxesplaces010020030040005101520cells or boxesplaces010020030040005101520
    • the grid, n2n^2
    • the lists, 7n7n
    The grid's cells and the lists' boxes for 1 to 20 places. No map below 7 places holds 3 times as many roads as places, so read it from 7, where the lines meet. Then the grid's line climbs away.

    why it is allowed: Question 1 gives the grid n⋅nn \cdot n, and line 2 the lists 7⋅n7 \cdot n. Both are nn times a number, nn for the grid and 7 for the lists. At 7 places they are equal, 49 each. Past 7 the grid holds more, and the gap grows with nn.

  4. the move: read it

    the line:
    1,000 places:7,000 boxes, not 1,000,000\begin{gathered} 1{,}000 \text{ places:} \\ 7{,}000 \text{ boxes, not } 1{,}000{,}000 \end{gathered}

    why it is allowed: The last row of line 2's table.

try it with a small nn: at n=7n = 7, line 1 gives L=49L = 49, which you can work out by hand

a worked example

Store the one-way map above as a grid and as lists.

  1. Its roads: P to Q, Q to R, R to P, R to S and S to Q. That is 5 roads.
  2. The grid: 4 rows of 4, 16 cells. Each road is one yes, in the row of the place it leaves. 5 yeses, 11 nos.
  3. The grid is no mirror now. Row P, column Q says yes, but row Q, column P says no.
  4. The lists: P leads to Q, Q to R, R to P and S, S to Q. 4 boxes for the places and 5 for the roads, 9 in all.
  5. Is there a road from Q to P? The grid looks once, at row Q, column P: no. The lists read Q's box, then its list, R alone: 2 looks, no.
PPQQRRSS
PQQRRPSSQ
The one-way map's grid, rows the places a road leaves, and its lists. The road R to S, in red, is one yes and one box.

the trap

check yourself

  1. A map has 8 places and 10 roads, each both ways. How many cells does its grid hold, and how many say yes?

    the answer

    By question 1, 8 times 8, which is 64. Each road is two yeses, so 20 say yes and 44 say no.

  2. A map of 6 places has every road, both ways. Which holds less, its grid or its lists?

    the answer

    Neither. The grid holds 6 times 6, 36 cells. By graphs and their words, question 2, every two of 6 places joined make 6 times 5, halved, 15 roads. Each is 2 boxes, so the lists hold 6 + 30 = 36 boxes.

  3. A map has 10 places and 30 one-way roads. How many cells, and how many boxes?

    the answer

    By question 1, 10 times 10, which is 100 cells. One way, a road is one box, so the lists hold 10 + 30 = 40 boxes.