3.5.2 · D5Graphs

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

1,535 words7 min readBack to topic

Before we start, one shared vocabulary so no symbol is unearned:


True or false — justify

Each item is a statement. The reveal says T/F and why — the "why" is the point.

Adjacency matrix always uses more space than adjacency list
False. On a dense graph , so the list is also ; the space tie means the matrix's edge check makes it competitive, not worse.
An adjacency list uses space
False. It uses : you allocate one list header per vertex even for isolated (degree-0) vertices, so a graph with and edges still costs .
For an undirected graph the adjacency matrix equals its own transpose
True. Edge sets both and , so — the grid is symmetric across the diagonal.
An undirected edge takes exactly one entry in an adjacency list
False. It is stored twice — once in and once in — so total entries , keeping neighbor lookups symmetric.
Checking isEdge(u,v) is in an adjacency list
False. You must scan 's neighbor list, costing ; only the matrix gives true via a direct index .
Listing all neighbors of costs in an adjacency list
False. It costs — you only touch the actual neighbors. The scan is the matrix's cost, because you must walk all columns to find the few 1s.
Iterating over all edges is asymptotically equal in both representations
False. The matrix must inspect all cells (); the list visits only real entries (). This gap is exactly why BFS/DFS use lists.
Breadth-First Search (BFS) and Depth-First Search (DFS) run in regardless of representation
False. They are on an adjacency list; on a matrix each vertex's neighbor scan is , pushing the traversal to .
A weighted graph cannot be stored in an adjacency matrix
False. Store the weight in the cell instead of a , and use a sentinel ( or a special value) for "no edge."
Adding an edge is in both representations
True. Matrix: set one cell. List: push to the front of a list. Both are constant-time (assuming amortized push for dynamic arrays).

Spot the error

Each line is a wrong claim. The reveal names the flaw and corrects it.

"A social network of users with ~100 friends each fits fine in a matrix."
Wrong. cells = terabytes, while the graph is sparse (). Compare to first — sparse means list.
" counts the shortest path length from to ."
Wrong. It counts the number of walks of length exactly 2, not a shortest distance. = count of length- walks; see Matrix Multiplication.
"For Floyd-Warshall Algorithm on you should use a list to save space."
Wrong. The algorithm indexes directly and needs edge weight lookups inside loops; a -cell matrix matches its access pattern perfectly.
"Because the matrix wastes cells, an empty graph costs in it."
Wrong. The matrix cost is independent of — you allocate every ordered pair's cell even for an empty graph. Only the list drops to when empty.
"An undirected graph's list has entries, so it's smaller than a directed one's."
Wrong. Undirected stores each edge in both endpoints ( entries); a directed graph stores each edge once ( entries). Undirected costs more per edge, not less.
"The diagonal is always 1."
Wrong. The diagonal is unless self-loops are allowed; would falsely claim vertex has an edge to itself.
"Dijkstra's Algorithm is always faster with a matrix because edge lookups are ."
Wrong. Dijkstra repeatedly lists neighbors of the popped vertex; on a matrix that's per vertex → total, worse than the list's on sparse graphs.

Why questions

Why is the matrix even when the graph has almost no edges?
Because it reserves one cell for every ordered pair cells — and this allocation is decided by alone, never by how many edges actually exist.
Why does one undirected edge create two list entries instead of one?
So that both and can answer "who are my neighbors?" symmetrically; storing it once would hide the edge from one endpoint's lookups.
Why do the two representations' space costs tie on dense graphs?
On a dense graph , so the list's becomes — the same order as the matrix. The saving of a list only exists when .
Why can the matrix answer isEdge(u,v) in but the list cannot?
The matrix indexes a fixed memory slot directly; the list has no slot per pair, so it must search 's neighbor list, costing .
Why is the adjacency list the default choice in practice?
Most real-world graphs (road maps, social networks, web links) are sparse grows near-linearly in — so storage and neighbor scans dominate the matrix. See Big-O Notation.
Why does count walks, using Matrix Multiplication?
Each multiply sums over an intermediate vertex : adds 1 for every that is a neighbor of both — i.e. every 2-step route — and induction extends this to length .
Why does listing neighbors cost in a matrix but only in a list?
The matrix stores the neighbor info spread across a whole row of cells (mostly 0s you must skip); the list stores only the actual neighbors contiguously.

Edge cases

Zero edges, : what does each representation cost?
Matrix still cells (allocation ignores ); list drops to — just the empty headers. The list crushes the matrix here.
A complete graph (every pair connected): which representation wins on space?
Neither on order — so both are . The matrix then wins on speed ( edge checks), not space.
A single isolated vertex (degree 0): how many list entries does it hold?
Zero neighbor entries, but it still occupies one list header — this is exactly why the list bound keeps the term in .
A self-loop edge : how is it recorded?
In the matrix, set the diagonal cell (or its weight); in the list, add to . It counts as one edge touching 's degree (conventionally +2 in undirected degree, or +1 as an entry).
Maximum possible edges in an undirected simple graph on vertices?
— every unordered pair connected once, no self-loops, so edges can never exceed the matrix's capacity.
A directed graph: how many list entries per edge, and is the matrix still symmetric?
One entry per directed edge (only in ), giving total entries; the matrix is not symmetric since need not equal .
, no edges (a lone vertex): compare the two costs concretely.
Matrix: a grid = 1 cell. List: 1 empty header = . A degenerate but valid case — both are constant, confirming the formulas hold at the boundary.

Active Recall

Recall Rapid re-test (cover and answer aloud)
  1. Why is the matrix independent of ?
  2. When do the two space costs become equal?
  3. Why is isEdge only in the matrix?
  4. Why does BFS/DFS prefer the list?

Answers: 1) one cell per ordered pair, allocated by alone. 2) dense graphs, . 3) direct index vs list scan. 4) traversal is on a list but on a matrix.