• Dijkstra's Algorithm (for shortest path in weighted graphs):

    • Finds the shortest path from a source to all other vertices in a weighted graph.
    • Time complexity: \(O(V \log V + E)\) using priority queue.
    void dijkstra(int source, vector<vector<pair<int, int>>> &adj, int V) {
        vector<int> dist(V, INT_MAX);
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        dist[source] = 0;
        pq.push({0, source});
    
        while (!pq.empty()) {
            int u = pq.top().second;
            pq.pop();
    
            for (auto &neighbor : adj[u]) {
                int v = neighbor.first;
                int weight = neighbor.second;
    
                if (dist[u] + weight < dist[v]) {
                    dist[v] = dist[u] + weight;
                    pq.push({dist[v], v});
                }
            }
        }
    }