Daa Ex-8 Mine
Daa Ex-8 Mine
Daa Ex-8 Mine
Experiment 8
3. Algorithm:
Step1:- Initialize:
Create a distance array dist[] and set all values to infinity (∞) except for
the source node, which is set to 0.
Use a priority queue (min-heap) to store nodes with their current known
shortest distance.
Insert the source node into the priority queue with a distance of 0.
Extract the node u with the minimum distance from the priority queue.
For each neighboring node v of u, calculate the tentative distance to v
through u.
If this distance is smaller than the current distance in dist[v], update
dist[v] and insert v into the priority queue with the new distance.
4. Implementation/Code:
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
while (!pq.empty()) {
int u = pq.top().second; // Get the vertex with the smallest distance
pq.pop();
// Relaxation step
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v}); // Push updated distance to the priority queue
}
}
}
return 0;
}
5. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Time Complexity:
Using Binary Heap: O((V + E) log V), where V is the number of vertices and E
is the number of edges.
Using Fibonacci Heap: O(E + V log V).
Space Complexity:
The overall space complexity of Dijkstra’s algorithm is O(V + E), where V is
the number of vertices and E is the number of edges.
7. Learning Outcomes:
1. Understanding of Dijkstra's Algorithm: You will learn how to implement
and apply Dijkstra’s algorithm to find the shortest paths in graphs with
positive edge weights, utilizing a priority queue for efficiency.
2. Time and Space Complexity Analysis: You will gain insights into
analyzing the time complexity (O((V + E) log V) using a binary heap) and
space complexity (O(V + E)) of the algorithm.
3. Graph Representation: You will understand how to represent graphs
efficiently using adjacency lists and handle shortest path problems with real-
world applications.