Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
552 B
2
Indexable
    vector<int> dfsOfGraph(int V, vector<int> adj[]) {
        bool visited[V] = {false};
        vector<int> dfsVec;
        dfsUtil(0, visited, dfsVec, adj);
        return dfsVec;
    }
    
    void dfsUtil(int i, bool visited[], vector<int> &dfsVec, vector<int> adj[]) {
        dfsVec.push_back(i);
        visited[i] = true;
        
        for (int j = 0; j < adj[i].size(); j++) {
            int connectedNode = adj[i][j];
            if (!visited[connectedNode])
                dfsUtil(connectedNode, visited, dfsVec, adj);
        }
    }