Tree ek connected acyclic graph hai jisme V − 1 edges hote hain.
RepresentationsRepresentations
Adjacency matrix
Adjacency list
Space
O(V²)
O(V + E)
Check edge (u, v)
O(1)
O(degree of u)
List neighbours of u
O(V)
O(degree of u)
Best for
Dense graphs
Sparse graphs
BFS and DFSBFS aur DFS
BFS
DFS
Uses
Queue
Stack / recursion
Time (adj. list)
O(V + E)
O(V + E)
Time (adj. matrix)
O(V²)
O(V²)
Finds
Shortest path in unweighted graphs
Cycles, topological order, components
TipIn BFS of an undirected graph there are no forward edges; in DFS of an undirected graph there are no cross edges.Undirected graph ke BFS mein forward edges nahi hote; undirected graph ke DFS mein cross edges nahi hote.
HashingHashing
A hash function maps a key to a slot in a table of size m, giving average O(1) search, insert and delete. Collisions (two keys in one slot) are handled by chaining or open addressing.Hash function ek key ko size m ki table ke ek slot pe map karta hai, jisse search, insert aur delete average O(1) mein hote hain. Collisions (do keys ek hi slot mein) ko chaining ya open addressing se handle karte hain.
Division method: h(k) = k mod m (choose m prime, not close to a power of 2)
Load factor: α = n / m
Common mistakeCommon galtiIn open addressing α must be < 1 (the table can fill up); in chaining α can be greater than 1.Open addressing mein α < 1 hona chahiye (table bhar sakti hai); chaining mein α 1 se zyada bhi ho sakta hai.