๐ Graphs โ Comprehensive Study Guide
A **Graph** is a **collection of nodes (vertices) and edges** that connect them. Graphs are widely used in **networking, AI, and real-world applications**.
โก Why are Graphs Important?
- โ **Used in shortest path finding (Google Maps, GPS, AI).**
- ๐ก **Essential for social networks & web crawling.**
- ๐ **Applied in networking, game development & robotics.**
- ๐ **Forms the foundation of AI-based recommendation systems.**
๐ Types of Graphs
๐ฅ **1. Directed vs Undirected Graphs**
- ๐ **Directed Graph (Digraph)** โ Edges have a direction (e.g., social media followers).
- ๐ **Undirected Graph** โ Edges are bidirectional (e.g., Facebook friends).
๐ค **2. Weighted vs Unweighted Graphs**
- ๐ **Weighted Graph** โ Edges have weights (e.g., distance in Google Maps).
- ๐ **Unweighted Graph** โ Edges have no weight (e.g., friendships in social networks).
๐ ๏ธ Graph Representation
๐ **1. Adjacency Matrix (O(Vยฒ) Space Complexity)**
int graph[V][V]; // If graph[i][j] = 1, there is an edge from i to j
๐ **2. Adjacency List (O(V + E) Space Complexity, More Efficient)**
vector adj[V]; // Stores neighbors for each vertex
๐ Graph Traversal Techniques
๐ **1. Breadth-First Search (BFS) (O(V + E))**
Explores neighbors before moving deeper into the graph.
void BFS(vector>& adj, int start) {
queue q;
vector visited(adj.size(), false);
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);
}
}
}
}
๐ **2. Depth-First Search (DFS) (O(V + E))**
Explores as deep as possible before backtracking.
void DFS(vector>& adj, int node, vector& visited) {
visited[node] = true;
cout << node << " ";
for (int neighbor : adj[node]) {
if (!visited[neighbor]) {
DFS(adj, neighbor, visited);
}
}
}
๐ Shortest Path Algorithms
๐ **1. Dijkstraโs Algorithm (O(V + E log V))**
void dijkstra(vector>>& graph, int src) {
priority_queue, vector>, greater>> pq;
vector dist(graph.size(), INT_MAX);
pq.push({0, src});
dist[src] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
for (auto edge : graph[u]) {
int v = edge.first, weight = edge.second;
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v});
}
}
}
}
๐ **2. Bellman-Ford Algorithm (O(VE))**
void bellmanFord(vector>& edges, int V, int src) {
vector dist(V, INT_MAX);
dist[src] = 0;
for (int i = 1; i < V; i++) {
for (auto edge : edges) {
int u = edge[0], v = edge[1], weight = edge[2];
if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
}
}
}
}
๐ **3. Floyd-Warshall Algorithm (O(Vยณ))**
void floydWarshall(vector>& dist, int V) {
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
}
โณ Time Complexity Analysis
Algorithm | Time Complexity |
---|---|
BFS | O(V + E) |
DFS | O(V + E) |
Dijkstra | O(V + E log V) |
Bellman-Ford | O(VE) |
Floyd-Warshall | O(Vยณ) |
๐ Real-World Applications of Graphs
- ๐ก **Google Maps & GPS Navigation** โ Shortest route finding.
- ๐ **Social Networks** โ Analyzing connections between users.
- ๐ค **AI & Robotics** โ Pathfinding in automated systems.
- ๐ฎ **Game Development** โ AI-based movement & level design.