Professional Documents
Culture Documents
DAA Exp - 3.2
DAA Exp - 3.2
DAA Exp - 3.2
Experiment-3.2
1. Aim: Develop a program and analyze complexity to find shortest paths in a graph with
positive edge weights using Dijkstra’s algorithm.
2. Algorithms:
Step 1: Initialize an array 'dist' of size V (number of vertices) to store the shortest distances. Set
all distances to infinity except for the source vertex, which is set to 0.
Step 2: Initialize a priority queue (min-heap) to store pairs (distance, vertex) and push (0, source)
into it. This represents the starting vertex with a distance of 0.
Step 4: After processing all vertices or when the priority queue is empty, 'dist' contains the
shortest distances from the source vertex to all other vertices.
End of Algorithm.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. Code:
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
pq.push({0, start});
while (!pq.empty()) {
int u = pq.top().second;
int u_dist = pq.top().first;
pq.pop();
return dist;
}
int main() {
int V = 6; // Number of vertices
vector<vector<Edge>> graph(V);
cout << "Shortest distances from vertex " << start << " to all other vertices:" << endl;
for (int i = 0; i < V; i++) {
cout << "Vertex " << i << ": " << shortestDistances[i] << endl;
}
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. Output: