A graph consists of vertices and edges. Directed graphs (digraphs) have ordered edges. A DAG (directed acyclic graph) has no cycles.
Representations:
- Adjacency matrix — if edge exists. space, edge lookup
- Adjacency list — each node stores its neighbors. space, better for sparse graphs
Core traversals:
- BFS (Breadth-First Search) — explore level by level using a queue. . Gives shortest paths in unweighted graphs
- DFS (Depth-First Search) — explore as deep as possible using a stack/recursion. . Used for cycle detection, topological sort
Topological sort — linear ordering of a DAG such that every edge points forward. Found via DFS (reverse post-order). .
Where this appears in ML:
- Computation Graphs are DAGs — backpropagation does a reverse topological sort to compute gradients in the right order
- Graph Neural Networks (GNNs) — message passing on graph structure: each node aggregates features from neighbors
- Knowledge graphs — structured representations for reasoning and retrieval
- Pipeline DAGs — Airflow, Kubeflow, and training pipelines are DAGs of dependent stages
- Tree search — Monte Carlo Tree Search (MCTS) for AlphaGo, reasoning in LLMs (Test-Time Compute)
- Attention as a graph — self-attention is a weighted complete graph over tokens; sparse attention prunes edges
See also: Computation Graphs, Dynamic Programming, Big-O and Complexity Analysis