Is this correct?
If not, please help me identify, whats wrong, and why?
#include<iostream>
#include<map>
#include<list>
#include<queue>
#include<climits>
using namespace std;
template<typename T>
class Graph{
// adj list (vertices mapped with its neighbours)
map<T,list<T>> m;
public:
void addEdge(T x, T y){
m[x].push_back(y);
m[y].push_back(x);
}
// single source shortest path using BFS
void sssp(T src, T dest){
queue<T> q;
map<T,int> dist;
// initialize dist from src to all other vertices = INT_MAX
for(auto node_pair:m){
T node = node_pair.first;
dist[node] = INT_MAX;
}
// overwriting distance of src node
dist[src] = 0;
q.push(src);
// logic using queue(similar to level order traversal)
while(!q.empty()){
T node = q.front();
q.pop();
// traverse adj list, for 'node'
for(auto nbr:m[node]){
if(dist[nbr] == INT_MAX){
// it means, this neighbour has been discovered first time
dist[nbr] = dist[node] + 1;
// push neighbour into queue
q.push(nbr);
}
}
}
// print dist to every node
for(auto node_pair:m){
T node = node_pair.first;
int d = dist[node];
cout<<"Node "<<node<<" is at distance "<<d<<" from source node"<<endl;
}
}
};
int main(){
Graph<int> g;
g.addEdge(0,1);
g.addEdge(1,2);
g.addEdge(2,3);
g.addEdge(3,4);
g.addEdge(4,5);
g.sssp(0,5);
return 0;
}