1: Graphs and Representations
A graph consists of vertices and edges . Edges can be directed (one-way) or undirected (two-way), and weighted or unweighted.
Terminology
- Path: Sequence of edges connecting two vertices
- Cycle: Path that starts and ends at the same vertex
- Connected: Path exists between every pair (undirected)
- Strongly connected: Directed path exists in both directions between every pair
- DAG: Directed Acyclic Graph
Adjacency List Representation
Vertex:
- key: Key
- in_edges: List[Edge]
- out_edges: List[Edge]
- val: Object
Edge:
- key: Key
- from: Vertex
- to: Vertex
- wgt: Integer
Graph:
- vertices: Dictionary[Key, Vertex]Each vertex maintains lists of incoming and outgoing edges. For undirected graphs, each edge appears in both directions.
Adjacency List vs Matrix
| Adjacency List | Adjacency Matrix | |
|---|---|---|
| Space | ||
| Check edge | ||
| Iterate neighbors |
Use adjacency lists for sparse graphs () and when iterating neighbors (most algorithms).
Use adjacency matrix for dense graphs or when checking specific edges repeatedly.
Runtime Pattern
Most graph algorithms are : visit each vertex once, examine each edge once. This is optimal—you can’t do better than looking at the entire input.