3.5.2 · D4Graphs

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

2,223 words10 min readBack to topic

Every symbol we use is defined right here so you can start from line one:


Level 1 — Recognition

Goal: read a picture/definition and report the raw facts.

L1.1 — Count from a drawing

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

WHAT we do: just count dots and lines from figure s01.

  • (a) Dots .
  • (b) Lines drawn: .
  • (c) Lines touching vertex : to , to , to . (Look at the three cyan lines meeting the amber dot.)
  • (d) The lines have no arrowheads, so each is two-way → undirected. ✔

L1.2 — Read a matrix

Recall Solution

WHAT / WHY: to check an edge in a matrix we simply index the cell — that is the superpower of the matrix.

  • Edge : read no edge.
  • : count the s in row 1.
  • Symmetry check: , , and every mirrored pair matches, so undirected. ✔

Level 2 — Application

Goal: build a representation and compute exact space.

L2.1 — Build both representations

Recall Solution

WHAT we do: for every edge set both and (undirected ⇒ symmetric), and push each endpoint into the other's list.

Matrix (, rows/cols ): Why row 1 has three 1s: vertex touches → columns .

List:

0 -> [1]
1 -> [0, 2, 3]
2 -> [1]
3 -> [1, 4]
4 -> [3]

Total list entries (each of the edges appears twice). ✔

L2.2 — Exact space numbers

Recall Solution

WHY these formulas: matrix always allocates one cell per ordered pair → . List allocates headers plus endpoint entries (undirected).

  • (a) cells.
  • (b) entries.
  • (c) List is smaller. Factor — the list uses roughly 8600× less memory. Sparse graph → list wins. ✔

Level 3 — Analysis

Goal: reason about cost, symmetry, and which query pattern dominates.

L3.1 — Cost of an operation, both ways

Recall Solution
Operation Matrix List
isEdge(u,v) — index — scan 's list
list neighbours of — scan whole row
iterate all edges — scan every cell

BFS choice: BFS repeatedly asks "who are 's neighbours?" and visits each vertex/edge once. In a list that whole sweep costs , so BFS is . In a matrix each neighbour scan is , forcing . → Use the adjacency list. Same argument holds for Depth-First Search (DFS). ✔

L3.2 — Symmetry detective

Recall Solution

WHY symmetry: for undirected graphs forces both cells to , so .

  • (a) No. If it were undirected, would also be . The mismatch breaks .
  • (b) It is a directed graph: there is a one-way arrow but no arrow . Directed matrices need not be symmetric. ✔

L3.3 — Dense crossover point

Recall Solution

WHAT we do: set the two costs equal and solve for . But the maximum possible for a simple undirected graph is . So the list's raw entry count only reaches the matrix's cell count at (essentially) maximum density — a complete graph. For everything sparser, and the list stores fewer things. In Big-O terms both are once , so their space asymptotics tie for dense graphs, and the matrix's edge check becomes the tie-breaker. ✔


Level 4 — Synthesis

Goal: combine ideas — matrix powers, algorithm-driven choices.

L4.1 — Counting walks with matrix powers

Recall Solution

WHY matrix powers: counts every middle vertex neighbouring both and — i.e. every -step route. Compute :

  • (a) : routes and 2 walks.
  • (b) : only route (you can't step since no self-loop) → 1 walk. ✔

L4.2 — Match the representation to the algorithm

Recall Solution

Reasoning = access pattern + density.

  • (a) Matrix. Floyd–Warshall indexes dist[i][j] inside triple loops and needs edge weight lookups; cells is cheap. Its own runtime is so the matrix fits perfectly.
  • (b) List. (very sparse); Dijkstra sweeps each vertex's neighbours, total with a heap — a matrix would force neighbour scans, a disaster at .
  • (c) List. DFS visits each vertex and edge once → with a list, versus scanning matrix rows. ✔

Level 5 — Mastery

Goal: prove a general statement / design under constraints.

L5.1 — Prove the diagonal of equals degree

Recall Solution

WHAT / WHY: expand the matrix-product definition and use the meaning of 's entries. Each term equals exactly when is a neighbour of (then by symmetry), and otherwise. Summing once per neighbour of gives the number of neighbours: Sanity check against L4.1: triangle vertex has , and indeed . ✔

L5.2 — Design under a memory budget

Recall Solution
  • (a) Matrix needs cells. Since , the matrix fits. ✔
  • (b) List budget: . So a list could hold up to about edges — but a simple graph on vertices has at most edges, so the real cap is .
  • (c) Rule: if , the list stores fewer things (sparse → list); if is near (dense) and you need fast edge queries, take the matrix. Here is small enough that the matrix comfortably fits, so if you need isEdge the matrix is fine. ✔

Active Recall

Recall Rapid checks (hide, answer, reveal)
  1. For undirected , how many matrix cells and how many list entries does it create?
  2. Why is BFS with a list but with a matrix?
  3. What is for a simple undirected graph?
  4. At what do and meet, and what graph is that?

Answers: 1) two cells () and two list entries. 2) list visits neighbours total; matrix scans columns per vertex. 3) . 4) — a complete graph.

Cells created by an undirected edge in the matrix
Two — and
for a simple undirected graph
Space of a adjacency matrix
cells
Max edges in a simple undirected graph on 6000 vertices