3.5.2 · HinglishGraphs

Representations — adjacency matrix (space O(V²)), adjacency list (space O(V+E))

1,903 words9 min readRead in English

3.5.2 · Coding › Graphs


HUM KYA represent kar rahe hain?

Woh key fact jo saare space costs control karta hai:

Toh zyada se zyada edges ho sakti hain. Jis graph mein ho use dense kehte hain; jisme ho (ya ) use sparse kehte hain. Yahi ek distinction decide karti hai ki kaun sa representation better rahega.


Representation 1 — Adjacency Matrix

YEH kyun cost karti hai: hum har ordered pair ke liye ek cell allocate karte hain — yeh cells hain — chahe actually kitni bhi edges exist karti hon. Ek empty graph bhi jisme ho woh million cells kha jaata hai.


Representation 2 — Adjacency List

YEH kyun cost karta hai: hum list headers store karte hain (), aur saari lists milake, exactly ek entry per edge-endpoint hoti hai. Har undirected edge do lists mein appear hoti hai, toh total entries hain. Directed: entries. Sum: . Absent edges ke liye kuch store nahi hota — yahi saving hai.


Figure — Representations — adjacency matrix (space O(V²)), adjacency list (space O(V+E))

YEH DONO kaise compare karte hain (80/20 table)

Operation Adjacency Matrix Adjacency List
Space
Edge query isEdge(u,v)?
ke saare neighbors list karo (poori row scan)
Edge add karo
Saari edges iterate karo
Best for dense graphs / fast edge checks sparse graphs (common case)

Worked Examples


Common Mistakes (Steel-manned)


Active Recall

Recall Quick self-test (chhupa ke jawab do)
  1. Matrix ke liye exactly kyun hai chahe edges kam hon?
  2. Undirected list mein nahi entries kyun hain?
  3. Kaun sa representation isEdge(u,v) ko banata hai?
  4. Kis graph type ke liye dono ke space costs equal ho jaate hain?

Jawab: 1) har ordered pair ke liye ek cell, edges irrelevant. 2) edge dono endpoints ki lists mein store hoti hai. 3) matrix. 4) dense, .

Adjacency matrix space complexity
— vertices ke har ordered pair ke liye ek cell
Adjacency list space complexity
headers plus (undirected) edge entries
Matrix kyun hai jab graph mein edges kam hon
Yeh har pair ke liye cell allocate karta hai chahe edge exist kare ya na kare
Ek undirected edge kitni list entries banati hai
Do — yeh dono endpoints ke neighbor lists mein store hoti hai
Matrix mein isEdge(u,v) check karne ka time
— direct index A[u][v]
List mein isEdge(u,v) check karne ka time
— u ki neighbor list scan karni padti hai
Matrix mein u ke saare neighbors list karne ka time
— poori row scan
List mein u ke saare neighbors list karne ka time
Sparse graphs ke liye default representation
Adjacency list
Matrix ki space list ki space se kab tie karti hai
Dense graphs jahan ho
Undirected graph ki adjacency matrix ki property
Yeh symmetric hoti hai,
ka matlab
se tak exactly length ke walks ki count
V vertices wale undirected simple graph mein edges ki max count
Recall Feynman: 12-saal ke bachche ko explain karo

Apni class imagine karo aur socho kaun kaun se dost hain. Method 1 (grid / matrix): ek bada checkerboard banao jisme har bachche ka naam upar aur side mein ho. Agar do log dost hain toh us square mein ✗ lagao. Kisi bhi pair ko check karna super fast hai — lekin un lakhon pairs ke liye bhi square waste hota hai jo dost nahi hain. Badi class = bahut bada grid. Method 2 (lists): har bachche ko ek sticky note do jisme sirf unke doston ke naam hon. Kam doston mein chhota hoga. Amy aur Ben dost hain ya nahi check karna ho toh Amy ki note padho — thoda slow hai, lekin koi space waste nahi. Kam dosti → sticky notes use karo (list). Sabko sabse jaanta ho → grid use karo (matrix).

Connections

  • Breadth-First Search (BFS) mein run karta hai kyunki yeh adjacency lists use karta hai.
  • Depth-First Search (DFS) — wahi traversal bound.
  • Floyd-Warshall Algorithm — adjacency matrix prefer karta hai ( edge weight access).
  • Dijkstra's Algorithm — sparse graphs ke liye adjacency list + priority queue.
  • Big-O Notation — yahan use ki gayi space/time trade-off vocabulary.
  • Sparse vs Dense Graphs — woh property jo choice decide karti hai.
  • Matrix Multiplication — explain karta hai walks kaise count karta hai.

Concept Map

stored as

stored as

dense E~V squared

sparse E~V

space

answers edge query

undirected

A to power k

space

list all neighbors

trade space vs speed

Graph G=V,E

Adjacency Matrix

Adjacency List

edge count 0..O(V squared)

O(V squared) all pairs

is u-v edge in O(1)

symmetric A = A transpose

counts walks length k

O(V+E) only real edges

scan neighbors fast