snippets/graph/Connectivity/isConnected.cpp
bool Graph::isReachable(int s, int d)
{
    // Base case
    if (s == d)
      return true;

    // Mark all the vertices as not visited
    bool *visited = new bool[V];
    for (int i = 0; i < V; i++)
        visited[i] = false;

    // Create a queue for BFS
    list<int> queue;

    // Mark the current node as visited and enqueue it
    visited[s] = true;
    queue.push_back(s);

    // it will be used to get all adjacent vertices of a vertex
    list<int>::iterator i;

    while (!queue.empty())
    {
        // Dequeue a vertex from queue and print it
        s = queue.front();
        queue.pop_front();

        // Get all adjacent vertices of the dequeued vertex s
        // If a adjacent has not been visited, then mark it visited
        // and enqueue it
        for (i = adj[s].begin(); i != adj[s].end(); ++i)
        {
            // If this adjacent node is the destination node, then
            // return true
            if (*i == d)
                return true;

            // Else, continue to do BFS
            if (!visited[*i])
            {
                visited[*i] = true;
                queue.push_back(*i);
            }
        }
    }

    // If BFS is complete without visiting d
    return false;
}
// A BFS based function to check whether d is reachable from s.
A BFS based function to check whether d is reachable from s.
A BFS based function to check whether d is reachable from s .
bool Graph::isReachable(int s, int d)
{
    if (s == d)
      return true;
    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();
        queue.pop_front();
        for (i = adj[s].begin(); i != adj[s].end(); ++i)
        {
            if (*i == d)
                return true;
            if (!visited[*i])
            {
                visited[*i] = true;
                queue.push_back(*i);
            }
        }
    }
    return false;
}
bool Graph :: isReachable ( int s , int d ) { if ( s == d ) return true ; 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 ( ) ; queue . pop_front ( ) ; for ( i = adj [ s ] . begin ( ) ; i != adj [ s ] . end ( ) ; ++ i ) { if ( * i == d ) return true ; if ( ! visited [ * i ] ) { visited [ * i ] = true ; queue . push_back ( * i ) ; } } } return false ; }
A BFS based function to check whether d is reachable from s.
A BFS based function to check whether d is reachable from s .
// Base case // Mark all the vertices as not visited // Create a queue for BFS // Mark the current node as visited and enqueue it // it will be used to get all adjacent vertices of a vertex // Dequeue a vertex from queue and print it // Get all adjacent vertices of the dequeued vertex s // If a adjacent has not been visited, then mark it visited // and enqueue it // If this adjacent node is the destination node, then // return true // Else, continue to do BFS // If BFS is complete without visiting d