570 words
3 min read
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
# Computational Thinking · Week 7 — Graphs and matrices Deep study for Quiz 2 week 7. Graphs model connections between vertices; matrices store grid or adjacency data in rows and columns.

Computational Thinking · Week 7 — Graphs and matrices
Deep study for Quiz 2 week 7. Graphs model connections between vertices; matrices store grid or adjacency data in rows and columns.
Week map
Vertex and edge → directed vs undirected → adjacency matrix → matrix indexing → paths and degrees.
Graph notation
- Vertex (node) → entity → labeled v1,v2,… or named A, B, C.
- Edge → connection between two vertices → (u,v).
- Undirected edge → {u,v} — travel both ways.
- Directed edge → (u→v) — one-way arrow from u to v.
- Degree of vertex → number of edges incident → in directed graph: in-degree and out-degree separately.
Mini-example: vertices {A, B, C}, edges A—B, B—C. A has degree 1, B has degree 2, C has degree 1.
Path
- Path → sequence of vertices where consecutive pairs are edges.
- Simple path → no repeated vertices.
Path A→B→C exists above; A→C does not (no direct edge).
Adjacency matrix notation
For n vertices labeled 0..n−1:
- M is n×n matrix.
- M[i][j]=1 (or weight) if edge from i to j exists; else 0.
- Undirected graph → matrix symmetric: M[i][j]=M[j][i].
Mini-example: 3 vertices, edges 0—1 and 1—2 (undirected):
textM = [0 1 0] [1 0 1] [0 1 0]
Row i lists neighbors of vertex i (out-neighbors if directed).
Matrix indexing
- M[i][j] → row i, column j → 0-based unless problem states 1-based.
- Row i → all j values — outgoing from i.
- Column j → all i values — incoming to j (directed).
Pattern families
Easy — Read graph from diagram
Count vertices and edges. List neighbors of one vertex. Identify directed vs undirected.
Medium — Build or read adjacency matrix
Fill matrix from edge list. Read M[i][j] for edge existence. Check symmetry for undirected.
Hard — Degree and path reasoning
Compute degree from matrix row sums. Determine if path exists of length 2 (via intermediate vertex). Spot isolated vertex (all zeros in row and column).
Worked mini-examples
Example 1 — Neighbors.
Triangle on {0,1,2} all connected. Each vertex degree 2.
Example 2 — Directed matrix.
Edge 0→1 only: M[0][1]=1, M[1][0]=0. Not symmetric.
Example 3 — Row sum = out-degree.
Row [0, 1, 1, 0] sums to 2 — vertex has two outgoing edges (directed).
Example 4 — Path length 2.
M[0][1]=1 and M[1][2]=1 implies path 0→1→2 exists (length 2).
Example 5 — Isolated vertex.
Row and column all zero — no edges incident.
Traps
- 0-based vs 1-based vertex labels in matrix.
- Directed edge i→j does not imply j→i.
- Counting self-loop M[i][i]=1 in degree twice wrongly in undirected mental model.
- Confusing number of vertices with matrix size n.
- Path vs edge — path can use multiple edges.
Diagnostic (try yourself)
-
Graph with vertices {P, Q, R} and edges P—Q, Q—R. What is degree of Q?
-
Write adjacency matrix (0/1) for single undirected edge between vertices 0 and 1, no other edges (n=2).
-
In directed graph, M[2][5]=1. What does this mean?
-
How many edges in undirected graph if adjacency matrix has 6 ones above the diagonal?
-
Vertex with row sum 0 in directed adjacency matrix — what can you conclude?