0% found this document useful (0 votes)
32 views5 pages

Experiment 8

The document outlines an experiment on implementing Dijkstra's algorithm to find the shortest paths in a graph with positive edge weights. It includes the aim, objectives, algorithm steps, implementation code, time and space complexity analysis, and learning outcomes. The experiment emphasizes understanding the algorithm's efficiency and practical applications in graph representation.
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)
32 views5 pages

Experiment 8

The document outlines an experiment on implementing Dijkstra's algorithm to find the shortest paths in a graph with positive edge weights. It includes the aim, objectives, algorithm steps, implementation code, time and space complexity analysis, and learning outcomes. The experiment emphasizes understanding the algorithm's efficiency and practical applications in graph representation.
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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 8
Student Name: Sagandeep Singh UID: 22BCS14568
Branch: BE-CSE Section/Group: 22BCS_IOT-618/A
Semester: 5th Date of Performance:
Subject Name: Design and Analysis of Algorithms (DAA)
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: 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. 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.
Step 2:- Push Source:
• Insert the source node into the priority queue with a distance of 0.
Step 3:- While Queue is Not Empty:
• 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.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Step 4:- Repeat:


• Continue this process until the priority queue is empty.
Step 5:- Result:
• At the end, dist[] contains the shortest distance from the source node
to all other nodes.

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

using namespace std;

typedef pair<int, int> PII;

vector<int> dijkstra(int source, const vector<vector<PII>>& graph, int V)


{
priority_queue<PII, vector<PII>, greater<PII>> pq;
vector<int> dist(V, INT_MAX);

dist[source] = 0;
[Link]({0, source});

while (![Link]())
{
int u = [Link]().second;
[Link]();

for (const auto& neighbor : graph[u])


{
int v = [Link];
int weight = [Link];

if (dist[u] + weight < dist[v])


{
dist[v] = dist[u] + weight;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

[Link]({dist[v], v});
}
}
}

return dist;
}

int main()
{
int V = 5;
vector<vector<PII>> graph(V);

graph[0].push_back({10, 1});
graph[0].push_back({3, 2});
graph[1].push_back({1, 3});
graph[2].push_back({4, 1});
graph[2].push_back({8, 3});
graph[2].push_back({2, 4});
graph[3].push_back({7, 4});

int source = 0;

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


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

return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5. Output

Fig 1 Dijkstra’s algorithm

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 Outcome
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
realworld applications.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like