Computational Thinking · Week 8 — Adjacency & labelled graphs
1031 words
5 min read
2026-08-16T00:00:00.000Z
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
adjacency matrix, edge labels, representation switch — concepts, pattern families, and traps for Quiz 2 week 8. # Week 8 — adjacency & labelled graphs > **Quiz 2 scope:** Weeks 1–8 per IITM May 2026 foundation courses.

Week 8 — adjacency & labelled graphs
Quiz 2 scope: Weeks 1–8 per IITM May 2026 foundation courses. Source baseline: IITM BS admissions important-dates calendar · May 2026 cycle. Times on assessments are operational conventions — verify hall ticket.
Part of the Quiz 2 prep system%20%C2%B7%20%5BWeeks%201%E2%80%938%20index%5D(.%2Fmay-2026-ct-quiz-2-weeks-1-8-prep) · Pattern atlas · Formula chains.
Week map
Adjacency matrix → labelled edges → switch list ↔ matrix view
Classify → Represent → Execute → Trap-check
- Recognize: Ask: How fill matrix from edge list?
- Procedure: Zero matrix; for each edge (u,v,w) set M[u][v]=w (and M[v][u] if undirected).
- Variations / traps: Watch for: Off-by-one vertex numbering (1..n vs 0..n-1).
Formula chain (compressed)
labelled edges → matrix stores weight/label → switch list ↔ matrix.
- Labelled edge —
edge (u,v,w)— weight or name w - Matrix entry —
M[u][v] = label or 0— no edge = sentinel - Directed —
M[u][v] ≠ M[v][u]— one-way edges - Representation switch —
list ↔ matrix— same graph, two views - Self-loop —
M[i][i] nonzero?— check diagonal
Open interactive formula desk · Week 8 tab.
Deep study
Computational Thinking · Week 8 — Labelled adjacency graphs
Deep study for Quiz 2 week 8. Labels on edges carry weights, costs, or names — adjacency structures must store values, not just 0/1.
Week map
Unweighted vs labelled → weight matrix → multi-graph caution → shortest-path intuition → reading labelled diagrams.
Labelled edge notation
- Label → data on edge → weight, distance, time, capacity.
- Weighted graph → each edge has numeric label → often non-negative in intro problems.
- M[i][j]=w → edge from i to j has weight w.
- M[i][j]=0 or ∞ → no edge (convention varies — read problem statement).
Mini-example: three cities 0, 1, 2. Direct roads: 0→1 weight 4, 1→2 weight 2, 0→2 weight 10.
text1 --4-- 2 \ | 10 2 \ | ---- 3
(Adjust to matrix form in examples below.)
Weight matrix
textW = [ 0 4 10] [ ∞ 0 2] [ ∞ ∞ 0]
Use ∞ or blank for missing edge. W[0][1]=4, W[1][2]=2. Path 0→1→2 has total weight 4+2=6, better than direct 0→2 weight 10.
Adjacency list with labels
Alternative to matrix:
text0 → [(1, 4), (2, 10)] 1 → [(2, 2)] 2 → []
Each entry (neighbor, weight). Space-efficient for sparse graphs.
Directed vs undirected weights
Undirected road length w on {i,j} → typically W[i][j]=W[j][i]=w.
Directed → only one direction gets weight unless both stated.
Pattern families
Easy — Read label from diagram
Identify weight on edge between two named vertices. List all neighbors with weights from one vertex.
Medium — Build weight matrix
From edge list (u,v,w) fill W[u][v]. Handle missing edges with 0 or ∞ per convention.
Hard — Compare path totals
Sum weights along path. Compare two routes. Greedy “pick lightest edge next” may fail globally — but Quiz 2 often asks direct sum comparison only.
Worked mini-examples
Example 1 — Matrix fill.
Edges: (0,1,3), (1,2,5). Directed:
textW[0][1]=3, W[1][2]=5, others missing
Example 2 — Path sum.
Path 0→1→2: weights 3 + 5 = 8.
Example 3 — Undirected symmetry.
Edge A—B weight 7 → W[A][B]=W[B][A]=7.
Example 4 — No edge.
W[2][0] missing → no direct 2→0 edge in directed sense.
Example 5 — List form lookup.
From vertex 1 list [(0, 2), (2, 4)]: edge to 0 weight 2, to 2 weight 4.
Traps
- Confusing “no edge” 0 with “zero weight” edge — problem defines convention.
- Forgetting directed orientation when summing path.
- Double-counting undirected edge in degree/weight tally.
- Picking edge with min label locally vs min total path.
- Vertex label mismatch with matrix index.
Diagnostic (try yourself)
-
Weighted edge from X to Y is 6. In matrix with X=1, Y=2, what is W[1][2]?
-
Path A→B→C with weights 3 and 5. Total weight?
-
Undirected edge weight 4 between vertices 0 and 2. What are W[0][2] and W[2][0]?
-
Adjacency list for vertex 1 is [(2, 7), (3, 1)]. What is weight of edge 1→3?
-
Direct path weight 12 vs two-hop path 4+5. Which route is cheaper?
ChatGPT prep archive
Archived import for extra depth — complements the notes above, not official IITM material.
Core concepts
- Adjacency matrix: n×n for n vertices; entry labels edge or 0/absent.
- Labelled graph: edges carry names/weights/distances—not just on/off.
- Switch representation: same graph as edge list or matrix; convert by filling cells.
- Sparse vs dense: matrix fine small n; list of pairs for few edges.
Notation & vocabulary
| Rep | Best when |
|---|---|
| matrix | dense, fixed n |
| edge list | few edges |
| label | weight/name in cell |
Pattern families
Easy — Fill adjacency matrix
Zero matrix; for each edge (u,v,w) set M[u][v]=w (and M[v][u] if undirected).
Medium — Read label
Shortest direct connection label is cell value. No edge often 0 or infinity per convention—check problem.
Hard — Convert representations
Matrix to list: scan nonzero cells. List to matrix: place labels at indices. Verify vertex count n consistent.
Drill these on the pattern atlas — filter to week 8.
Traps
- Off-by-one vertex numbering (1..n vs 0..n-1).
- Missing reverse edge in undirected fill.
- Label 0 confused with no edge.
- Matrix size not matching vertex count.
Retrieval prompts
- How fill matrix from edge list?
- What is labelled edge?
- When matrix vs edge list?
Practice loop
- Read Deep study (if present) or core concepts once.
- Recite the formula chain without looking.
- Open one easy pattern on the interactive atlas for week 8.
- Attempt without solutions; mark studied after an honest try.
- Say one trap aloud before closing the tab.