1: Graphs and Representations

A graph G=(V,E)G = (V, E) consists of vertices VV and edges EE. 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 ListAdjacency Matrix
SpaceO(V+E)O(V + E)O(V2)O(V^2)
Check edge (u,v)(u,v)O(deg(u))O(\deg(u))O(1)O(1)
Iterate neighborsO(deg(u))O(\deg(u))O(V)O(V)

Use adjacency lists for sparse graphs (EV2E \ll V^2) and when iterating neighbors (most algorithms).

Use adjacency matrix for dense graphs or when checking specific edges repeatedly.


Runtime Pattern

Most graph algorithms are O(V+E)O(V + E): visit each vertex once, examine each edge once. This is optimal—you can’t do better than looking at the entire input.