forked from pujazha/Hactoberfest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkshtra's_Algorithm.cpp
83 lines (64 loc) · 2.17 KB
/
dijkshtra's_Algorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <vector>
#include <queue>
#include <limits>
using namespace std;
const int INF = numeric_limits<int>::max();
// Class to represent edges in the graph
class Edge {
public:
int destination;
int weight;
Edge(int dest, int w) : destination(dest), weight(w) {}
};
// Class to represent the graph using an adjacency list
class Graph {
public:
int numVertices;
vector<vector<Edge>> adjList;
Graph(int V) : numVertices(V), adjList(V) {}
// Add an edge to the graph
void addEdge(int src, int dest, int weight) {
adjList[src].emplace_back(dest, weight);
adjList[dest].emplace_back(src, weight); // For undirected graph
}
};
void dijkstra(Graph& graph, int startVertex) {
int numVertices = graph.numVertices;
vector<int> dist(numVertices, INF); // Initialize distances with infinity
dist[startVertex] = 0; // Distance from the source vertex to itself is 0
// Priority queue to store vertices and their distances
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push(make_pair(0, startVertex));
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
// Visit each neighbor of u and relax the edges
for (const Edge& edge : graph.adjList[u]) {
int v = edge.destination;
int weight = edge.weight;
if (dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
pq.push(make_pair(dist[v], v));
}
}
}
// Print the shortest distances from the startVertex
for (int i = 0; i < numVertices; ++i) {
cout << "Shortest distance from " << startVertex << " to " << i << " is: " << dist[i] << endl;
}
}
int main() {
Graph graph(6); // Example graph with 6 vertices
// Adding edges and their weights
graph.addEdge(0, 1, 5);
graph.addEdge(0, 2, 1);
graph.addEdge(1, 3, 3);
graph.addEdge(1, 4, 7);
graph.addEdge(2, 4, 1);
graph.addEdge(2, 5, 5);
int startVertex = 0; // Starting vertex for Dijkstra's algorithm
cout << "Dijkstra's Algorithm:" << endl;
dijkstra(graph, startVertex);
return 0;
}