Quiz 2

570 words
3 min read
Python Week 1: the first filter for runtime behavior
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,v_1, v_2, \ldots or named A, B, C.
  • Edge → connection between two vertices → (u,v)(u, v).
  • Undirected edge{u,v}\{u, v\} — travel both ways.
  • Directed edge(uv)(u \to v) — one-way arrow from uu to vv.
  • 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 nn vertices labeled 0..n10..n-1:
  • MM is n×nn \times n matrix.
  • M[i][j]=1M[i][j] = 1 (or weight) if edge from ii to jj exists; else 00.
  • Undirected graph → matrix symmetric: M[i][j]=M[j][i]M[i][j] = M[j][i].
Mini-example: 3 vertices, edges 0—1 and 1—2 (undirected):
text
M = [0 1 0]
    [1 0 1]
    [0 1 0]
Row ii lists neighbors of vertex ii (out-neighbors if directed).

Matrix indexing

  • M[i][j]M[i][j] → row ii, column jj → 0-based unless problem states 1-based.
  • Row ii → all jj values — outgoing from ii.
  • Column jj → all ii values — incoming to jj (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]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 010 \to 1 only: M[0][1]=1M[0][1]=1, M[1][0]=0M[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]=1M[0][1]=1 and M[1][2]=1M[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 iji \to j does not imply jij \to i.
  • Counting self-loop M[i][i]=1M[i][i]=1 in degree twice wrongly in undirected mental model.
  • Confusing number of vertices with matrix size nn.
  • Path vs edge — path can use multiple edges.

Diagnostic (try yourself)

  1. Graph with vertices {P, Q, R} and edges P—Q, Q—R. What is degree of Q?
  2. Write adjacency matrix (0/1) for single undirected edge between vertices 0 and 1, no other edges (n=2n=2).
  3. In directed graph, M[2][5]=1M[2][5]=1. What does this mean?
  4. How many edges in undirected graph if adjacency matrix has 6 ones above the diagonal?
  5. Vertex with row sum 0 in directed adjacency matrix — what can you conclude?
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.