From 9fe033ea88c2f705ec18c232873d056e0c229d72 Mon Sep 17 00:00:00 2001 From: Gaspard Coulet Date: Wed, 28 Apr 2021 23:05:53 +0200 Subject: Initial commit --- sem_5/HLIN501_Graphes/TP/TP3/tp3.cc | 207 ++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 sem_5/HLIN501_Graphes/TP/TP3/tp3.cc (limited to 'sem_5/HLIN501_Graphes/TP/TP3/tp3.cc') diff --git a/sem_5/HLIN501_Graphes/TP/TP3/tp3.cc b/sem_5/HLIN501_Graphes/TP/TP3/tp3.cc new file mode 100644 index 0000000..15dfce8 --- /dev/null +++ b/sem_5/HLIN501_Graphes/TP/TP3/tp3.cc @@ -0,0 +1,207 @@ +#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[]); +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 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 profondeur 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; + } + 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[]){ + int dejavu[n]; + for (int i =0; i < n; i ++ ){ + dejavu[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]=1; + AT.push(y); + debut[y]=t; + t++; + pere[y]=x; + niveau[y]=niveau[x]+1; + cout << "Niveau de "<