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

Vocabulary

Lesson flow

1. Opening: Four Different Worlds, One Same Shape (8 min)

  1. Put four scenarios on the board and ask what they have in common:
  2. Packet routers connected by fiber-optic cables (Grade 6 Computing)
  3. Meadow habitat patches connected by wildlife movement corridors (Grade 10 Science: Ecology Field Sampling)
  4. Web pages connected by clickable hyperlinks (Grade 8 Society: Information Literacy)
  5. Cities connected by rail lines
  6. 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!
  7. Ask: "Which of those four has one-way (directed) edges by default?"
  8. 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

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)

  1. Part A — Directed Graph Practice (9 min): Pairs are given a 4-page mini-web of directed hyperlinks:
  2. Page 1 links to 2 and 3 (1 → 2, 1 → 3)
  3. Page 2 links to 3 (2 → 3)
  4. Page 3 links to 1 and 4 (3 → 1, 3 → 4)
  5. Page 4 links to 2 (4 → 2) Students write:
  6. The 4×4 directed adjacency matrix (and verify it is not symmetric: matrix[1][2] = 1, matrix[2][1] = 0).
  7. The in-degree (column sum = incoming links) and out-degree (row sum = outgoing links) of every page.
  8. Part B — Memory Calculation for a Real Road Map (8 min):
  9. Suppose a country road map has |V| = 100,000 intersections, and each intersection meets an average of 4 roads (200,000 undirected edges).
  10. Adjacency Matrix size: 100,000 × 100,000 = 10,000,000,000 (10 billion cells—over 99.996% of them filled with 0s!).
  11. 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)

5. Closing check (5 min)

  1. 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?
  2. Look at this adjacency list for a directed graph:
  3. X: [Y, Z]
  4. Y: [Z]
  5. Z: [] What are the out-degree and in-degree of vertex Z?
  6. 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

  1. 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.
  2. Vertex Z has out-degree 0 (its outgoing list Z: [] is empty) and in-degree 2 (both X and Y point to Z).
  3. 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

Teacher notes