Lecture Notes — Lecture 24: Graphs as Models

Companion to the lecture plan and the worked examples. Read the Definitions before class, the Explanation after it; the practice questions and the lecture quiz close the loop.

Definitions

Explanation

Graphs are the modelling lecture: the algorithm is only as good as the vertex-edge choice. Students are vertices with friendship edges; courses are vertices with prerequisite arrows. Adjacency lists make exploration cheap. Breadth-first search floods outward in rings, which makes it simultaneously a reachability test, a distance measurer (unweighted), and a multi-source propagator when several starting vertices are seeded at day zero. Connected components (friend circles) fall out of repeated floods. Directed graphs add Kahn's peeling: repeatedly take a vertex with no remaining prerequisites; if you get stuck before finishing, the requirements contain a cycle - the algorithm doubles as a contradiction detector. The design questions repeat every time: what is a vertex, what is an edge, directed or not, and what does 'distance' mean here. Answer those and the traversal usually writes itself.

Examples

The lecture's worked examples live in examples.md; the case studies this lecture builds on are worked in class from the projector. The case sequence with difficulties:

Common misconceptions

Summary and key takeaways

  1. Choose vertices and edges first; the traversal follows from the model.
  2. BFS = rings = unweighted distances; seed several sources for propagation problems.
  3. Repeated floods count components; every vertex must get a start chance.
  4. Kahn's peeling schedules prerequisites and detects contradictory cycles.
  5. Directed or not is a modelling decision with algorithmic consequences.

Practice questions

Where this leads

Next lecture: Thinking with Data. The quiz below checks this lecture's essentials before we build on them.