Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Dijkstra's algorithm #89

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions application of algorithm/shortestPath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <vector>
#include <climits>

const int V = 6; // Number of vertices in the graph

// Function to find the vertex with the minimum distance value from the set of vertices not yet included in the shortest path tree
int minDistance(const std::vector<int>& dist, const std::vector<bool>& sptSet) {
int minDist = INT_MAX, minIndex = -1;
for (int v = 0; v < V; ++v) {
if (!sptSet[v] && dist[v] < minDist) {
minDist = dist[v];
minIndex = v;
}
}
return minIndex;
}

// Function to print the constructed distance array
void printSolution(const std::vector<int>& dist) {
std::cout << "Vertex \t Distance from Source" << std::endl;
for (int i = 0; i < V; ++i) {
std::cout << i << " \t " << dist[i] << std::endl;
}
}

// Dijkstra's algorithm to find the shortest path from source to all vertices
void dijkstra(const std::vector<std::vector<int>>& graph, int src) {
std::vector<int> dist(V, INT_MAX); // The output array to store the shortest distance from the source vertex
std::vector<bool> sptSet(V, false); // Set to keep track of vertices included in the shortest path tree

dist[src] = 0; // Distance from the source to itself is 0

for (int count = 0; count < V - 1; ++count) {
int u = minDistance(dist, sptSet);

sptSet[u] = true;

for (int v = 0; v < V; ++v) {
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}

printSolution(dist);
}

int main() {
std::vector<std::vector<int>> graph = {
{0, 2, 4, 0, 0, 0},
{2, 0, 3, 2, 0, 0},
{4, 3, 0, 0, 1, 0},
{0, 2, 0, 0, 3, 2},
{0, 0, 1, 3, 0, 2},
{0, 0, 0, 2, 2, 0}
};

int src = 0; // Source vertex

dijkstra(graph, src);

return 0;
}