3.5.2 · D1Graphs

Foundations — Representations — adjacency matrix (space O(V²)), adjacency list (space O(V+E))

2,136 words10 min readBack to topic

Before you can compare an adjacency matrix with an adjacency list, you must be able to read every symbol the parent note throws at you. Below, each symbol is built from nothing — plain words first, then the picture, then why the topic can't live without it. Read top to bottom; each one leans on the one above.


1. The dot: a vertex

Look at the figure below: four dots, labelled . In coding we number our things starting from (not ) because array slots are numbered from . So "vertex 2" is just "the dot we chose to call 2."

Figure — Representations — adjacency matrix (space O(V²)), adjacency list (space O(V+E))

Why the topic needs it: every storage method has to have a name for each thing so it can say "thing 2 is connected to thing 0." No dots, nothing to connect.


2. The line: an edge

In the figure above, the line between dot and dot is the edge that says "0 and 1 are friends."

Two flavours, shown side by side below:

Figure — Representations — adjacency matrix (space O(V²)), adjacency list (space O(V+E))
  • Undirected edge — a plain line. Friendship: if you are my friend, I am yours. Written with curly braces because the order does not matter: and are the same line.
  • Directed edge — an arrow. "Follows on social media": I can follow you without you following me. Written with round braces , an ordered pair, because is not the same as .

Why the topic needs it: the edges are the information we're storing. Everything else is bookkeeping around them.


3. The letters and : generic vertex names

Think of and like the "" in "let be a number" — a stand-in so we can talk about the rule without naming a specific dot. In A[u][v] the picks a row and picks a column.

Why the topic needs it: the storage rules ("store the edge in adj[u] and adj[v]") must work for every pair, so we describe them with placeholders.


4. The whole picture:

Why the topic needs it: every sentence in the parent ("a matrix", " space") is written in terms of these two bags. This is the vocabulary of the whole chapter.


5. The bars and : how many

Why the topic needs it: space and time costs are numbers, so they depend on counts (, ), not on the abstract bags. is meaningless until means "a number."


6. Degree and

In the figure of Section 1, dot touches dots , , and , so . Dot touches only dot , so .

Why the topic needs it: the parent's cost "list neighbours of is " is meaningless unless you know is "how many neighbours actually has" — and the list-size claim rests entirely on this.


7. Neighbours and the neighbour list adj[u]

In the figure, adj[2] = [0, 1, 3] — dot 2's three neighbours. Notice adj[u] has exactly items. That's the link between Section 6 and how the list representation stores things.

Why the topic needs it: the adjacency list representation is literally an array of these neighbour lists. Understanding "neighbour" is understanding the list.


8. The grid and the entry

Read it like a battleship board or a seating chart: go down to row , across to column , read the cell. The figure below shows the grid for our 4-dot graph — a means "edge here," a means "no edge."

Figure — Representations — adjacency matrix (space O(V²)), adjacency list (space O(V+E))

Why the topic needs it: the entire adjacency-matrix representation is "let when there's an edge." Every space and time claim about the matrix is a claim about this grid.


9. The counting symbol

Why the ? Picture picking two dots: the first can be any of , the second any of the remaining , giving ordered picks. But and are the same unordered pair, so we've double-counted — divide by .

This is the maximum possible number of edges in an undirected graph with no repeats and no self-loops: you can't have more lines than there are distinct pairs to join.

Why the topic needs it: it sets the ceiling . That ceiling is what makes the matrix's cells "not crazy" and what defines dense (near the ceiling) vs sparse (far below it) — see Sparse vs Dense Graphs.


10. The growth symbol:

For example means "grows roughly like the number of vertices plus the number of edges." means "grows like the square of the vertex count" — double and the cost roughly quadruples. Full treatment in Big-O Notation.

Why the topic needs it: the whole comparison — matrix space vs list space — is stated in this language. Without Big-O you cannot even state the trade-off.


11. Sparse vs dense

Why the topic needs it: this single word decides the winner. Sparse → list wins on space. Dense → the list is also and the matrix's edge check wins. See Sparse vs Dense Graphs.


How these feed the topic

vertex = dot

graph G = V and E

edge = line

count bars size of V and E

choose 2 max edges

Big-O growth label

sparse vs dense

degree how many lines touch

neighbour list adj of u

matrix grid A of u v

Adjacency List space O of V plus E

Adjacency Matrix space O of V squared

which representation wins

Everything on the left is built in this page; the two boxes K and L are the parent topic Representations, and M is the decision it teaches you to make. These foundations also power Breadth-First Search (BFS), Depth-First Search (DFS), Dijkstra's Algorithm, Floyd-Warshall Algorithm, and the walk-counting fact needs Matrix Multiplication.


Equipment checklist

Hide the right side and test yourself — if any answer is fuzzy, re-read that section before touching the parent note.

What is a vertex, in one word and one picture?
A "thing" in the network, drawn as a dot.
What does an edge represent, and what's the difference between and ?
A connection (line); curly is an unordered two-way edge, round is a one-way arrow.
What do and stand for?
Placeholder names for "any two vertices."
Read out in plain words.
A graph is made of a bag of vertices and a bag of edges .
What do the bars mean in and ?
Count how many items are in the bag — number of vertices, number of edges.
What is ?
The number of edges touching vertex (its neighbour count).
Why does ?
Each edge is counted once from each of its two endpoints.
What is adj[u] and how many items does it hold?
The list of 's neighbours; it holds exactly items.
What does mean, and when is symmetric?
The cell in row , column ; (symmetric) exactly when the graph is undirected.
What does count, and why the ?
The number of distinct vertex pairs (max edges); divide by 2 to undo double-counting ordered picks.
In plain words, what does mean?
The cost grows roughly like the vertex count plus the edge count.
When is a graph sparse vs dense?
Sparse: (few edges); dense: (almost fully connected).