BFS choice: BFS repeatedly asks "who are u's neighbours?" and visits each vertex/edge once. In a list that whole sweep costs ∑udeg(u)=2E, so BFS is O(V+E). In a matrix each neighbour scan is O(V), forcing O(V2). → Use the adjacency list. Same argument holds for Depth-First Search (DFS). ✔
WHAT we do: set the two costs equal and solve for E.
2E=V2⇒E=2V2.
But the maximum possible E for a simple undirected graph is (2V)=2V(V−1)≈2V2.
So the list's raw entry count only reaches the matrix's cell count at (essentially) maximum density — a complete graph. For everything sparser, 2E<V2 and the list stores fewer things. In Big-O terms both are O(V2) once E=Θ(V2), so their space asymptotics tie for dense graphs, and the matrix's O(1) edge check becomes the tie-breaker. ✔
WHY matrix powers:(A2)[u][v]=∑mA[u][m]A[m][v] counts every middle vertex m neighbouring both u and v — i.e. every 2-step route.
Compute A2:
A2=211121112
(a) (A2)[0][0]=2: routes 0→1→0 and 0→2→0 → 2 walks.
(b) (A2)[0][1]=1: only route 0→2→1 (you can't step 0→1→1 since no self-loop) → 1 walk. ✔
(a) Matrix. Floyd–Warshall indexes dist[i][j] inside triple loops and needs O(1) edge weight lookups; V2=2.5×105 cells is cheap. Its own runtime is O(V3) so the matrix fits perfectly.
(b) List.E≪V2 (very sparse); Dijkstra sweeps each vertex's neighbours, total O(ElogV) with a heap — a matrix would force O(V2) neighbour scans, a disaster at V=106.
(c) List. DFS visits each vertex and edge once → O(V+E) with a list, versus O(V2) scanning matrix rows. ✔
WHAT / WHY: expand the matrix-product definition and use the meaning of A's entries.
(A2)[u][u]=∑m=0V−1A[u][m]A[m][u].
Each term A[u][m]A[m][u] equals 1exactly whenm is a neighbour of u (then A[u][m]=A[m][u]=1 by symmetry), and 0 otherwise. Summing 1 once per neighbour of u gives the number of neighbours:
(A2)[u][u]=#{m:A[u][m]=1}=deg(u).■
Sanity check against L4.1: triangle vertex 0 has deg(0)=2, and indeed (A2)[0][0]=2. ✔
(a) Matrix needs V2=60002=3.6×107 cells. Since 3.6×107≤8×107, the matrix fits. ✔
(b) List budget: V+2E≤8×107⇒2E≤8×107−6000=79,994,000⇒E≤39,997,000. So a list could hold up to about 4.0×107 edges — but a simple graph on 6000 vertices has at most (26000)=26000⋅5999=17,997,000 edges, so the real cap is E≤17,997,000.
(c) Rule: if 2E<V2, the list stores fewer things (sparse → list); if E is near (2V) (dense) and you need fast edge queries, take the matrix. Here V is small enough that the matrix comfortably fits, so if you need O(1)isEdge the matrix is fine. ✔
For undirected {u,v}, how many matrix cells and how many list entries does it create?
Why is BFS O(V+E) with a list but O(V2) with a matrix?
What is (A2)[u][u] for a simple undirected graph?
At what E do 2E and V2 meet, and what graph is that?
Answers: 1) two cells (A[u][v],A[v][u]) and two list entries. 2) list visits ∑deg=2E neighbours total; matrix scans V columns per vertex. 3) deg(u). 4) E=V2/2≈(2V) — a complete graph.
Cells created by an undirected edge in the matrix
Two — A[u][v] and A[v][u]
(A2)[u][u] for a simple undirected graph
deg(u)
Space of a V=6000 adjacency matrix
3.6×107 cells
Max edges in a simple undirected graph on 6000 vertices