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

Lab 5 (Fouzia)

lab task

Uploaded by

imransarker.web
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)
19 views4 pages

Lab 5 (Fouzia)

lab task

Uploaded by

imransarker.web
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

Lab no: 05

Lab Name: Implement Single source shortest path (Dijkstra)

Single Source Shortest Path: The Single Source Shortest Path problem aims to find
the shortest path from a given source vertex to all other vertices in a weighted graph.
Dijkstra's Algorithm solves this problem for graphs with non-negative edge weights. It
starts from the source vertex, initializes distances to all other vertices as infinite, and sets
the source's distance to zero. The algorithm repeatedly selects the vertex with the
smallest known distance, updates the distances of its neighboring vertices, and marks it
as processed. This process continues until all vertices are processed, ensuring that the
shortest path from the source to every other vertex is found.

Dijkstra’s Algorithm
For each edge (u, v) E, assume w(u, v) ≥ 0, maintain a set S of vertices whose
final shortest path weights have been determined. Repeatedly select u V − S with
minimum shortest path estimate, add u to S, relax all edges out of u.

Pseudo-code
Dijkstra (G, W, s) //uses priority queue Q
Initialize (G, s)
S←φ
Q ← V [G] //Insert into Q
while Q = φ
do u ← EXTRACT-MIN(Q) //deletes u from Q
S = S ∪ {u}
for each vertex v Adj[u]
do RELAX (u, v, w) ← this is an implicit DECREASE KEY operation

Code:

#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <limits>
#include <functional>

using namespace std;

typedef pair<int, int> iPair;

void dijkstra(const vector<vector<iPair>> &graph, int src) {


int V = graph.size();
vector<int> dist(V, numeric_limits<int>::max());
vector<bool> visited(V, false);

priority_queue<iPair, vector<iPair>, greater<iPair>> pq;


pq.push(make_pair(0, src));
dist[src] = 0;

while (!pq.empty()) {
int u = pq.top().second;
pq.pop();

if (visited[u]) continue;
visited[u] = true;

for (const auto &adj : graph[u]) {


int v = adj.first;
int weight = adj.second;

// Relaxation step
if (!visited[v] && dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}
}

cout << "Vertex Distance from Source\n";


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

int main() {
int V = 9;
vector<vector<iPair>> graph(V);

graph[0].push_back(make_pair(1, 4));
graph[0].push_back(make_pair(7, 8));
graph[1].push_back(make_pair(0, 4));
graph[1].push_back(make_pair(2, 8));
graph[1].push_back(make_pair(7, 11));
graph[2].push_back(make_pair(1, 8));
graph[2].push_back(make_pair(3, 7));
graph[2].push_back(make_pair(5, 4));
graph[2].push_back(make_pair(8, 2));
graph[3].push_back(make_pair(2, 7));
graph[3].push_back(make_pair(4, 9));
graph[3].push_back(make_pair(5, 14));
graph[4].push_back(make_pair(3, 9));
graph[4].push_back(make_pair(5, 10));
graph[5].push_back(make_pair(2, 4));
graph[5].push_back(make_pair(3, 14));
graph[5].push_back(make_pair(4, 10));
graph[5].push_back(make_pair(6, 2));
graph[6].push_back(make_pair(5, 2));
graph[6].push_back(make_pair(7, 1));
graph[6].push_back(make_pair(8, 6));
graph[7].push_back(make_pair(0, 8));
graph[7].push_back(make_pair(1, 11));
graph[7].push_back(make_pair(6, 1));
graph[7].push_back(make_pair(8, 7));
graph[8].push_back(make_pair(2, 2));
graph[8].push_back(make_pair(6, 6));
graph[8].push_back(make_pair(7, 7));

dijkstra(graph, 0);

return 0;
}

Output for above code:


Conclusion: The implementation of Dijkstra's Algorithm in the lab provided a practical
understanding of finding the shortest paths from a single source to all other vertices in a weighted
graph. By applying the algorithm to various graph structures and analyzing the results, I gained
valuable insights into how the algorithm efficiently handles non-negative edge weights and
ensures optimal paths. The hands-on experience reinforced the theoretical concepts and
demonstrated the algorithm's effectiveness in real-world applications, such as network routing
and navigation systems. Overall, this lab exercise solidified my grasp of Dijkstra's Algorithm and
its significance in solving shortest path problems.

You might also like