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);
}
}
}