#include #include #include #include #include #include #include #include typedef struct coord{int abs; int ord;} coord; bool search (std::vector vec, int x); void ecritureNiveaux(int n, int niveau[]); void voisinsRandom(int n, int m, std::vectorvoisins[]); void afficheGraph (int n, std::vectorvoisins[]); void parcoursLargeur(int n, std::vectorvoisins[],int niveau[], int ordre[], int pere[]); void afficheArbre(int n, int pere[], int ordre[]); void pacoursProf(int n, int r, std::vector*> voisins, int debut[],int fin[], int pere[], int niveau[]); void vectortostack(int n,std::vector arch[], std::vector* > young); void afficheArbrebis(int n, int pere[]); int sommeniveau(int n, int niveau[]); using namespace std; void voisinsRandom(int n, int m, vectorvoisins[]){ srand(time(NULL)); int x=1; int y=0; for (int i = 0; i < m; i ++){ do { x = rand()%n; y = rand()%n; while (x==y){ y = rand()%n; } } while (search(voisins[x],y)); voisins[x].push_back(y); voisins[y].push_back(x); } } bool search (vector vec, int x){ int i = 0; if (vec.size()==0){ return false; } while (vec[i]!=x){ i ++; if (i>vec.size()){ return false; } } return true; } int main() { int n; //Le nombre de points. int m; // nb aretes cout << "Entrer le nombre de points: "; cin >> n; cout << "Entrer le nombre d'aretes: "; cin>>m; if (m > n*(n-1)/2){ cout<<"Nombre d'aretes trop grand, cap à : "<< n*(n-1)/2< voisins[n]; int pere[n]; int pere2[n]; int ordre[n]; int niveau[n]; int niveau2[n]; int debut[n]; int fin[n]; vector* > piledevoisins = vector< stack < int > * >(n); for (int i =0; i < n; i ++){ piledevoisins[i]=new stack; } // for (int i = 0; i < n ; i ++){ // piledevoisins[i]=new stack; // } voisinsRandom(n,m,voisins); //afficheGraph(n,voisins); parcoursLargeur(n,voisins,niveau,ordre,pere); //afficheArbre(n, pere, ordre); //ecritureNiveaux(n,niveau); cout<<"Parcours en largeur OK"<voisins[]){ for (int i =0; i < n; i ++){ int j = 0; cout<<"Sommet "<j){ cout<< " "<voisins[],int niveau[], int ordre[], int pere[]){ int dejavu[n]; for (int i =0; i < n; i ++ ){ dejavu[i]=0; niveau[i]=0; } int r=0; dejavu[r]=1; ordre[r]=1; pere[r]=r; niveau[r]=0; queue AT; AT.push(r); int t = 2; while (!AT.empty()){ int v = AT.front(); AT.pop(); for (int i =0; i < voisins[v].size();i ++){ int x = voisins[v][i]; if (!dejavu[x]){ dejavu[x]=1; AT.push(x); ordre[x]=t; t ++; pere[x]=v; niveau[x]=niveau[v]+1; } } } } void pacoursProf(int n, int r, vector* > voisins, int debut[],int fin[], int pere[] ,int niveau[]){ bool dejavu[n]; for (int i =0; i < n; i ++ ){ dejavu[i]=false; niveau[i]=0; } dejavu[r]=1; debut[r]=1; pere[r]=r; niveau[r]=0; stack AT; AT.push(r); int t = 2; while (!AT.empty()){ int x = AT.top(); if (voisins[x]->empty()){ AT.pop(); fin[x]=t; t ++; } else { int y = voisins[x]->top(); voisins[x]->pop(); if (!dejavu[y]){ dejavu[y]=true; AT.push(y); debut[y]=t; t++; pere[y]=x; niveau[y]=niveau[x]+1; cout << "Niveau de "<