#include <bits/stdc++.h>
using namespace std;

class graph
{
    int V;
    list<int> *adj;
    public:
    graph(int V)
    {
        this->V =V;
        adj = new list<int>[V];
    }
    void addEdge(int v, int w);
    void bfs(int s);
    void dfs(int s);
    
};

void graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
}

void graph::bfs(int s)
{

    bool *visited = new bool[V];
    for(int i = 0; i < V; i++)
        visited[i] = false;
 
 
    list<int> queue;
 
  
    visited[s] = true;
    queue.push_back(s);
 

    list<int>::iterator i;
 
    while(!queue.empty())
    {
        
        s = queue.front();
        cout << s << " ";
        queue.pop_front();
 
       
        for (i = adj[s].begin(); i != adj[s].end(); ++i)
        {
            if (!visited[*i])
            {
                visited[*i] = true;
                queue.push_back(*i);
            }
        }
    }

}

void graph::dfs(int s)
{
    vector<bool> visited(V, false);
    stack<int> stack;
    stack.push(s);
    while (!stack.empty())
    {
        int s = stack.top();
        stack.pop();
        if (!visited[s])
        {
            cout<< s <<" ";
            visited[s] = true;
        }
        for (auto i=adj[s].begin(); i!=adj[s].end(); i++)
        {
            if (!visited[*i])
            {
                stack.push(*i);
            }
        }
    }
}



int main()
{
     int n;
     cout<<"enter the total no of vertex: ";
     cin>>n;
     graph obj(n);

     int e;
     cout<<"enter the total no of edge: ";
     cin>>e;
     for (int i =0; i<e ; i++)
     {
         int temp1, temp2;
         cout<<" enter the edge: ";
        cin>>temp1>>temp2;
        obj.addEdge(temp1, temp2);
     }

     cout<<"\nthe BFS of the given graph from vertex 0\n";
     obj.bfs(0);

     cout<<"\nthe DFS of the given graph from vertex 0\n";
     obj.dfs(0);


} 
by