|
| 1 | +/** |
| 2 | + * Dijkstras_MinHeap.cpp |
| 3 | + * |
| 4 | + * This file implements Dijkstra's algorithm using a min-heap (priority queue). |
| 5 | + * The algorithm finds the shortest paths from the source vertex to all other vertices in a weighted graph. |
| 6 | + * |
| 7 | + * Functions: |
| 8 | + * - void dijkstra(const unordered_map<int, unordered_map<int, int>>& graph, int start_vertex) |
| 9 | + * - graph: An adjacency list representation of the graph. |
| 10 | + * - key: vertex |
| 11 | + * - value: unordered_map of connected vertices and their edge weights |
| 12 | + * - start_vertex: The starting vertex for Dijkstra's algorithm. |
| 13 | + * |
| 14 | + * Example Usage: |
| 15 | + * Uncomment the main function to run a sample test case. |
| 16 | + * The sample graph used in the main function is represented as an adjacency list. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <iostream> |
| 20 | +#include <vector> |
| 21 | +#include <queue> |
| 22 | +#include <unordered_map> |
| 23 | +#include <limits> |
| 24 | + |
| 25 | +using namespace std; |
| 26 | + |
| 27 | +// A structure to represent a node in the priority queue |
| 28 | +struct Node { |
| 29 | + int vertex; |
| 30 | + int distance; |
| 31 | + bool operator>(const Node& other) const { |
| 32 | + return distance > other.distance; |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +void dijkstra(const unordered_map<int, unordered_map<int, int>>& graph, int start_vertex) { |
| 37 | + // Initialize distances and predecessors |
| 38 | + unordered_map<int, int> dist; |
| 39 | + unordered_map<int, int> pred; |
| 40 | + for (const auto& pair : graph) { |
| 41 | + dist[pair.first] = numeric_limits<int>::max(); |
| 42 | + pred[pair.first] = -1; |
| 43 | + } |
| 44 | + dist[start_vertex] = 0; |
| 45 | + |
| 46 | + // Priority queue to store vertices and their distances |
| 47 | + priority_queue<Node, vector<Node>, greater<Node>> priority_queue; |
| 48 | + priority_queue.push({ start_vertex, 0 }); |
| 49 | + |
| 50 | + while (!priority_queue.empty()) { |
| 51 | + Node current = priority_queue.top(); |
| 52 | + priority_queue.pop(); |
| 53 | + |
| 54 | + // If this distance is not updated, continue |
| 55 | + if (current.distance > dist[current.vertex]) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + // Visit each neighbor of the current vertex |
| 60 | + for (const auto& neighbor_pair : graph.at(current.vertex)) { |
| 61 | + int neighbor = neighbor_pair.first; |
| 62 | + int weight = neighbor_pair.second; |
| 63 | + int distance = current.distance + weight; |
| 64 | + |
| 65 | + // If a shorter path to the neighbor is found |
| 66 | + if (distance < dist[neighbor]) { |
| 67 | + dist[neighbor] = distance; |
| 68 | + pred[neighbor] = current.vertex; |
| 69 | + priority_queue.push({ neighbor, distance }); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Print distances and predecessors |
| 75 | + cout << "Distances: \n"; |
| 76 | + for (const auto& pair : dist) { |
| 77 | + cout << "Vertex " << pair.first << ": " << pair.second << endl; |
| 78 | + } |
| 79 | + cout << "\nPredecessors: \n"; |
| 80 | + for (const auto& pair : pred) { |
| 81 | + cout << "Vertex " << pair.first << ": " << pair.second << endl; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Uncomment the following main function to run a sample test case |
| 86 | +/* |
| 87 | +int main() { |
| 88 | + // Example graph represented as an adjacency list |
| 89 | + unordered_map<int, unordered_map<int, int>> graph = { |
| 90 | + {0, {{1, 1}, {2, 4}}}, |
| 91 | + {1, {{0, 1}, {2, 2}, {3, 5}}}, |
| 92 | + {2, {{0, 4}, {1, 2}, {3, 1}}}, |
| 93 | + {3, {{1, 5}, {2, 1}}} |
| 94 | + }; |
| 95 | +
|
| 96 | + // Running Dijkstra's algorithm from vertex 0 |
| 97 | + dijkstra(graph, 0); |
| 98 | +
|
| 99 | + return 0; |
| 100 | +} |
| 101 | +*/ |
0 commit comments