lundi 10 mai 2021

the result is printed more than one times

class Graph{
 public:
    //Function to return a list containing the DFS traversal of the graph.
    int Vkor;
    //map <int ,bool > visited;
    vector<int>vec;
    
    void dfsutil(int v,vector<int> adj[],map<int,bool> visited)
    {
        //vector<int>vec;
        
        visited[v]=true;
        vec.push_back(v);
        vector<int>::iterator vptr;
        for(vptr=adj[v].begin();vptr!=adj[v].end();vptr++)
        {
            if(!visited[*vptr])
            {
                //vec.push_back(v);
                dfsutil(*vptr,adj,visited);
            }
        }
        //return vec;
    }
    vector<int>dfsOfGraph(int V, vector<int> adj[])
    {
        this->Vkor=V;
        map<int,bool> visited;
        for(int im=0;im<=V-1;im++)
        {
            visited[im]=false;
        }
        for (int i=0;i<=adj->size()-1;i++)
        {`enter code here`
            if(!visited[i])//.first])
            dfsutil(i,adj,visited);
        }
        return vec;//dfsutil(V-1,adj,visited);
        
        //return vec;
    }
};
int main() {}

////5 4 :0 3 0 2 0 1 2 4 and the result should be 0 1 2 4 3 (not 0 1 2 4 3 1 0 2 4 3 2 0 1 3 4) //Can someone tell me what the error is? Thank you.

Aucun commentaire:

Enregistrer un commentaire