Experiment 8
Student Name: Akshat Shukla UID: 22BCS12484
Branch: CSE Section/Group: 801 - A
Semester:5 Date of Performance: 08/10/24
Subject Name: DAA Lab Subject Code: 22CSH-311
1. Aim:
Develop a program and analyze complexity to find shortest paths in a graph with
positive edge weights using Dijkstra‟s algorithm.
2. Objective
Code and analyze to find shortest paths in graph with positive edge weights using
Dijkstra‟s
3. Algorithm
1. Initialize an adjacency matrix for node connections and set all distances to
infinity (INF).
2. Input number of vertices and edges, then input each edge's vertices and weight.
3. Set the initial distance from the source node to itself as zero.
4. Use a priority queue to manage and retrieve the node with the shortest distance
not yet visited.
5. For each node processed, update the distances to its adjacent nodes if a shorter
path is found.
6. Mark each node as visited once it's processed to prevent reprocessing.
7. Output the shortest distances from source node to all other nodes in the graph.
4. Implemetation/Code
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1001;
const long long INF = 1e18;
int main() {
int n, m;
cin >> n >> m;
// Adjacency matrix to represent the graph
vector<vector<pair<int, long long>>> adj(n + 1); // 1-based indexing
// Initialize edge weights to infinity
for (int i = 1; i <= n; ++i) {
adj[i].assign(n + 1, {0, INF}); // Initialize with default value (0, INF)
}
// Read edges
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
adj[u][v] = {1, w}; // Indicate connection and weight
adj[v][u] = {1, w}; // Handle undirected graphs
}
int source;
cin >> source;
// Distance array to store shortest distances
vector<long long> dist(n + 1, INF); // 1-based indexing
// Visited array to track processed vertices
vector<bool> visited(n + 1, false); // 1-based indexing
// Priority queue for efficient retrieval of nearest unvisited node
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
greater<pair<long long, int>>> pq;
// Start from source node with distance 0
dist[source] = 0;
pq.emplace(0, source);
while (!pq.empty()) {
pair<long long, int> current = pq.top();
pq.pop();
long long current_dist = current.first;
int u = current.second;
if (visited[u]) {
continue; // Skip if already visited
}
visited[u] = true;
for (const auto& neighbor : adj[u]) {
int v = neighbor.first; // Neighboring vertex
long long weight = neighbor.second; // Edge weight
if (dist[v] > current_dist + weight) {
dist[v] = current_dist + weight;
pq.emplace(dist[v], v);
}
}
}
// Print shortest distances
for (int i = 1; i <= n; ++i) {
if (dist[i] == INF) {
cout << "Distance from " << source << " to " << i << " is INF\n";
} else {
cout << "Distance from " << source << " to " << i << " is " << dist[i] << "\n";
}
}
return 0;
}
Output:
5. Time Complexity : O( n2+ m + nlog n)
6. Space Complexity : O(n2)
7. Learning Outcomes:-
1. Understand the implementation of Dijkstra's algorithm using adjacency
matrices.
2. Learn how to use priority queues to manage processing order of nodes based on
distance.
3. Recognize effect of node visitation state in preventing cycles and redundant
processing.
4. Gain experience in handling large constant values for initializations and
comparisons in graph algorithms.