Skip to content
Snippets Groups Projects
Commit e8419383 authored by Nicolas Floquet's avatar Nicolas Floquet
Browse files

Correction commentaires MR graphe + Nouvelle version liste_url

parent 53de159a
No related branches found
No related tags found
1 merge request!15Floquet nicolas graphe v01
......@@ -167,6 +167,27 @@ void graphe_supprimer_arete(graphe* g, int i, int j) {
}
/** Retasse la matrice creuse contenant des -1
* @param g un pointeur vers un graphe alloué en mémoire
* @param casesVides un tableau contenant les indices des cases vides de la matrice
* @param nbCasesVides le nombre de cases vides à retasser
* O(n)
*/
void graphe_retasser(graphe* g, int* casesVides, int nbCasesVides) {
int i;
for(i = graphe_get_max(g) - 1; i >= 0 && nbCasesVides; i--) {
if(g->ligne[i] != -1) { //Pas une case vide, donc on peut l'échanger avec une case vide
g->ligne[casesVides[nbCasesVides-1]] = g->ligne[i];
g->col[casesVides[nbCasesVides-1]] = g->col[i];
nbCasesVides--;
g->ligne[i] = -1;
g->col[i] = -1;
}
}
}
/** Retire un sommet du graphe, assure qu'aucun sommet n'a d'arête allant vers ce dernier et qu'aucune arête ne part de celui ci.
* Puis les sommets supérieurs sont renommés, ex : si x = 5, 6->5, 7->6 etc
* @param g un pointeur vers un graphe alloué en mémoire
......@@ -182,6 +203,7 @@ void graphe_retirer_sommet(graphe* g, int x) {
printf("Erreur allocation graphe_retirer_sommet (tableau casesVides)\n");
return;
}
//Le tableau casesVides stockant les indices des cases vides, on suppose au départ qu'aucune case n'est vide, donc on met -1 (pas un indice)
for(i=0; i < graphe_get_max(g); i++) {
casesVides[i]--;
}
......@@ -202,17 +224,8 @@ void graphe_retirer_sommet(graphe* g, int x) {
}
g->premierVide -= nbCasesVides;
//Maintenant on a aucune trace du sommet x, mais plein de (-1 -1) où il était (ATTENTION ON PEUT ENCORE VOIR DES SOMMETS X, MAIS CE SONT LES ANCIENS SOMMETS X+1, CECI N'EST PAS UN BUG, NE PAS DÉBUGGER)
//Il faut retasser tout ça
graphe_retasser(g, casesVides, nbCasesVides);
for(i = graphe_get_max(g) - 1; i >= 0 && nbCasesVides; i--) {
if(g->ligne[i] != -1) { //Pas une case vide, donc on peut l'échanger avec une case vide
g->ligne[casesVides[nbCasesVides-1]] = g->ligne[i];
g->col[casesVides[nbCasesVides-1]] = g->col[i];
nbCasesVides--;
g->ligne[i] = -1;
g->col[i] = -1;
}
}
free(casesVides);
}
}
......@@ -268,6 +281,64 @@ int graphe_est_succ(graphe* g, int u, int v) {
}
/** Crée la matrice d'adjacence d'un graphe
* @param g un pointeur vers un graphe alloué en mémoire
* @return une matrice d'entiers, la matrice d'adjacence du graphe
* O(n)
*/
int* graphe_to_Adj_Matrix(graphe* g) {
int lim = graphe_get_plus_grand_sommet(g)+1, i;
int* matrice = (int*) calloc(sizeof(int), lim * lim);
if(!matrice) {
printf("Erreur allocation graphe_to_Adj_Matrix \n");
return NULL;
}
for(i = 0; i < g->premierVide; i++) {
matrice[g->ligne[i] * lim + g->col[i]]++;
}
return matrice;
}
/** Affiche une matrice d'adjacence
* @param matrice un pointeur vers la matrice
* @param lim, un entier qui est la taille des côtés de la matrice
* O(n)
*/
void graphe_afficher_Matrice_Adj(int* matrice, int lim) {
int v, w;
/* ligne des indices des colonnes */
printf("\t\t");
for(w = 0 ; w < lim ; w++) {
printf("%d \t", w);
}
printf("\n");
printf("\t\t");
for(w = 0 ; w < lim ; w++) {
printf("_\t");
}
printf("\n");
/* lignes de la matrice */
for(v = 0 ; v < lim ; v++) {
printf("%d\t|\t", v);
for(w = 0 ; w < lim ; w++) {
printf("%d \t", matrice[v * lim + w]);
}
printf("|\n");
}
printf("\t\t");
for(w = 0 ; w < lim ; w++) {
printf("_\t");
}
printf("\n");
}
/** Affiche le graphe sous la forme d'une matrice d'adjacence
* @param g un pointeur vers un graphe alloué en mémoire
* O(n)
......@@ -275,43 +346,17 @@ int graphe_est_succ(graphe* g, int u, int v) {
void graphe_afficher(graphe* g) {
if(g) {
printf("Graphe à %d arêtes\n", graphe_get_premier_vide(g));
int lim = graphe_get_plus_grand_sommet(g)+1, i, v, w;
int* matrice = (int*) calloc(sizeof(int), lim * lim);
if(!matrice) {
printf("Erreur allocation graphe_afficher \n");
return;
}
for(i = 0; i < g->premierVide; i++) {
matrice[g->ligne[i] * lim + g->col[i]]++;
// pas dans les 2 sens matrice[g->col[i] * lim + g->ligne[i]]++;
}
/* matrice d'adjacence faite */
/* ligne indices colonnes */
printf("\t\t");
for(w = 0 ; w < lim ; w++) {
printf("%d\t", w);
}
printf("\n");
int lim = graphe_get_plus_grand_sommet(g)+1;
printf("\t\t");
for(w = 0 ; w < lim ; w++) {
printf("_\t");
//Création de la matrice d'adjacence
int* matrice = graphe_to_Adj_Matrix(g);
if(!g) {
printf("Erreur matrice adjacence graphe_afficher\n");
return;
}
printf("\n");
//Affichage
graphe_afficher_Matrice_Adj(matrice, lim);
/* lignes de la matrice */
for(v = 0 ; v < lim ; v++) {
printf("%d\t|\t", v);
for(w = 0 ; w < lim ; w++) {
printf("%d\t", matrice[v * lim + w]);
}
printf("|\n");
}
printf("\t\t");
for(w = 0 ; w < lim ; w++) {
printf("_\t");
}
printf("\n");
free(matrice);
}
else {
......@@ -320,37 +365,74 @@ void graphe_afficher(graphe* g) {
}
/** Affiche un tableau d'un graphe, celui des lignes si choix == 0 et celui des colonnes sinon
* @param g un pointeur vers un graphe alloué en mémoire
* @param choix un entier déterminant le tableau à afficher pour le graphe donné
* O(n)
*/
void graphe_afficher_Un_Des_Tableaux(int* t, int premierVide, int taille) {
int i;
for(i = 0; i < taille; i++) {
if(i < premierVide) {
printf("| %2d ", t[i]);
}
else { //Il ne devrait y avoir que des (-1)
printf("| (%d) ", t[i]);
}
}
}
/** Affiche le graphe sous la forme des 2 tableaux le composant
* @param g un pointeur vers un graphe alloué en mémoire
* O(n)
*/
void graphe_afficher_tableaux(graphe* g) {
int i;
printf("\n======================");
printf("\nIndices\t\t");
printf("\n======================\nIndices\t\t");
for(i = 0; i < graphe_get_max(g); i++) {
printf("| %2d ", i);
}
printf("\n\nlignes :\t");
for(i = 0; i < graphe_get_max(g); i++) {
if(i < graphe_get_premier_vide(g)) {
printf("| %2d ", g->ligne[i]);
}
else { //Il ne devrait y avoir que des (-1)
printf("| (%d) ", g->ligne[i]);
}
}
graphe_afficher_Un_Des_Tableaux(g->ligne, graphe_get_premier_vide(g), graphe_get_max(g));
printf("\ncolonnes :\t");
for(i = 0; i < graphe_get_max(g); i++) {
if(i < graphe_get_premier_vide(g)) {
printf("| %2d ", g->col[i]);
graphe_afficher_Un_Des_Tableaux(g->col, graphe_get_premier_vide(g), graphe_get_max(g));
printf("\n======================\n\n");
}
/** Donne la liste des sommets du graphe, sans répétitions dans le tableau listeSommets passé en argument
* @param g un graphe alloué en mémoire
* @param flagSommet un tableau qui indique pour chacun de ces indices, si le sommet correspondant est dans le graphe
* @param listeSommets la liste des sommets du graphe, au max il y en a 2n, où n = l'indice du premierVide du graphe
* @param ptrNbSommets un pointeur vers un entier qui est le nombre de sommets distincts du graphe
* O(n)
*/
void graphe_liste_sommets_dot(graphe* g, int* flagSommet, int* listeSommets, int* ptrNbSommets) {
/*
On utilise 2 tableaux pour avoir une liste des sommets dans le graphe.
Soit n = graphe_get_premier_vide(g), il y a au max 2n sommets différents
(Si tous les sommets dans le tableau des lignes sont distincts et pointent chacun vers un sommets distinct)
Donc c'est le nombre max de sommets dans le fichier DOT.
Et les valeurs des sommets sont entre 0 et graphe_get_plus_grand_sommet(g) inclus
*/
int i, n = graphe_get_premier_vide(g);
for(i = 0; i < 2*n; i++) {
listeSommets[i]--; //-1 partout
}
for(i = 0; i < n; i++) {
if(!flagSommet[g->ligne[i]]) { //g->ligne[i] valait 0, donc le sommet n'était pas découvert
flagSommet[g->ligne[i]] = 1;
listeSommets[(*ptrNbSommets)++] = g->ligne[i];
}
else { //Il ne devrait y avoir que des (-1)
printf("| (%d) ", g->col[i]);
if(!flagSommet[g->col[i]]) { //g->col[i] valait 0, donc le sommet n'était pas découvert
flagSommet[g->col[i]] = 1;
listeSommets[(*ptrNbSommets)++] = g->col[i];
}
}
printf("\n======================");
printf("\n\n");
}
......@@ -365,18 +447,11 @@ void graphe_afficher_tableaux(graphe* g) {
int graphe_ecrire_dot(graphe* g, char* nomFichier) {
if(g) {
int i, nbSommets = 0, n = graphe_get_premier_vide(g), sommetMax = graphe_get_plus_grand_sommet(g);
int* ptrNbSommets = &nbSommets;
if(!n) {
printf("Erreur premier vide = 0, graphe vide graphe_ecrire_dot\n");
return -2;
}
/*
On utilise 2 tableaux pour avoir une liste des sommets dans le graphe.
Soit n = graphe_get_premier_vide(g), il y a au max 2n sommets différents
(Si tous les sommets dans le tableau des lignes sont distincts et pointent chacun vers un sommets distinct)
Donc c'est le nombre max de sommets dans le fichier DOT.
Et les valeurs des sommets sont entre 0 et graphe_get_plus_grand_sommet(g) inclus
*/
int* flagSommet = (int *) calloc(sizeof(int), sommetMax + 1);
/*
......@@ -402,9 +477,6 @@ int graphe_ecrire_dot(graphe* g, char* nomFichier) {
return -2;
}
for(i = 0; i < 2*n; i++) {
listeSommets[i]--; //-1 partout
}
FILE *f = fopen(nomFichier, "w");
if (!f) {
free(flagSommet);
......@@ -412,16 +484,8 @@ int graphe_ecrire_dot(graphe* g, char* nomFichier) {
printf("Erreur ouverture fichier \"%s\"graphe_ecrire_dot\n", nomFichier);
return -1;
}
for(i = 0; i < n; i++) {
if(!flagSommet[g->ligne[i]]) { //g->ligne[i] valait 0, donc le sommet n'était pas découvert
flagSommet[g->ligne[i]] = 1;
listeSommets[nbSommets++] = g->ligne[i];
}
if(!flagSommet[g->col[i]]) { //g->col[i] valait 0, donc le sommet n'était pas découvert
flagSommet[g->col[i]] = 1;
listeSommets[nbSommets++] = g->col[i];
}
}
graphe_liste_sommets_dot(g, flagSommet, listeSommets, ptrNbSommets);
//Dans flagSommet, on a tous les flags indiquant si un sommet est dans le graphe ou non
//Dans listeSommets on a la liste des sommets distincts
......
......@@ -87,6 +87,14 @@ void graphe_ajouter_arete(graphe* g, int i, int j);
void graphe_supprimer_arete(graphe* g, int i, int j);
/** Retasse la matrice creuse contenant des -1
* @param g un pointeur vers un graphe alloué en mémoire
* @param casesVides un tableau contenant les indices des cases vides de la matrice
* @param nbCasesVides le nombre de cases vides à retasser
* O(n)
*/
void graphe_retasser(graphe* g, int* casesVides, int nbCasesVides);
/** Retire un sommet du graphe, assure qu'aucun sommet n'a d'arête allant vers ce dernier et qu'aucune arête ne part de celui ci.
* Puis les sommets supérieurs sont renommés, ex : si x = 5, 6->5, 7->6 etc
* @param g un pointeur vers un graphe alloué en mémoire
......@@ -116,6 +124,22 @@ int graphe_voisins(graphe* g, int u, int v);
int graphe_est_succ(graphe* g, int u, int v);
/** Crée la matrice d'adjacence d'un graphe
* @param g un pointeur vers un graphe alloué en mémoire
* @return une matrice d'entiers, la matrice d'adjacence du graphe
* O(n)
*/
int* graphe_to_Adj_Matrix(graphe* g);
/** Affiche une matrice d'adjacence
* @param matrice un pointeur vers la matrice
* @param lim, un entier qui est la taille des côtés de la matrice
* O(n)
*/
void graphe_afficher_Matrice_Adj(int* matrice, int lim);
/** Affiche le graphe sous la forme d'une matrice d'adjacence
* @param g un pointeur vers un graphe alloué en mémoire
* O(n)
......@@ -123,6 +147,15 @@ int graphe_est_succ(graphe* g, int u, int v);
void graphe_afficher(graphe* g);
/** Affiche un tableau
* @param t un pointeur vers un tableau d'entiers
* @param premierVide un entier le nombre d'éléments non nuls
* @param taille un entier la taille du tableau
* O(n)
*/
void graphe_afficher_Un_Des_Tableaux(int* t, int premierVide, int taille);
/** Affiche le graphe sous la forme des 2 tableaux le composant
* @param g un pointeur vers un graphe alloué en mémoire
* O(n)
......@@ -130,6 +163,16 @@ void graphe_afficher(graphe* g);
void graphe_afficher_tableaux(graphe* g);
/** Donne la liste des sommets du graphe, sans répétitions dans le tableau listeSommets passé en argument pour les indiquer dans le fichier DOT
* @param g un graphe alloué en mémoire
* @param flagSommet un tableau qui indique pour chacun de ces indices, si le sommet correspondant est dans le graphe
* @param listeSommets la liste des sommets du graphe, au max il y en a 2n, où n = l'indice du premierVide du graphe
* @param ptrNbSommets un pointeur vers un entier qui est le nombre de sommets distincts du graphe
* O(n)
*/
void graphe_liste_sommets_dot(graphe* g, int* flagSommet, int* listeSommets, int* ptrNbSommets);
/** Convertit un graphe au format DOT dans un fichier
* @param g un graphe alloué en mémoire
* @param nomFichier, le nom du fichier où l'on veut écrire le graphe
......
......@@ -22,13 +22,19 @@ int main()
char* fichier3 = "graphe_3.dot";
assert(fichier3);
//Test création
creer_graphe(&g, NB_SOMMETS);
assert(g.max > 0);
assert(g.premierVide == 0);
assert(g.ligne);
assert(g.col);
//Test insertion
graphe_ajouter_arete(&g, 0, 1);
assert(graphe_est_succ(&g, 0, 1));
assert(!graphe_est_succ(&g, 1, 0));
assert(graphe_voisins(&g, 0, 1));
assert(g.ligne[0] == 0);
assert(g.col[0] == 1);
assert(g.premierVide == 1);
......@@ -36,6 +42,8 @@ int main()
graphe_afficher_tableaux(&g);
graphe_ajouter_arete(&g, 0, 9);
assert(graphe_est_succ(&g, 0, 9));
assert(graphe_voisins(&g, 0, 9));
assert(g.ligne[1] == 0);
assert(g.col[1] == 9);
assert(g.premierVide == 2);
......@@ -43,6 +51,8 @@ int main()
//graphe_afficher_tableaux(&g);
graphe_ajouter_arete(&g, 1, 3);
assert(graphe_est_succ(&g, 1, 3));
assert(graphe_voisins(&g, 1, 3));
assert(g.ligne[2] == 1);
assert(g.col[2] == 3);
assert(g.premierVide == 3);
......@@ -50,6 +60,8 @@ int main()
//graphe_afficher_tableaux(&g);
graphe_ajouter_arete(&g, 1, 2);
assert(graphe_est_succ(&g, 1, 2));
assert(graphe_voisins(&g, 1, 2));
assert(g.ligne[3] == 1);
assert(g.col[3] == 2);
assert(g.premierVide == 4);
......@@ -57,6 +69,8 @@ int main()
//graphe_afficher_tableaux(&g);
graphe_ajouter_arete(&g, 1, 4);
assert(graphe_est_succ(&g, 1, 4));
assert(graphe_voisins(&g, 1, 4));
assert(g.ligne[4] == 1);
assert(g.col[4] == 4);
assert(g.premierVide == 5);
......@@ -64,6 +78,8 @@ int main()
//graphe_afficher_tableaux(&g);
graphe_ajouter_arete(&g, 2, 4);
assert(graphe_est_succ(&g, 2, 4));
assert(graphe_voisins(&g, 2, 4));
assert(g.ligne[5] == 2);
assert(g.col[5] == 4);
assert(g.premierVide == 6);
......@@ -71,6 +87,8 @@ int main()
//graphe_afficher_tableaux(&g);
graphe_ajouter_arete(&g, 3, 4);
assert(graphe_est_succ(&g, 3, 4));
assert(graphe_voisins(&g, 3, 4));
assert(g.ligne[6] == 3);
assert(g.col[6] == 4);
assert(g.premierVide == 7);
......@@ -78,6 +96,8 @@ int main()
//graphe_afficher_tableaux(&g);
graphe_ajouter_arete(&g, 3, 5);
assert(graphe_est_succ(&g, 3, 5));
assert(graphe_voisins(&g, 3, 5));
assert(g.ligne[7] == 3);
assert(g.col[7] == 5);
assert(g.premierVide == 8);
......@@ -85,6 +105,8 @@ int main()
//graphe_afficher_tableaux(&g);
graphe_ajouter_arete(&g, 4, 5);
assert(graphe_est_succ(&g, 4, 5));
assert(graphe_voisins(&g, 4, 5));
assert(g.ligne[8] == 4);
assert(g.col[8] == 5);
assert(g.premierVide == 9);
......@@ -92,6 +114,8 @@ int main()
//graphe_afficher_tableaux(&g);
graphe_ajouter_arete(&g, 4, 6);
assert(graphe_est_succ(&g, 4, 6));
assert(graphe_voisins(&g, 4, 6));
assert(g.ligne[9] == 4);
assert(g.col[9] == 6);
assert(g.premierVide == 10);
......@@ -99,6 +123,8 @@ int main()
graphe_afficher_tableaux(&g);
graphe_ajouter_arete(&g, 5, 6);
assert(graphe_est_succ(&g, 5, 6));
assert(graphe_voisins(&g, 5, 6));
assert(g.ligne[10] == 5);
assert(g.col[10] == 6);
assert(g.premierVide == 11);
......@@ -107,6 +133,9 @@ int main()
graphe_afficher_tableaux(&g);
graphe_afficher(&g);
//Test contains
printf("Le graphe %s le sommet 9, indice : %d\n", (graphe_contains(&g, 9) ? "contient" : "ne contient pas"), graphe_find(&g, 9));
printf("On stocke le graphe tel qu'il est après tous les ajouts dans le fichier %s\n", fichier1);
......@@ -114,12 +143,14 @@ int main()
printf("Retirons le sommet 4 (un nouveau sommet 4 prendra sa place\n");
//Test retirer sommet
graphe_retirer_sommet(&g, 4);
assert(graphe_get_plus_grand_sommet(&g) == 8);
printf("On stocke le graphe tel qu'il est après la suppression du sommet 4 dans le fichier %s\n", fichier2);
graphe_ecrire_dot(&g, fichier2);
//Test retirer arêtes
printf("Retirons les arêtes 4-5, 1-3, et 5-4 (déjà retirée)\n");
graphe_supprimer_arete(&g, 4, 5);
graphe_afficher(&g);
......@@ -128,10 +159,12 @@ int main()
graphe_supprimer_arete(&g, 5, 4);
graphe_afficher(&g);
//Test voisins et est_succ
printf("Les sommets 1 et 7 %s\n", (graphe_voisins(&g, 1, 7) ? "sont voisins" : "ne sont pas voisins"));
assert(!graphe_voisins(&g, 1, 7));
printf("Ajoutons une arête 1-7\n");
graphe_ajouter_arete(&g, 1, 7);
assert(graphe_est_succ(&g, 1, 7));
printf("Les sommets 1 et 7 %s\n", (graphe_voisins(&g, 1, 7) ? "sont voisins" : "ne sont pas voisins"));
assert(graphe_voisins(&g, 1, 7));
graphe_afficher(&g);
......
......@@ -8,38 +8,46 @@
int main()
{
liste_url l;
char* google = "www.google.com";
char* pasGoogle = "www.gogol.com";
char* fichierNico = "file://C:\\Users\\Nicolas\\Homework";
char* lipn = "lipn.univ-paris13.fr/";
char* lipnPresentation = "https://lipn.univ-paris13.fr/accueil/presentation/le-laboratoire/";
creer_liste(&l, 1);
liste_add(&l, "www.google.com");
assert(!strcmp(l.urls[0], "www.google.com"));
liste_add(&l, google);
assert(!strcmp(l.urls[0], google));
liste_add(&l, "www.gogol.com");
assert(!strcmp(l.urls[1], "www.gogol.com"));
liste_add(&l, pasGoogle);
assert(!strcmp(l.urls[1], pasGoogle));
liste_add(&l, "file://C:\\Users\\Nicolas\\Homework");
assert(!strcmp(l.urls[2], "file://C:\\Users\\Nicolas\\Homework"));
liste_add(&l, fichierNico);
assert(!strcmp(l.urls[2], fichierNico));
liste_add(&l, "lipn.univ-paris13.fr/");
assert(!strcmp(l.urls[3], "lipn.univ-paris13.fr/"));
liste_add(&l, lipn);
assert(!strcmp(l.urls[3], lipn));
liste_add(&l, "https://lipn.univ-paris13.fr/accueil/presentation/le-laboratoire/");
assert(!strcmp(l.urls[4], "https://lipn.univ-paris13.fr/accueil/presentation/le-laboratoire/"));
liste_add(&l, lipnPresentation);
assert(!strcmp(l.urls[4], lipnPresentation));
liste_afficher(&l);
printf("La première URL est %s\n", liste_get_i(&l, 0));
printf("La liste %s l'URL \"www.google.com\", indice : %d\n", (liste_contains(&l, "www.google.com") ? "contient" : "ne contient pas"), liste_find(&l, "www.google.com"));
assert(liste_contains(&l, "www.google.com"));
printf("La liste %s l'URL \"www.google.com\", indice : %d\n", (liste_contains(&l, google) ? "contient" : "ne contient pas"), liste_find(&l, google));
assert(liste_contains(&l, google));
printf("Retirons la première URL, celle de google\n");
liste_remove(&l, "www.google.com");
assert(strcmp(l.urls[0], "www.google.com")); //strcmp != 0 => chaînes différentes (inférieures ou supérieures)
liste_remove(&l, google);
assert(strcmp(l.urls[0], google)); //strcmp != 0 => chaînes différentes (inférieures ou supérieures)
liste_afficher(&l);
printf("La liste %s l'URL \"www.google.com\", indice : %d\n", (liste_contains(&l, "www.google.com") ? "contient" : "ne contient pas"), liste_find(&l, "www.google.com"));
assert(!liste_contains(&l, "www.google.com"));
printf("La liste %s l'URL \"www.google.com\", indice : %d\n", (liste_contains(&l, google) ? "contient" : "ne contient pas"), liste_find(&l, google));
assert(!liste_contains(&l, google));
liste_detruire(&l);
return EXIT_SUCCESS;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment