#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
#define V 3
template<typename T>
class graph
{
int edge;
std::map<T ,std::list<std::pair<T,int> > > adj;
public :
void ae(T a,T b,int weight)
{
adj[a].push_back(std::pair<T,int>(b,weight));
}
void bfs(T source)
{
std::vector<bool> vis;
for(int i=0;i<V;i++)
vis[i]=false;
vis[source]=true;
std::queue<T> q;
q.push(source);
while(!q.empty())
{
T s=q.front();
std::cout<<s<<" ";
q.pop();
for(auto ash:adj[s])
{
if(!vis[ash.first])
{
vis[ash.first]=true;
q.push(ash.first);
}
}
}
}
};
using namespace std;
int main()
{
graph<int> g;
g.ae(0,1,1); // adding edge 0 -> 1 with weight=1;
g.ae(0,2,1); // adding edge 0 -> 2 with weight=1;
g.ae(1,0,1); // adding edge 1 -> 0 with weight=1;
g.bfs(0); // bfs
return 0;
}
Please format your code by ``` before code and after code.
Since you are using map, I assume that you can have any node number
for that use map
std::map<T,bool> vis;
1 Like
thanks got it!!