Graph ek advanced non-linear data structure hai jo real-world relationships ko represent karta hai. Agar aap DSA roadmap for beginners follow kar rahe hain, to Graph samajhna bahut important hai kyunki ye complex problems jaise social networks, maps aur routing systems me use hota hai.
Tree ek special type ka graph hi hota hai, lekin graph me cycles aur multiple connections ho sakte hain. Isi wajah se Graph algorithms interviews aur competitive programming me frequently pooche jate hain.
Graph ek collection hota hai vertices (nodes) aur edges (connections) ka.
👉 Important terms:
Vertex (Node) – data point
Edge – connection between nodes
Degree – node ke connections ki sankhya
Edges dono direction me hote hain
Edges ek direction me hote hain
Edges ke saath weight (cost) hota hai
Edges ka koi weight nahi hota
2D array me graph store hota hai
Linked list ka use hota hai
👉 Adjacency list zyada efficient hoti hai
#include <iostream>
#include <vector>
using namespace std;
vector<int> graph[100];
// Add edge
void addEdge(int u, int v) {
graph[u].push_back(v);
graph[v].push_back(u); // undirected graph
}
// Display graph
void display(int n) {
for (int i = 0; i < n; i++) {
cout << i << " -> ";
for (int x : graph[i]) {
cout << x << " ";
}
cout << endl;
}
}
int main() {
int n = 5;
addEdge(0,1);
addEdge(0,2);
addEdge(1,3);
addEdge(2,4);
display(n);
return 0;
}
#include <queue>
#include <vector>
using namespace std;
vector<int> adj[100];
bool visited[100];
void bfs(int start) {
queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
visited[neighbor] = true;
q.push(neighbor);
}
}
}
}
#include <vector>
using namespace std;
vector<int> adj[100];
bool visited[100];
void dfs(int node) {
visited[node] = true;
cout << node << " ";
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
dfs(neighbor);
}
}
}
👉 Ye simple graph representation hai
Graph traversal ka matlab nodes ko visit karna hai:
Level by level traversal
Queue ka use hota hai
Depth me traversal
Stack ya recursion use hota hai
Graph traversal me agar BFS use karein:
👉 Starting node se sabse pehle uske neighbours visit honge
DFS me:
👉 Ek path ko end tak follow kiya jata hai
Algorithm Complexity
BFS O(V + E)
DFS O(V + E)
👉 Yaha V = vertices, E = edges
Google Maps (shortest path)
Social networks (friends connections)
Web crawling
Network routing
AI & machine learning
Complex relationships represent karta hai
Flexible structure
Real-world modeling
Implementation complex
Memory usage zyada
Visualization difficult
Feature Graph Tree
Cycle Allowed Not allowed
Structure Complex Hierarchical
Root Optional Mandatory
Graph representation galat choose karna
BFS aur DFS confuse karna
Visited nodes track na karna
👉 Tip: Diagram bana ke samjho
Basic concepts samjho
BFS aur DFS practice karo
Shortest path algorithms (Dijkstra) seekho
Problems solve karo (20+)
👉 Ye sab topics milkar aapki DSA strong banate hain
Graph ek powerful aur advanced data structure hai jo complex problems ko solve karne me use hota hai. Agar aap DSA roadmap follow kar rahe hain to Graph aapka final aur sabse important step hai.
Practice aur consistency ke saath aap Graph ko master kar sakte hain aur coding interviews me strong performance de sakte hain.
Read Next : Linked List