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 algorithm in cpp #189

Merged
merged 3 commits into from
Oct 30, 2020
Merged
Changes from all commits
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
79 changes: 79 additions & 0 deletions Dijkstra Shortest Path/dijkstra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

#include<bits/stdc++.h>
using namespace std;
// Total number of vertices in graph
const V = 9;

// For printing the shortest path
int shortest_path(int dist[], int n)
{
cout<<"Vertex "<<"\t"<<"Distance from Source\n";
for (int i = 0; i < V; i++)
cout<<" \t\t \n"<< i<<" \t\t "<<dist[i];
}

// For calculating minimum distance
int minDist(int dist[], bool Set[])
{
int min = INT_MAX, min_index;
for (int i = 0; i < V; i++)
if (Set[i] == false && dist[i] <= min)
min = dist[i], min_index = i;
return min_index;
}

// Calculate shortest paths from source to all other vertices
void Dijkstra(int g[V][V], int src)
{
int dist[V];
bool Set[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, Set[i] = false;
dist[src] = 0;
for (int c = 0; c < V- 1; c++)
{
int u = minDist(dist, Set);
Set[u] = true;
for (int j = 0; j < V; j++)
if (!Set[j] && g[u][j] && dist[u] != INT_MAX && dist[u]+ g[u][j] < dist[j])
{
dist[j] = dist[u] + g[u][j];
}
}
shortest_path(dist, V);
}


//DIJKSTRA
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

// int G[V][V] = {
// { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
// { 4, 0, 8, 0, 0, 0, 0, 11, 0 },
// { 0, 8, 0, 7, 0, 4, 0, 0, 2 },
// { 0, 0, 7, 0, 9, 14, 0, 0, 0 },
// { 0, 0, 0, 9, 0, 10, 0, 0, 0 },
// { 0, 0, 4, 14, 10, 0, 2, 0, 0 },
// { 0, 0, 0, 0, 0, 2, 0, 1, 6 },
// { 8, 11, 0, 0, 0, 0, 1, 0, 7 },
// { 0, 0, 2, 0, 0, 0, 6, 7, 0 }};
int G[V][V];
cout<<"Enter the graph"<<endl;
for(int i=0;i<V;i++)
{
for(int j=0;j<V;j++)
{
cin>>G[i][j];
}
cout<<"\n";
}


cout<< "\nSource vetiex is 0\n"<<endl;
cout<<"following shows SHORTEST Distance of vertices from source vertex \n\n"<<endl;
Dijkstra(G, 0);
return 0;
}