A graph is a collection of:
Example:
1 ----- 2
| |
| |
3 ----- 4Nodes = {1,2,3,4}
Edges = {(1,2),(1,3),(2,4),(3,4)}
1 ----- 21 connected to 2 and 2 connected to 1.
1 -----> 2Only 1 can reach 2.
1 --5--> 2Edge weight = 5
This is the easiest way to store a graph.
Suppose:
1 ----- 2
| |
| |
3 ----- 4We create an n × n matrix.
vector<vector<int>> graph(5, vector<int>(5,0));For edge (u,v):
graph[u][v] = 1;
graph[v][u] = 1;Matrix becomes:
1 2 3 4
1 0 1 1 0
2 1 0 0 1
3 1 0 0 1
4 0 1 1 0int n,m;
cin>>n>>m;
vector<vector<int>> adj(n+1, vector<int>(n+1,0));
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
adj[u][v]=1;
adj[v][u]=1;
}Easy to understand.
Checking edge existence:
adj[u][v]Time:
O(1)For n=10000
Need:
10000 × 10000memory.
Space:
O(V²)Huge waste.
Most interview problems use this.
Instead of storing all possibilities, store only neighbors.
Graph:
1 ----- 2
| |
| |
3 ----- 4Representation:
1 -> 2,3
2 -> 1,4
3 -> 1,4
4 -> 2,3int n,m;
cin>>n>>m;
vector<vector<int>> adj(n+1);
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}adj[1] = {2,3}
adj[2] = {1,4}
adj[3] = {1,4}
adj[4] = {2,3}Space:
O(V + E)Much better than matrix.
Most graph interview problems use adjacency lists.
| Feature | Matrix | Adj List |
|---|---|---|
| Space | O(V²) | O(V+E) |
| Edge Check | O(1) | O(degree) |
| Traversal | O(V²) | O(V+E) |
| Interview Usage | Rare | Very Common |
BFS explores level by level.
Uses:
QueueExample:
1
/ \
2 3
/ \
4 5Traversal:
1
2 3
4 5Result:
1 2 3 4 5vector<int> bfs(int V, vector<vector<int>>& adj){
vector<int> ans;
vector<int> vis(V,0);
queue<int> q;
q.push(0);
vis[0]=1;
while(!q.empty()){
int node=q.front();
q.pop();
ans.push_back(node);
for(auto neigh:adj[node]){
if(!vis[neigh]){
vis[neigh]=1;
q.push(neigh);
}
}
}
return ans;
}Time:
O(V+E)void bfsMatrix(vector<vector<int>>& graph){
int n=graph.size();
vector<int> vis(n,0);
queue<int> q;
q.push(0);
vis[0]=1;
while(!q.empty()){
int node=q.front();
q.pop();
cout<<node<<" ";
for(int neigh=0;neigh<n;neigh++){
if(graph[node][neigh]==1 &&
!vis[neigh]){
vis[neigh]=1;
q.push(neigh);
}
}
}
}Time:
O(V²)DFS explores one path completely.
Uses:
Recursion
or
StackExample:
1
|
2
|
4
|
5Go deep first.
Result:
1 2 4 5 ...void dfs(int node,
vector<vector<int>>& adj,
vector<int>& vis){
vis[node]=1;
cout<<node<<" ";
for(auto neigh:adj[node]){
if(!vis[neigh]){
dfs(neigh,adj,vis);
}
}
}void dfsMatrix(int node,
vector<vector<int>>& graph,
vector<int>& vis){
vis[node]=1;
cout<<node<<" ";
int n=graph.size();
for(int neigh=0;neigh<n;neigh++){
if(graph[node][neigh]==1 &&
!vis[neigh]){
dfsMatrix(neigh,graph,vis);
}
}
}Recursion internally uses stack.
We can do it manually.
vector<int> dfsStack(int V,
vector<vector<int>>& adj){
vector<int> ans;
vector<int> vis(V,0);
stack<int> st;
st.push(0);
while(!st.empty()){
int node=st.top();
st.pop();
if(vis[node])
continue;
vis[node]=1;
ans.push_back(node);
for(auto neigh:adj[node]){
if(!vis[neigh]){
st.push(neigh);
}
}
}
return ans;
}Most interview questions are one of these:
"Find connected components"
Use:
DFS/BFSExamples:
Number of Provinces
Number of Islands
Flood Fill
Connected Components
LINK : []
"Find shortest distance in unweighted graph"
Use:
BFSExamples:
"Spread process level by level"
Use:
Multi Source BFSExamples:
"Traverse all cells"
Treat matrix as graph.
Use:
DFS/BFSExamples:
Idea:
Each city = node
If city not visited:
DFS/BFS
count++Connected Components problem.
Idea:
Starting pixel.
Visit all connected pixels having same color.
Use:
DFS
or
BFSMatrix becomes graph.
Idea:
All rotten oranges start simultaneously.
Use:
Multi Source BFSPush all rotten oranges into queue first.
Idea:
Distance from nearest zero.
Use:
Multi Source BFSPush every 0 into queue.
Idea:
Each word = node.
One character change = edge.
Need shortest transformation.
Use:
BFSBecause BFS gives shortest path in unweighted graph.
Idea:
Grid graph.
Every land cell = node.
Find connected components.
Use:
DFS/BFSIdea:
Start DFS from boundary O's.
Mark safe cells.
Convert remaining O → X.
Idea:
Directed Graph.
Use:
Topological SortIdea:
Graph traversal + mapping.
Use:
DFS/BFSIdea:
Weighted Graph.
Use:
Dijkstra✅ Graph Representation
✅ BFS
✅ DFS
✅ Number of Provinces
✅ Number of Islands
✅ Flood Fill
✅ Rotting Oranges
✅ 01 Matrix
✅ Word Ladder
✅ Surrounded Regions
✅ Bipartite Graph
✅ Topological Sort
✅ Course Schedule
✅ Dijkstra
✅ Bellman Ford
✅ Floyd Warshall
✅ Disjoint Set Union (DSU)
Graph Representation
|
+---- Matrix
|
+---- Adjacency List
Traversal
|
+---- BFS (Queue)
|
+---- DFS (Recursion)
|
+---- DFS (Stack)
Connected Components
|
+---- Number of Provinces
+---- Number of Islands
Multi Source BFS
|
+---- Rotting Oranges
+---- 01 Matrix
Shortest Path
|
+---- BFS (Unweighted)
+---- Dijkstra (Weighted)
Directed Graph
|
+---- Topological Sort
+---- Course ScheduleIf you're preparing for interviews, the strongest order is:
Graph Representation → BFS → DFS → Number of Provinces → Flood Fill → Number of Islands → Rotting Oranges → 01 Matrix → Word Ladder → Topological Sort → Dijkstra.
This sequence covers about 70–80% of graph questions commonly asked in coding interviews.