viewing paste isConnected | C++

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
bool isConnected(List* G, int n){
    
    if(n < 2)
    {
        return true;
    }
    bool* M = new bool[n];
    for(int i = 0; i < n; i++)
    {
        M[i] = false; //none of the nodes have been visited
    }
    int v0 = 0;
    visit(G,M,v0);
    //if there are nodes that were not visited
    //return false
    for(int i = 0; i < n; i++)
    {
        if(!M[i])
        {
            delete[] M;
            return false;
        }
    }
    delete[] M
    return true;
}
 
void visit(List* G, bool* M, int v){
    //take vertex v, mark it, and then take all it's neighbours marking and visiting them
    M[v] = true;
    for(List p = G[v]; p != nullptr; p = p->next)
    {
        int u = p->vertex;
        if(!M[u])
        {
            visit(G,M,u);
        }
    }
}
Viewed 1439 times, submitted by Streusel.