//il faut peut être faire gaffe si y'a une arete couplée qui sort de l'ensemble à fusionner ? //attention, mon implémentation génère des graphes "à trous" (ie les noeuds seront numérotés par ex 1, 2, 15, 16, ...) Graph* contracte(Graph* graph, Vecteur aContracter, int affiche) { int i; int repres = get(&aContracter, 0); Graph* gRes = recopieGraphe(graph); for(i = 0; i < size(&aContracter); i++) { int noeud = get(&aContracter, i); int j; int sz = size(&gRes->listeAdj[noeud]); for(j = 0; j < sz; j++) { int idArete = get(&gRes->listeAdj[noeud], j); Arc *arete = &gRes->aretes[idArete]; if(arete->a==noeud) arete->a=repres; if(arete->b==noeud) arete->b=repres; /*if(arete->a==repres && arete->b==repres) { sz--; swap(&gRes->listeAdj[noeud], j, sz); pop_back(&gRes->listeAdj[noeud]); j--; }*/ } } if(affiche) { int noeud; printf("Graphe contracté :\n"); printf("%d %d\n", gRes->nbNoeuds, gRes->nbAretes); for (noeud=0; noeud < gRes->nbNoeuds; noeud++) { int sz = size(&(gRes->listeAdj[noeud])); int idArete = get(&gRes->listeAdj[noeud], 0); Arc arete = gRes->aretes[idArete]; if(sz == 0 || (arete.a != noeud && arete.b != noeud)) continue; printf("noeud %d :\n", noeud); printf("%d ", sz); affiche_vecteur(&(gRes->listeAdj[noeud])); } } return gRes; }
int main() { vecteur v1,v2; int taille; v1 = lit_vecteur("vecteur1.txt"); affiche_vecteur(v1); taille = taille_vecteur(v1); v2 = allouer_vecteur(taille); my_memcpy(acces_vecteur(v2,0), acces_vecteur(v1,0),taille*sizeof(double)); my_memmove(acces_vecteur(v2,0), acces_vecteur(v2,taille/4), (taille/2)*sizeof(double)); affiche_vecteur(v2); my_memcpy(acces_vecteur(v2,0), acces_vecteur(v1,0),taille*sizeof(double)); my_memmove(acces_vecteur(v2,taille/4), acces_vecteur(v2,0), (taille/2)*sizeof(double)); affiche_vecteur(v2); liberer_vecteur(v1); liberer_vecteur(v2); printf("Difference malloc/free : %d\n",malloc_counter - free_counter); return 0; }