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.
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.
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.
| row | yes | no |
|---|---|---|
| A | 2 | 3 |
| B | 3 | 2 |
| C | 2 | 3 |
| D | 2 | 3 |
| E | 3 | 2 |
| all | 12 | 13 |
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.
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.
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.
| on the map at the top | grid | lists |
|---|---|---|
| a road from A to B? | 1 look: yes | 2 looks: A, B, yes |
| a road from A to E? | 1 look: no | 3 looks: A, B, D, no |
| every road leaving A | 5 looks | 3 looks: A, B, D |
| every road of the map | 25 looks | 17 looks |
| roads leaving | grid looks | lists' looks |
|---|---|---|
| A | 5 | 3 |
| B | 5 | 4 |
| C | 5 | 3 |
| D | 5 | 3 |
| E | 5 | 4 |
| all | 25 | 17 |
| question | grid | lists |
|---|---|---|
| a road from A to B? | 1 look | at most 1 + A's degree |
| every road leaving A | looks | 1 + A's degree |
| every road of the map | looks | looks |
Room matters too. For places, the grid always holds times 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 , 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.
the move: count row by row
the line:why it is allowed: Let be the cells. Each of the rows has a cell for each of the places. So is added times. For 5 places, 5 rows of 5.
the move: add them up
the line:why it is allowed: added times is times , written . For 5 places, 25 cells.
guess it, thenthe move: read it
the line: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.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: count each road twice
the line: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 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 roads give twos.
the move: add the twos
the line:why it is allowed: twos make twice . The map at the top has 6 roads, so 12 boxes.
guess it, thenthe move: add a box for each place
the line:why it is allowed: Each place adds its own box, more. For the map at the top, 5 + 12 = 17.
guess it, thenthe move: one way, one box a road
the line: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, of them.
guess it, thenthe move: add the ones
the line:why it is allowed: ones make .
guess it, thenthe move: add a box for each place
the line:why it is allowed: As in line 3, the places add . For the one-way map above, 4 + 5 = 9.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: put in 3n roads
the line:places most roads, each place times one fewer, halved 3 times the places 5 10 15 6 15 18 7 21 21 8 28 24 10 45 30 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 be the lists' boxes. Question 2 counts , and here is . Below 7 places, no map holds that many roads, as the table shows.
the move: add them up
the line:places grid cells list boxes 7 49 49 20 400 140 100 10,000 700 1,000 1,000,000 7,000 The grid's cells against the lists' 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 is , and and make .
guess it, thenthe move: set it against the grid
the line:- the grid,
- the lists,
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 , and line 2 the lists . Both are times a number, 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 .
guess it, thenthe move: read it
the line:why it is allowed: The last row of line 2's table.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
a worked example
Store the one-way map above as a grid and as lists.
- Its roads: P to Q, Q to R, R to P, R to S and S to Q. That is 5 roads.
- 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.
- The grid is no mirror now. Row P, column Q says yes, but row Q, column P says no.
- 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.
- 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.
the trap
check yourself
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.
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.
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.