Skip to content

Commit c33d76a

Browse files
authored
Merge pull request #89 from Souradeephazra123/main
Added Dijkstra's algorithm
2 parents 9313fb2 + 373cf72 commit c33d76a

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <climits>
4+
5+
const int V = 6; // Number of vertices in the graph
6+
7+
// Function to find the vertex with the minimum distance value from the set of vertices not yet included in the shortest path tree
8+
int minDistance(const std::vector<int>& dist, const std::vector<bool>& sptSet) {
9+
int minDist = INT_MAX, minIndex = -1;
10+
for (int v = 0; v < V; ++v) {
11+
if (!sptSet[v] && dist[v] < minDist) {
12+
minDist = dist[v];
13+
minIndex = v;
14+
}
15+
}
16+
return minIndex;
17+
}
18+
19+
// Function to print the constructed distance array
20+
void printSolution(const std::vector<int>& dist) {
21+
std::cout << "Vertex \t Distance from Source" << std::endl;
22+
for (int i = 0; i < V; ++i) {
23+
std::cout << i << " \t " << dist[i] << std::endl;
24+
}
25+
}
26+
27+
// Dijkstra's algorithm to find the shortest path from source to all vertices
28+
void dijkstra(const std::vector<std::vector<int>>& graph, int src) {
29+
std::vector<int> dist(V, INT_MAX); // The output array to store the shortest distance from the source vertex
30+
std::vector<bool> sptSet(V, false); // Set to keep track of vertices included in the shortest path tree
31+
32+
dist[src] = 0; // Distance from the source to itself is 0
33+
34+
for (int count = 0; count < V - 1; ++count) {
35+
int u = minDistance(dist, sptSet);
36+
37+
sptSet[u] = true;
38+
39+
for (int v = 0; v < V; ++v) {
40+
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
41+
dist[v] = dist[u] + graph[u][v];
42+
}
43+
}
44+
}
45+
46+
printSolution(dist);
47+
}
48+
49+
int main() {
50+
std::vector<std::vector<int>> graph = {
51+
{0, 2, 4, 0, 0, 0},
52+
{2, 0, 3, 2, 0, 0},
53+
{4, 3, 0, 0, 1, 0},
54+
{0, 2, 0, 0, 3, 2},
55+
{0, 0, 1, 3, 0, 2},
56+
{0, 0, 0, 2, 2, 0}
57+
};
58+
59+
int src = 0; // Source vertex
60+
61+
dijkstra(graph, src);
62+
63+
return 0;
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <climits>
4+
5+
const int V = 6; // Number of vertices in the graph
6+
7+
// Function to find the vertex with the minimum distance value from the set of vertices not yet included in the shortest path tree
8+
int minDistance(const std::vector<int>& dist, const std::vector<bool>& sptSet) {
9+
int minDist = INT_MAX, minIndex = -1;
10+
for (int v = 0; v < V; ++v) {
11+
if (!sptSet[v] && dist[v] < minDist) {
12+
minDist = dist[v];
13+
minIndex = v;
14+
}
15+
}
16+
return minIndex;
17+
}
18+
19+
// Function to print the constructed distance array
20+
void printSolution(const std::vector<int>& dist) {
21+
std::cout << "Vertex \t Distance from Source" << std::endl;
22+
for (int i = 0; i < V; ++i) {
23+
std::cout << i << " \t " << dist[i] << std::endl;
24+
}
25+
}
26+
27+
// Dijkstra's algorithm to find the shortest path from source to all vertices
28+
void dijkstra(const std::vector<std::vector<int>>& graph, int src) {
29+
std::vector<int> dist(V, INT_MAX); // The output array to store the shortest distance from the source vertex
30+
std::vector<bool> sptSet(V, false); // Set to keep track of vertices included in the shortest path tree
31+
32+
dist[src] = 0; // Distance from the source to itself is 0
33+
34+
for (int count = 0; count < V - 1; ++count) {
35+
int u = minDistance(dist, sptSet);
36+
37+
sptSet[u] = true;
38+
39+
for (int v = 0; v < V; ++v) {
40+
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
41+
dist[v] = dist[u] + graph[u][v];
42+
}
43+
}
44+
}
45+
46+
printSolution(dist);
47+
}
48+
49+
int main() {
50+
std::vector<std::vector<int>> graph = {
51+
{0, 2, 4, 0, 0, 0},
52+
{2, 0, 3, 2, 0, 0},
53+
{4, 3, 0, 0, 1, 0},
54+
{0, 2, 0, 0, 3, 2},
55+
{0, 0, 1, 3, 0, 2},
56+
{0, 0, 0, 2, 2, 0}
57+
};
58+
59+
int src = 0; // Source vertex
60+
61+
dijkstra(graph, src);
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)