Lesson 3: Modeling Networks — Graphs, Adjacency Lists, and Adjacency Matrices
Learning objective
Students will model real-world systems as graphs G = (V, E) (choosing directed vs. undirected and weighted vs. unweighted edges), convert a graph between its adjacency matrix and adjacency list representations, and compare their memory and lookup trade-offs.
Materials
- Grid paper or printed worksheets for drawing a 5×5 adjacency matrix and adjacency list
Vocabulary
- Graph (
G = (V, E)): A mathematical structure made of a set of vertices (nodes, V) and a set of edges (connections, E) linking pairs of vertices.
- Directed vs. Undirected Edge: An undirected edge
{u, v} is a two-way connection (u ↔ v); a directed edge (u → v) is a one-way arrow from u to v.
- Degree of a Vertex: The number of edges connected to that vertex (in a directed graph, split into in-degree and out-degree).
- Adjacency Matrix: A
|V| × |V| grid where row u, column v stores 1 (or edge weight w) if edge (u, v) exists, and 0 otherwise.
- Adjacency List: A dictionary/array where each vertex
u stores a list of only its direct neighbors.
Lesson flow
1. Opening: Four Different Worlds, One Same Shape (8 min)
- Put four scenarios on the board and ask what they have in common:
- Packet routers connected by fiber-optic cables (Grade 6 Computing)
- Meadow habitat patches connected by wildlife movement corridors (Grade 10 Science: Ecology Field Sampling)
- Web pages connected by clickable hyperlinks (Grade 8 Society: Information Literacy)
- Cities connected by rail lines
- In every case, we have nodes (vertices) and connections (edges). Once we represent any of these as a graph, the exact same algorithms can analyze all four!
- Ask: "Which of those four has one-way (directed) edges by default?"
- Web hyperlinks! Page
A can link to Page B (A → B) without Page B linking back to Page A.
2. Direct instruction: Adjacency Matrix vs. Adjacency List (15 min)
Consider this 5-vertex undirected regional network with vertices V = {A, B, C, D, E} and 6 edges:
E = { {A,B}, {A,C}, {B,C}, {B,D}, {C,D}, {D,E} }:
A --- B
| / |
| / |
C --- D --- E
Build both computer representations side by side:
Representation 1: 5×5 Adjacency Matrix (|V|² = 25 cells)
| From \ To |
A |
B |
C |
D |
E |
Row Sum (Degree) |
| A |
0 |
1 |
1 |
0 |
0 |
2 |
| B |
1 |
0 |
1 |
1 |
0 |
3 |
| C |
1 |
1 |
0 |
1 |
0 |
3 |
| D |
0 |
1 |
1 |
0 |
1 |
3 |
| E |
0 |
0 |
0 |
1 |
0 |
1 |
- Notice that for an undirected graph, the matrix is symmetric across the main diagonal (
matrix[u][v] == matrix[v][u]), and the sum of all 1s in the matrix (12) equals 2 × |E| (the Handshaking Lemma: every undirected edge adds 1 to the degree of both its endpoints!).
Representation 2: Adjacency List (|V| + 2|E| entries)
graph = {
"A": ["B", "C"],
"B": ["A", "C", "D"],
"C": ["A", "B", "D"],
"D": ["B", "C", "E"],
"E": ["D"]
}
3. Main activity: Sparse vs. Dense Trade-Off Analysis & Directed Conversion (17 min)
- Part A — Directed Graph Practice (9 min):
Pairs are given a 4-page mini-web of directed hyperlinks:
- Page
1 links to 2 and 3 (1 → 2, 1 → 3)
- Page
2 links to 3 (2 → 3)
- Page
3 links to 1 and 4 (3 → 1, 3 → 4)
- Page
4 links to 2 (4 → 2)
Students write:
- The 4×4 directed adjacency matrix (and verify it is not symmetric:
matrix[1][2] = 1, matrix[2][1] = 0).
- The in-degree (column sum = incoming links) and out-degree (row sum = outgoing links) of every page.
- Part B — Memory Calculation for a Real Road Map (8 min):
- Suppose a country road map has
|V| = 100,000 intersections, and each intersection meets an average of 4 roads (200,000 undirected edges).
- Adjacency Matrix size:
100,000 × 100,000 = 10,000,000,000 (10 billion cells—over 99.996% of them filled with 0s!).
- Adjacency List size:
100,000 vertex buckets holding 2|E| = 400,000 neighbor entries (25,000× fewer neighbor entries than the matrix, or |V| + 2|E| = 500,000 total entries including the vertex headers—20,000× smaller overall!).
4. Discussion questions (5 min)
- Question 1: "When is an Adjacency Matrix actually better than an Adjacency List?"
- Example good answer: When the graph is small or dense (nearly every vertex connects to every other vertex), or when you frequently need to check "Does edge
(u, v) exist?" in a single array lookup matrix[u][v] (O(1)).
- Question 2: "Why is the sum of the degrees of all vertices in any undirected graph always an even number?"
- Example good answer: Because every single edge connects two endpoints, adding
+2 to the total degree sum (Sum of degrees = 2 × |E|).
5. Closing check (5 min)
- An undirected graph has
|V| = 6 vertices and |E| = 8 edges. How many total cells are in its adjacency matrix, how many of those cells contain a 1, and how many contain a 0?
- Look at this adjacency list for a directed graph:
X: [Y, Z]
Y: [Z]
Z: []
What are the out-degree and in-degree of vertex Z?
- Would you use an adjacency matrix or an adjacency list to store a social network with
1,000,000 users where the average user has 150 connections? Explain why using numbers.
Worked answers for closing check
- A
6 × 6 adjacency matrix has 36 total cells. Since each of the 8 undirected edges places two 1s in the matrix (matrix[u][v] and matrix[v][u]), 16 cells contain 1 and 36 − 16 = 20 cells contain 0.
- Vertex
Z has out-degree 0 (its outgoing list Z: [] is empty) and in-degree 2 (both X and Y point to Z).
- An adjacency list, because it stores only
1,000,000 × 150 = 150,000,000 connection entries (~1.5 × 10⁸), whereas an adjacency matrix would require 1,000,000 × 1,000,000 = 1,000,000,000,000 (10¹², one trillion) cells—nearly all zeros.
Differentiation
- Extra support: Have students color-code each undirected edge
{u, v} with a matching pair of highlighters in the matrix (row u, col v and row v, col u) so the diagonal symmetry and the 2 × |E| count become visually immediate.
- Extra challenge: Ask students how the adjacency matrix and adjacency list change if we allow self-loops (an edge from a vertex to itself, like a web page linking to itself) and parallel edges (two separate roads between the same two towns).
Teacher notes
- Common misconception: When reading a directed adjacency matrix, students frequently swap row (
from) and column (to). Keep the header label From (Row) \ To (Column) visible on the board so out-degree is always the row sum and in-degree is always the column sum.