Skip to content

Commit aec66b8

Browse files
Aman2632Mohamad655
authored andcommitted
Dijkstra_implementation.cpp
dijkstra's shortest path algorithm implemented in C++ with comments to explain each and every step of the algorithm
1 parent 0226ff6 commit aec66b8

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Dijkstra implementation to find the shortest path and print it........
2+
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
typedef long long int ll;
6+
using ii= pair<ll,ll>;
7+
#define F first
8+
#define S second
9+
#define MP make_pair
10+
11+
vector<ii> v[100];
12+
ll dist[100];
13+
ll vst[100];
14+
vector<ll> parent;
15+
16+
class prioritize{ // comparator design of priority queue...
17+
public: bool operator()(ii &p1,ii &p2){
18+
return p1.S>p2.S; // S -> second element of the pair ii... p-> (node,weight)
19+
}
20+
};
21+
22+
void dijkstra(ll s,ll n)
23+
{
24+
for(ll i=0;i<n;i++) // initialize the global variables so that they are reset after each function call...
25+
{
26+
dist[i]=1e18; // give infinite distance to the node3s initially....
27+
vst[i]=0; // mark all as unvisited initially.....
28+
}
29+
30+
dist[s]=0; // set the distance of the source node to be zero....
31+
parent[s]=-1; // set parent of source node as -1.....
32+
priority_queue<ii,vector<ii>,prioritize > pq; // use a priority queue if a pair...its internally wrapped in a vector..hence the
33+
pq.push(MP(s,0)); // vector<ii> in the expression.....// push the source node ......
34+
35+
while(!pq.empty()) // loop untill the pq is empty...
36+
{
37+
ii h=pq.top(); // pop the top most element in the ascending order of the weights...
38+
pq.pop();
39+
ll node=h.F;
40+
41+
if(vst[node])continue; // if the node is already visited , we need not do anything....else...
42+
else
43+
{
44+
vst[node]=1; // mark it visited.....
45+
for(auto u: v[node]) // loop on all its neighbours in its adjacency list.....
46+
{
47+
ll neigh=u.F; // let neigh be a node in its adjacency list and wt be its distance from the parent node 'node'
48+
ll wt=u.S;
49+
if(dist[neigh]>dist[node]+wt) // if the distance of neigh from souce is > distance of it via the 'node', update the distance....
50+
{
51+
dist[neigh]=dist[node]+wt; // min distance updated...
52+
parent[neigh]=node; // push node as the parent of neigh as it get its shortst distance when accessed through 'node '...
53+
pq.push(make_pair(neigh,dist[neigh])); // now push the neigh into the pq, so that its neighbours can be analysed...
54+
}
55+
56+
}
57+
}
58+
}
59+
}
60+
61+
62+
int main()
63+
{
64+
ios_base::sync_with_stdio(false);
65+
cin.tie(NULL);
66+
67+
ll t;
68+
cin>>t;
69+
while(t--)
70+
{
71+
ll n;
72+
cin>>n;
73+
74+
for(ll i=0;i<n;i++){ // clearing the adjacency list before addressing each testcase coz the global list may have the data from a
75+
v[i].clear(); // previous test case..
76+
}
77+
78+
for(ll i=0;i<n;i++)
79+
{
80+
for(ll j=0;j<n;j++)
81+
{
82+
ll x;
83+
cin>>x;
84+
// wt[i][j]=x;
85+
if(x!=0)
86+
{
87+
v[i].push_back(MP(j,x)); // creating our adjacency list.we push a pair of (node,weight) in the adjac. list of another node
88+
}
89+
}
90+
}
91+
92+
ll s,g; // s-> source g-> destination....
93+
cin>>s>>g;
94+
parent.resize(n,-1); // must resize the parent vector otherwise u may get runtime (t)error....
95+
dijkstra(s,n); // calling dijkstra....
96+
ll j=dist[g]; // j-> shortest path of g from s...accesing it using the distance array
97+
cout<<j<<"\n";
98+
99+
vector<ll> path; // vector to store the shortest path nodes.....
100+
ll dest=g;
101+
while(dest!=-1) // in the end.. only the source will have a parent as -1,
102+
{
103+
path.push_back(dest);
104+
dest=parent[dest]; // -1->0->1->4->8->9->7
105+
}
106+
reverse(path.begin(),path.end()); // reverse the path vector....
107+
for(auto no: path) // loop on the path vector and print the node elements.....
108+
{
109+
cout<<no<<" ";
110+
}
111+
cout<<"\n";
112+
}
113+
}

0 commit comments

Comments
 (0)