0% found this document useful (0 votes)
13 views4 pages

Arun Exp-8

experiment 8 of DAA 5th sem in Chandigarh University

Uploaded by

kumararunlamba89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Arun Exp-8

experiment 8 of DAA 5th sem in Chandigarh University

Uploaded by

kumararunlamba89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment-8
Student Name: Arun UID:22BET10320
Branch: IT Section/Group:22BET_IoT_703-B
Semester: 5th Date of Performance: /10/2024
Subject Name: DAA Lab Subject Code: 22CHS-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: To implement and analyze the efficiency of Dijkstra’s algorithm for finding
the shortest paths in a graph with positive edge weights. The goal is to evaluate the
algorithm's time complexity based on the data structure used for the priority queue (e.g.,
binary heap or Fibonacci heap).

3. Implementation/Code:
#include <iostream>
#include <vector>
#include <queue>
#include <climits>

using namespace std;

// Define a pair to store (distance, vertex)


typedef pair<int, int> PII;

/**
* Dijkstra's algorithm to find the shortest paths from a source node.
*
* @param source The source node.
* @param graph The graph represented as an adjacency list.
* @param V The number of vertices in the graph.
*
* @return A vector of shortest distances from the source node to all other nodes.
*/
vector<int> dijkstra(int source, const vector<vector<PII>>& graph, int V) {
// Create a min-heap priority queue to store vertices to be processed
priority_queue<PII, vector<PII>, greater<PII>> pq;

// Initialize distances to all vertices as infinity


vector<int> dist(V, INT_MAX);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

// Distance to the source node is 0


dist[source] = 0;

// Push the source node into the priority queue


pq.push({0, source});

// Process vertices in the priority queue


while (!pq.empty()) {
// Get the vertex with the smallest distance
int u = pq.top().second;
pq.pop();

// Traverse all adjacent vertices of u


for (const auto& neighbor : graph[u]) {
int v = neighbor.second; // Adjacent vertex
int weight = neighbor.first; // Weight of the edge

// Relaxation step: Update the distance to v if a shorter path is found


if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push({dist[v], v}); // Push updated distance to the priority queue
}
}
}

// Return the shortest distances to all vertices


return dist;
}

int main() {
int V, E; // Number of vertices and edges
cout << "Enter the number of vertices: ";
cin >> V;
cout << "Enter the number of edges: ";
cin >> E;

vector<vector<PII>> graph(V); // Graph represented as an adjacency list

// Input edges (source, destination, weight) from the user


cout << "Enter the edges (format: source destination weight):\n";
for (int i = 0; i < E; ++i) {
int u, v, weight;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

cin >> u >> v >> weight;


graph[u].push_back({weight, v}); // Add edge to the graph
}

int source; // Set the source node


cout << "Enter the source node: ";
cin >> source;

// Run Dijkstra's algorithm


vector<int> distances = dijkstra(source, graph, V);

// Output the shortest distances from the source node


cout << "Shortest distances from node " << source << ":\n";
for (int i = 0; i < V; ++i) {
cout << "Node " << i << ": " << (distances[i] == INT_MAX ? "Infinity":
to_string(distances[i])) << "\n";
}

return 0;
}

4. Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. 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.

6. Learning Outcomes:
• 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.
• 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.
• Graph Representation: You will understand how to represent graphs efficiently using
adjacency lists and handle shortest path problems with real-world applications.

You might also like