#include #include #define N 11 #define A (N==4?5:2*N) typedef int graphe1[N][N], graphe3[2*A], tetesuite[N+2*A]; typedef struct maillon maillon, *liste; // maillon=srtuct maillon liste=maillon* struct maillon { int s; liste suite;}; typedef liste graphe2[N]; void afft(int *t, int n) { while(n--) printf("%d ",*t++); // affiche les n premiers éléments du tableau t sur une même ligne printf("\n"); // puis passe à la ligne } void affg1(graphe1 g1) { int i,j; for(i=0;i%d ", i, j); printf("\n"); } void affg2(graphe2 g2) { int i; liste m; for(i=0;isuite) printf("%d->%d ", i, m->s); printf("\n"); } void affg3(graphe3 g3) { int k; for(k=0;k<2*A;k+=2) printf("%d->%d ", g3[k], g3[k+1]); printf("\n"); } void g1g2(graphe1 g1, graphe2 g2) { int i,j; for(i=0;is=j; m->suite=g2[i], g2[i]=m; } } void g3g2(graphe3 g3, graphe2 g2) { int i,k; for(i=0; is = g3[k+1]; m->suite = g2[i=g3[k]], g2[i] = m; } } void g3g1(graphe3 g3, graphe1 g1) { int i,j,k; for(i=0;isuite) g1[i][m->s]=1; } void g2g3(graphe2 g2, graphe3 g3) { int k=0,i; liste m; for(i=0;isuite) g3[k++]=i, g3[k++]=m->s; } void g1g3(graphe1 g1, graphe3 g3) { int k=0,i,j; for(i=0;isuite) visite(m->s); etat[s]=2; } for(i=0; iN || trouve) return; if(profondeur[s]) { int i; for(i=profondeur[s]-1; isuite) visite(m->s); profondeur[s]=N+1; p--; } int i; for(i=0; isuite, j=m->s, m->s=i, m->suite=g2[j], g2[j]=m; } void retourne3(graphe3 g3) { int i,k; for(k=0;k<2*A;k+=2) i=g3[k], g3[k]=g3[k+1], g3[k+1]=i; } int nbcfc1(graphe1 g1,int cfc[N]) { int fin[N], ifin=0, nbcfc=0; void visite1(int s) { if(cfc[s]) return; cfc[s]=-1; int t; for(t=0;t-2) return; cfc[s]=-1; int t; for(t=0;tsuite) visite1(m->s); cfc[s]=-2; fin[ifin++]=s; } void visite2(int s) { if(cfc[s]>-2) return; cfc[s]=-1; liste m; for(m=g2[s];m;m=m->suite) visite2(m->s); cfc[s]=nbcfc; } int i; for(i=0;i-2) return; cfc[s]=-1; int k; for(k=tete[s];k;k=suite[k]) visite2(g3[k]); cfc[s]=nbcfc; } int i; calcultetesuite(g3,tete); for(i=0;isuite) if(dist[j=m->s]==N) dist[fil[nbIns++]=j]=dist[i]+1; return nbIns; } int largeur3(graphe3 g3, int origine, int dist[N]) { tetesuite tete; int j, i, nbIns=0, nbEnl=0, fil[N], k, *suite=tete+N; calcultetesuite(g3,tete); for(i=0; i4) echelle3(g3), g3g1(g3,g1), g3g2(g3,g2); graphe1 h1, f1; graphe2 h2, f2; graphe3 h3, f3; printf("conversions de type\n"); g1g2(g1,h2), g1g3(g1,f3), affg1(g1), affg2(h2), affg3(f3); g2g3(g2,h3), g2g1(g2,f1), affg2(g2), affg3(h3), affg1(f1); g3g1(g3,h1), g3g2(g3,f2), affg3(g3), affg1(h1), affg2(f2); printf("retournements de graphe\n"); affg1(f1), retourne1(f1), affg1(f1), retourne1(f1), affg1(f1); affg2(f2), retourne2(f2), affg2(f2), retourne2(f2), affg2(f2); affg3(f3), retourne3(f3), affg3(f3), retourne3(f3), affg3(f3); int l, t[N], x; for(x=0;x<4;x++) { printf("\n"); retalea3(g3,x), g3g2(g3,g2), g2g1(g2,g1), affg1(g1); printf("recherche de cycle\n"); printf("%d %d : ",detecteCycle2(g2),l=chercheCycle2(g2,t)), afft(t,l); printf("tri topologique des composantes fortement connexes\n"); printf("%d : ",nbcfc1(g1,t)), afft(t,N); printf("%d : ",nbcfc2(g2,t)), afft(t,N); printf("%d : ",nbcfc3(g3,t)), afft(t,N); printf("parcours en largeur d'abord\n"); printf("%d : ",largeur1(g1,1,t)), afft(t,N); printf("%d : ",largeur2(g2,1,t)), afft(t,N); printf("%d : ",largeur3(g3,1,t)), afft(t,N); } return 0; }