Subscribe Us

Responsive Advertisement

Advertisement

Problem 14 : Print all node using BFS | C++

 

            Print all node using BFS | C++

by Ujjal Roy



#include<bits/stdc++.h>

using namespace std;

vector<int>adj[1000];

int vis[1000];

void bfs(int node)

{

    vis[node]=1;

    queue<int>q;

    q.push(node);

    while(!q.empty())

    {

        int cur_node=q.front();

        cout<<cur_node<<endl;

        q.pop();

        for(auto u:adj[cur_node])

        {

            if(vis[u]==0)

            {

                q.push(u);

                vis[u]=1;



            }

        }


    }


}

int main()

{


    cout<<"Enter number of node and edge : ";

    int node,edge;

    cin>>node>>edge;

    while(edge--)

    {

        int u,v;

        cin>>u>>v;

        adj[u].push_back(v);

        adj[v].push_back(u);

    }

    bfs(0);



}


Post a Comment

0 Comments