diff --git a/Dijkstra's Algorithm b/Dijkstra's Algorithm new file mode 100644 index 0000000..8076121 --- /dev/null +++ b/Dijkstra's Algorithm @@ -0,0 +1,59 @@ +#include +using namespace std; + +typedef pair pii; // (distance, node) + + +void dijkstra(int source, vector>& graph, int V) { + vector dist(V, INT_MAX); + dist[source] = 0; + + priority_queue, greater> pq; + pq.push({0, source}); + + while (!pq.empty()) { + int currentDist = pq.top().first; + int u = pq.top().second; + pq.pop(); + + + if (currentDist > dist[u]) continue; + + + for (auto& edge : graph[u]) { + int v = edge.first; + int weight = edge.second; + + if (dist[u] + weight < dist[v]) { + dist[v] = dist[u] + weight; + pq.push({dist[v], v}); + } + } + } + + cout << "Shortest distances from source " << source << ":\n"; + for (int i = 0; i < V; i++) { + if (dist[i] == INT_MAX) + cout << i << " : INF\n"; + else + cout << i << " : " << dist[i] << "\n"; + } +} + +int main() { + int V = 5; + vector> graph(V); + + + graph[0].push_back({1, 4}); + graph[0].push_back({2, 8}); + graph[1].push_back({2, 3}); + graph[1].push_back({4, 6}); + graph[2].push_back({3, 2}); + graph[3].push_back({4, 10}); + + int source = 0; + dijkstra(source, graph, V); + + return 0; +}