Subscribe Us

Responsive Advertisement

Advertisement

Problem 15 : Print all the node using DFS|C++

 

Print all the node using DFS|C++

By Ujjal Roy





#include<bits/stdc++.h>

using namespace std;

const int N=1000;

vector<int>adj[N];

int vis[N];

void dfs(int node )

{

    vis[node]=1;

    cout<<node<<" ";

    for(auto u:adj[node])

    {

        if(vis[u]==0)dfs(u);

    }


}

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);


    }

    dfs(0);


    return 0;

}


Post a Comment

0 Comments