snippets/graph/Connectivity/isStronglyConnected.cpp
bool Graph::isSC()
{
    // St1p 1: Mark all the vertices as not visited (For first DFS)
    bool visited[V];
    for (int i = 0; i < V; i++)
        visited[i] = false;

    // Step 2: Do DFS traversal starting from first vertex.
    DFSUtil(0, visited);

     // If DFS traversal doesn’t visit all vertices, then return false.
    for (int i = 0; i < V; i++)
        if (visited[i] == false)
             return false;

    // Step 3: Create a reversed graph
    Graph gr = getTranspose();

    // Step 4: Mark all the vertices as not visited (For second DFS)
    for(int i = 0; i < V; i++)
        visited[i] = false;

    // Step 5: Do DFS for reversed graph starting from first vertex.
    // Staring Vertex must be same starting point of first DFS
    gr.DFSUtil(0, visited);

    // If all vertices are not visited in second DFS, then
    // return false
    for (int i = 0; i < V; i++)
        if (visited[i] == false)
             return false;

    return true;
}
// The main function that returns true if graph is strongly connected
The main function that returns true if graph is strongly connected
The main function that returns true if graph is strongly connected
bool Graph::isSC()
{
    bool visited[V];
    for (int i = 0; i < V; i++)
        visited[i] = false;
    DFSUtil(0, visited);
    for (int i = 0; i < V; i++)
        if (visited[i] == false)
             return false;
    Graph gr = getTranspose();
    for(int i = 0; i < V; i++)
        visited[i] = false;
    gr.DFSUtil(0, visited);
    for (int i = 0; i < V; i++)
        if (visited[i] == false)
             return false;
    return true;
}
bool Graph :: isSC ( ) { bool visited [ V ] ; for ( int i = 0 ; i < V ; i ++ ) visited [ i ] = false ; DFSUtil ( 0 , visited ) ; for ( int i = 0 ; i < V ; i ++ ) if ( visited [ i ] == false ) return false ; Graph gr = getTranspose ( ) ; for ( int i = 0 ; i < V ; i ++ ) visited [ i ] = false ; gr . DFSUtil ( 0 , visited ) ; for ( int i = 0 ; i < V ; i ++ ) if ( visited [ i ] == false ) return false ; return true ; }
The main function that returns true if graph is strongly connected
The main function that returns true if graph is strongly connected
// St1p 1: Mark all the vertices as not visited (For first DFS) // Step 2: Do DFS traversal starting from first vertex. // If DFS traversal doesn’t visit all vertices, then return false. // Step 3: Create a reversed graph // Step 4: Mark all the vertices as not visited (For second DFS) // Step 5: Do DFS for reversed graph starting from first vertex. // Staring Vertex must be same starting point of first DFS // If all vertices are not visited in second DFS, then // return false