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

Harshit DAA 3.2

The document describes an experiment to implement and analyze Dijkstra's algorithm for finding the shortest paths in a graph with positive edge weights. It includes: 1) The aim, objective, input/apparatus, procedure, sample code, observations, and time/space complexities of implementing Dijkstra's algorithm. 2) The implementation uses an adjacency matrix and priority queue, calculates distances and updates the distance array at each step, and has time complexity O(V^2) and space complexity O(V^2). 3) The student learned about Dijkstra's algorithm, finding shortest paths in graphs, and analyzing its time and space complexity.

Uploaded by

harshit143singh
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)
28 views5 pages

Harshit DAA 3.2

The document describes an experiment to implement and analyze Dijkstra's algorithm for finding the shortest paths in a graph with positive edge weights. It includes: 1) The aim, objective, input/apparatus, procedure, sample code, observations, and time/space complexities of implementing Dijkstra's algorithm. 2) The implementation uses an adjacency matrix and priority queue, calculates distances and updates the distance array at each step, and has time complexity O(V^2) and space complexity O(V^2). 3) The student learned about Dijkstra's algorithm, finding shortest paths in graphs, and analyzing its time and space complexity.

Uploaded by

harshit143singh
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/ 5

Experiment-3.

Student Name: Harshit Singh UID: 21BCS7148


Branch: BE-CSE Section/Group: 21BCS_IOT-605 B
Semester: 5th Date of Performance: 31/10/23
Subject Name: Design and Analysis of Subject Code: 21CSH-311
Algorithms with LAB

Aim:
Develop a program and analyze complexity to find shortest paths in a graph with positive
edge weights using Dijkstra’s algorithm.

Objective:

Code and analyze to find shortest paths in a graph with positive edge weights using
Dijkstra’s Algorithm.

Input/Apparatus Used:

Visual studio code, GDB compiler, etc.


Graph ( G =(V,E) ) is taken as input

Procedure/Algorithm:

Step-1: Create a graph represented by the adjacency matrix 'adj[maxv][maxv]' with 'V'
vertices.
Step-2: Initialize an array 'dist[maxv]' with size 'V' to store the shortest distances from
the source vertex. Set all elements to infinity (or a sufficiently large value), except for
the source vertex, which is set to 0.
Step-3: Create a priority queue (min-heap) to store vertices to be processed. Initialize

Name: Harshit Singh UID: 21BCS7148


the queue with the source vertex and its distance (0).
Step-4: While the priority queue is not empty, do the following:
 Pop the vertex with the minimum distance from the priority queue. Let's call it
'u'.
 If the minimum distance to 'u' has been updated in the meantime, ignore it (this
is known as a relaxation step).

Step-4.1: For each adjacent vertex 'v' to 'u', do the following:


 If there is an edge between 'u' and 'v' (i.e., 'adj[u][v]' is non-zero), calculate the
tentative distance from the source to 'v' through 'u'. This is done by adding the
distance from the source to 'u' and the weight of the edge between 'u' and 'v'.
 If the calculated distance is less than the current known distance to 'v' (i.e.,
'dist[v]'), update 'dist[v]' with the new distance.
 Push the vertex 'v' with the updated distance into the priority queue.
Step-5: After the algorithm completes, the 'dist[]' array will contain the shortest
distances from the source vertex to all other vertices in the graph.
Step-6: Print or use the 'dist[]' array to access the shortest distances.

Sample Code:

#include <bits/stdc++.h>
#include <queue>
#include <climits>

using namespace std;

#define INF INT_MAX


#define maxv 1000

struct Edge {

Name: Harshit Singh UID: 21BCS7148


int to, weight;
};

struct Compare {
bool operator()(const Edge &a, const Edge &b) {
return a.weight > b.weight;
}
};

int V;
int adj[maxv][maxv];
int dist[maxv];

void dijkstra(int src) {


fill(dist, dist + V, INF);
dist[src] = 0;

priority_queue<Edge, vector<Edge>, Compare> pq;


pq.push({src, 0});

while (!pq.empty()) {
int u = pq.top().to;
int weight = pq.top().weight;
pq.pop();
if (weight > dist[u]) continue;
for (int v = 0; v < V; ++v) {
if (adj[u][v] && dist[v] > dist[u] + adj[u][v]) {
dist[v] = dist[u] + adj[u][v];
pq.push({v, dist[v]});
}

Name: Harshit Singh UID: 21BCS7148


}
}

cout << "Vertex \t Distance from Source" << endl;


for (int i = 0; i < V; i++) {
cout << i << "\t " << dist[i] << endl;
}
}

int main() {
V = 5;
for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
adj[i][j] = 0;
}
}

adj[0][1] = 4;
adj[0][2] = 8;
adj[1][2] = 7;
adj[1][3] = 9;
adj[2][4] = 5;
adj[3][4] = 6;

int source = 0;
dijkstra(source);

return 0;
}

Name: Harshit Singh UID: 21BCS7148


Observations/Outcome:

Time and Space Complexities:


Time Complexity:
The time complexity of the priority queue-based Dijkstra's algorithm is O(V^2),
where V is the number of vertices in the graph.

Space Complexity:
The space complexity is O(V^2) for the adjacency matrix because it uses a 2D array
to represent the graph, which has V * V elements.

Learning Outcomes:
 I learned about Dijkstra’s Algorithm.
 I learned to find the shortest path in a graph with positive edge weights using
Dijkstra’s Algorithm.
 I learned about the time and space complexity of Dijkstra’s Algorithm.
 I learned about its implementation and uses in path finding.

Name: Harshit Singh UID: 21BCS7148

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy