Getting segmentation fault while implementing Kosaraju Algorithm

Viewed 9

kosaraju algorithm: Problem statement: Given a Directed Graph with V vertices (Numbered from 0 to V-1) and E edges, Find the number of strongly connected components in the graph. while implementing this code I am getting a segmentation fault. Where is the error in this code?

   void dfs(int node,vector<int>adj[],stack<int>&st,vector<int>&vis){
    vis[node]=1;
    for(auto it: adj[node]){
        if(!vis[it]){
            dfs(it,adj,st,vis);
           
        }
    }
     st.push(node);
}

void revdfs(int node,vector<int>trs[],vector<int>&vis){
      vis[node]=1;
    for(auto it: trs[node]){
        if(!vis[it]){
            revdfs(it,trs,vis);
           
        }
    }
    
}

int kosaraju(int V, vector<int> adj[])
{
    stack<int>st;
    vector<int>vis(V+1,0);
    
    for(int i=0; i<V;i++){
        if(!vis[i])
        dfs(i,adj,st,vis);
    }
    
 
    int cnt=0;
    
vector<int> transpose[V+1]; 

for(int i = 1;i<=V;i++) {
    vis[i] = 0; 
    for(auto it: adj[i]) {
        transpose[it].push_back(i); 
    }
}

    while(!st.empty()){
        int node=st.top();
        st.pop();
        if(!vis[node]){
            cnt++;
            revdfs(node,transpose,vis);
        }
    }
    return cnt;
    
0 Answers
Related