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

Dijkstra algorithm added in cpp #737

Closed
wants to merge 1 commit into from
Closed
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
42 changes: 42 additions & 0 deletions dijstra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<bits/stdc++.h>
using namespace std;
int main(){
int n=5,m=6,source=1;
// creating an adjacentry list (1 based indexing)
vector<pair<int,int> > g[n+1];
g[1].push_back({2,2});
g[1].push_back({4,1});
g[2].push_back({1,2});
g[2].push_back({5,5});
g[2].push_back({3,4});
g[3].push_back({2,4});
g[3].push_back({4,3});
g[3].push_back({5,1});
g[4].push_back({1,1});
g[4].push_back({3,3});
g[5].push_back({2,5});
g[5].push_back({3,1});
// Dijkstra's algorithm begins from here
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int>>> pq;
vector<int> distTo(n+1,INT_MAX);// distance array intialised to infinte
distTo[source] = 0; // source to source is 0
pq.push(make_pair(0,source)); // (dist,source)
while(!pq.empty() ){
int dist = pq.top().first;
int prev = pq.top().second;
pq.pop();
vector<pair<int,int> >::iterator it;
for( it = g[prev].begin() ; it != g[prev].end() ; it++){
int next = it->first;
int nextDist = it->second;
if( distTo[next] > distTo[prev] + nextDist){
distTo[next] = distTo[prev] + nextDist;
pq.push(make_pair(distTo[next], next));
}
}
}
cout << "The distances from source " << source << " are : \n";
for(int i = 1 ; i<=n ; i++) cout << distTo[i] << " ";
cout << "\n";
return 0;
}