The key fact controlling all space costs:
0≤E≤(2V)=2V(V−1)=O(V2)(undirected, no self-loops)
So at most there are ∼V2/2 edges. A graph where E≈V2 is called dense; one where E≈V (or E≪V2) is called sparse. This single distinction decides which representation wins.
WHY it costs O(V2): we allocate one cell for every ordered pair(u,v) — that's V×V=V2 cells — regardless of how many edges actually exist. Even an empty graph with V=1000 eats a million cells.
WHY it costs O(V+E): we store the V list headers (+V) plus, across all lists, exactly one entry per edge-endpoint. Each undirected edge appears in two lists, so total entries =2E=O(E). Directed: E entries. Sum: O(V+E). Nothing is stored for absent edges — that's the saving.
Why exactly O(V2) for the matrix even with few edges?
Why 2E and not E entries for an undirected list?
Which representation makes isEdge(u,v)O(1)?
For which graph type do their space costs become equal?
Answers: 1) one cell per ordered pair, edges irrelevant. 2) edge stored in both endpoints' lists. 3) matrix. 4) dense, E=Θ(V2).
Adjacency matrix space complexity
O(V2) — one cell per ordered pair of vertices
Adjacency list space complexity
O(V+E) — V headers plus 2E (undirected) edge entries
Why is matrix O(V2) even when the graph has few edges
It allocates a cell for every pair (u,v) regardless of whether the edge exists
How many list entries does one undirected edge create
Two — it is stored in both endpoints' neighbor lists
Time to check isEdge(u,v) in a matrix
O(1) — direct index A[u][v]
Time to check isEdge(u,v) in a list
O(degu) — must scan u's neighbor list
Time to list all neighbors of u in a matrix
O(V) — scan the entire row
Time to list all neighbors of u in a list
O(degu)
Default representation for sparse graphs
Adjacency list
When does the matrix's space tie with the list's
Dense graphs where E=Θ(V2)
Property of an adjacency matrix for an undirected graph
It is symmetric, A=AT
Meaning of (Ak)[u][v]
Number of walks of length exactly k from u to v
Max number of edges in an undirected simple graph on V vertices
(2V)=2V(V−1)
Recall Feynman: explain to a 12-year-old
Imagine your class and who is friends with whom.
Method 1 (grid / matrix): draw a big checkerboard with every kid's name on the top and side. Put an ✗ in a square if those two are friends. Super fast to check any pair — but you waste a square even for the millions of pairs who aren't friends. Big class = giant grid.
Method 2 (lists): give each kid a sticky note listing only their friends. Tiny if friendships are few. To check if Amy and Ben are friends you read Amy's note — a little slower, but no wasted space.
Few friendships → use sticky notes (list). Everyone-knows-everyone → use the grid (matrix).
Dekho, graph ka matlab hai "kaun kis se connected hai." Ab computer me ye connection store karne ke do main tareeke hain. Pehla hai adjacency matrix — ek V×V ka grid bana lo, har cell A[u][v] me 1 likho agar edge hai, warna 0. Problem ye hai ki chahe edges 4 ho ya 0, tum har pair ke liye ek box reserve karte ho, isliye space O(V2) ho jaata hai. Plus point: "kya u aur v ke beech edge hai?" ye sawaal O(1) me answer ho jaata hai — bas index karo.
Doosra tareeka hai adjacency list — har vertex ke liye sirf uske actual neighbors ki ek list rakho. Yahan tum absent edges ke liye kuch store hi nahi karte, isliye space sirf O(V+E) — V headers plus har undirected edge ke do entries (2E). Ye real-world graphs ke liye perfect hai kyunki zyadatar graphs sparse hote hain (edges kam, vertices zyada). Bas thoda dhyaan: edge check karne ke liye list me O(degu) time lagta hai, instant nahi.
Yaad rakhne ka simple rule: agar graph dense hai (sabhi sabse connected, jaise E≈V2) ya V chhota hai ya tumhe baar-baar fast edge check chahiye (jaise Floyd-Warshall me), to matrix lo. Warna by default list hi best hai — BFS/DFS bhi list ke saath O(V+E) me chalte hain, jo bahut fast hai. Bas ye trade-off — space vs edge-query speed — pura topic isi pe ghoomta hai.