10324012122331440123401234u╲v00101000001000000100010001000001adj = [[] for _ in range(V)]2mat = [[0] * V for _ in range(V)]3for u, v in edges:4 adj[u].append(v)5 mat[u][v] = 164 in adj[3]7mat[3][4] == 1listmatrix
List slots and matrix 1s: one per directed edgeMatrix 0 tiles: a cell for every pair, edge or notThe probe: one read of a slot or a cellOne directed graph with 5 vertices and 6 edges, stored two ways from the same edge list: an adjacency list (a list of neighbours per vertex, V + E slots) and an adjacency matrix (a V × V grid, 1 where an edge exists). The yellow probe visits one slot or cell per read: an edge check costs 1 read in the matrix and up to deg(u) in the list; listing neighbours costs deg(u) against V; visiting every edge costs V + E against V².

Two ways to store it

  1. A directed graph: 5 vertices numbered 0 to 4 and 6 one-way edges, such as 3 → 4. Python receives it as an edge list of pairs. Algorithms need a shape they can ask questions of quickly: a list per vertex, or a grid.

Asking questions

Choosing

What happens

;; step 1/9 · the-graph
edges = [(0, 1), (0, 2), (1, 2), (2, 3), (3, 1), (3, 4)]
V = 5, E = 6

out-degree deg(u) = edges leaving u:
  deg(0) = 2
  deg(1) = 1
  deg(2) = 1
  deg(3) = 2
  deg(4) = 0
sum of out-degrees = 6 = E

The figure counts slots and cells, not bytes. A Python list of lists also stores pointers and spare room for appends, but the growth stays the same: Θ(V + E) for lists and Θ(V²) for the matrix. In an undirected graph every edge is stored twice, as v in adj[u] and u in adj[v], so the lists hold V + 2E slots, and the matrix is symmetric about its diagonal.