๐ŸŒ‰ 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.

๐Ÿ”— Next Topics