3.5.2 · D3Graphs

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

3,474 words16 min readBack to topic

The scenario matrix

Below, = number of vertices, = number of edges. "Cell" = a distinct kind of situation. Every worked example downstream is tagged with the cell it covers.

Cell Case class The degenerate / edge point it probes
C1 Empty graph, Space still for the list, still for the matrix — the "zero input"
C2 Complete (dense) graph, The maximum edge count; space costs tie
C3 Directed graph One edge → one list entry, matrix not symmetric
C4 Weighted graph Cells hold , absent edge = sentinel
C5 Isolated vertex (degree 0) Why you cannot drop the term
C6 Self-loop The diagonal cell
C7 Real-world "which representation?" Compare vs to decide sparse/dense
C8 Exam twist — count walks via Matrix power meaning, a limiting/growth behaviour
C9 Multigraph (parallel edges) Where the plain matrix breaks and the list wins

We will hit every cell C1–C9 below.


Worked Examples

Example 1 — The empty graph (Cell C1)


Example 2 — The complete graph (Cell C2)


Example 3 — Directed graph (Cell C3)


Example 4 — Weighted graph with a missing edge (Cell C4)


Example 5 — Isolated vertex (Cell C5)


Example 6 — Self-loop (Cell C6)


Example 7 — Real-world "which representation?" (Cell C7)


Example 8 — Exam twist: count walks with (Cell C8)


Example 9 — Multigraph: where the plain matrix breaks (Cell C9)


Coverage check

Recall Did we hit every cell?

Match each example to its cell — hide and recall.

Example 1 covers which cell? ::: C1 — empty graph, Example 2 covers which cell? ::: C2 — complete/dense, space tie Example 3 covers which cell? ::: C3 — directed, matrix not symmetric Example 4 covers which cell? ::: C4 — weighted, missing edge Example 5 covers which cell? ::: C5 — isolated vertex, cannot drop Example 6 covers which cell? ::: C6 — self-loop on the diagonal Example 7 covers which cell? ::: C7 — real-world sparse road map, pick list Example 8 covers which cell? ::: C8 — counts walks Example 9 covers which cell? ::: C9 — multigraph breaks the boolean matrix

Back to the parent overview.