Commit f5a1c2a8 authored by Valentin MEUNIER's avatar Valentin MEUNIER
Browse files

let's gogit add kruskal.h

parent 1d3654d7
#include "graph_aretes_tab.h"
graph_t * creer_graph(int n, int nb_aretes)
{
graph_t* graph= malloc(sizeof(graph_t));
graph->liste= malloc(nb_aretes*sizeof(aretes_t));
graph->noeuds=n;
graph->nb_aretes=nb_aretes;
return graph;
}
void affiche_graph_couple(graph_t * graph,int taille, int nb_aretes)
{
FILE *fichier;
fichier=fopen("graph1.dot","w");
if (fichier==NULL)
printf("echec de louverture du fichier\n");
else
fprintf(fichier,"graph Nom{\n");
for (int i=0; i<taille; i++)
{
fprintf(fichier,"%d",i);
fprintf(fichier,";");
}
for (int j=0; j<nb_aretes; j++)
{
fprintf(fichier,"%d",graph->liste[j].un);
fprintf(fichier,"--");
fprintf(fichier,"%d",graph->liste[j].deux);
fprintf(fichier," [label=%d]",graph->liste[j].valuation);
fprintf(fichier,";");
}
fprintf(fichier, "}");
fclose(fichier);
system("dot -Tpng graph1.dot -o graph1.png");
system("display graph1.png &");
}
void affiche_graph2(graph_t * graph, partition_t * part, int classe,int taille,int nb_aretes )
{
FILE *fichier;
fichier=fopen("graph_co.dot","w");
if (fichier==NULL)
printf("echec de louverture du fichier\n");
else
fprintf(fichier,"graph Nom{\n");
int classe_cours;
for (int i=0; i<taille; i++)
{
classe_cours=recuperer_classe(i, part);
if (classe==classe_cours)
{
fprintf(fichier,"%d",i);
fprintf(fichier,";");
}
}
for (int j=0; j<nb_aretes;j++)
{
classe_cours=recuperer_classe(graph->liste[j].un, part);
if (classe==classe_cours)
{
fprintf(fichier,"%d",graph->liste[j].un);
fprintf(fichier,"--");
fprintf(fichier,"%d",graph->liste[j].deux);
fprintf(fichier," [label=%d]",graph->liste[j].valuation);
fprintf(fichier,";");
}
}
fprintf(fichier, "}");
fclose(fichier);
system("dot -Tpng graph_co.dot -o graph_co.png");
system("display graph_co.png ");
}
void partition_connexe(partition_t* part, graph_t * graph,int nb_aretes)
{
for (int j=0; j<nb_aretes;j++)
{
fusion(graph->liste[j].un,graph->liste[j].deux,part);
}
}
int main()
{
int noeuds=10;
srand(time(0));
int nb_aretes=5;
// int ** mat=NULL;
// mat= creer_mat(10);
graph_t * graph=creer_graph(noeuds,nb_aretes);
partition_t * part=creer(noeuds);
int liste_part[noeuds];
int nbpart;
/* for (int i=0; i<10 ;i++)
{
for (int j=0; j<i; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
*/
for (int i=0; i<nb_aretes; i++)
{
graph->liste[i].un=rand()%noeuds;
graph->liste[i].deux=rand()%noeuds;
graph->liste[i].valuation=rand()%noeuds;
}
affiche_graph_couple(graph,noeuds,nb_aretes);
partition_connexe(part,graph,nb_aretes);
affiche_part(part,noeuds);
system("dot -Tpng graph_part.dot -o graph_part.png");
nbpart=system("display graph_part.png &");
nbpart=lister_partition(part,10,liste_part);
for (int i=0;i<nbpart;i++)
{
affiche_graph2(graph,part,liste_part[i], noeuds,nb_aretes);
}
free(graph->liste);
free(graph);
free(part);
return 0;
}
#ifndef _gard_graph_aretes_tab_
#define _gard_graph_aretes_tab_
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include <time.h>
#include "partition.h"
typedef struct aretes
{
int un;
int deux;
int valuation;
}aretes_t;
typedef struct graph
{
int noeuds;
int nb_aretes;
aretes_t * liste;
}graph_t;
graph_t * creer_graph(int n,int);
void remplir_couple(graph_t *);
void affiche_graph_couple(graph_t *,int,int);
void partition_connexe(partition_t *,graph_t *,int);
void affiche_graph2(graph_t *,partition_t *, int, int,int);
int main();
#endif
#include "kruskal.h"
int main()
{
int noeuds=10;
srand(time(0));
int nb_aretes=10;
int c1;
int c2;
// int ** mat=NULL;
// mat= creer_mat(10);
graph_t * graph=creer_graph(noeuds,nb_aretes);
graph_t * A=creer_graph(noeuds,nb_aretes);
partition_t * part=creer(noeuds);
partition_t * partA=creer(noeuds);
// int liste_part[noeuds];
for (int i=0; i<nb_aretes; i++)
{
graph->liste[i].un=rand()%noeuds;
graph->liste[i].deux=rand()%noeuds;
graph->liste[i].valuation=rand()%noeuds;
}
affiche_graph_couple(graph,noeuds,nb_aretes);
affiche_part(part,noeuds);
graph->liste=tri_par_tas(graph->liste,nb_aretes)->tab;
int cours=0;
for (int i=0; i<nb_aretes;i++)
{
c1=recuperer_classe(graph->liste[i].un,partA);
c2=recuperer_classe(graph->liste[i].deux, partA);
if (c1!=c2)
{
A->liste[cours]=graph->liste[i];
fusion(A->liste[cours].un,A->liste[cours].deux,partA);
cours+=1;
}
}
affiche_graph_couple(A,noeuds,cours);
system("dot -Tpng graph_part.dot -o graph_part.png");
system("display graph_part.png &");
/* nbpart=lister_partition(part,10,liste_part);
for (int i=0;i<nbpart;i++)
{
affiche_graph2(graph,part,liste_part[i], noeuds,nb_aretes);
}
*/
free(graph->liste);
free(graph);
free(A->liste);
free(A);
free(part);
return 0;
}
#ifndef _gard_kruskal_
#define _gard_kruskal_
#include "tas.h"
#include "graph_aretes_tab.h"
int main();
#endif
#include "tas.h"
tas_t * init_tas(int t)
{
tas_t * tas= malloc(sizeof(tas_t));
tas->taille_max=t;
tas->taille=-1;
tas->tab= malloc(t*sizeof(typetas));
return tas;
}
void liberer(tas_t* tas)
{
free(tas->tab);
free(tas);
}
tas_t * ajouter_tas_min(tas_t * tas,typetas x)
{
typetas aux;
tas->taille+=1;
int rang=tas->taille;
tas->tab[rang]=x;
while (rang>0 && plusgrand(tas->tab[(rang-1)/2],x))
{
aux=tas->tab[rang];
tas->tab[rang]=tas->tab[(rang-1)/2];
rang=(rang-1)/2;
tas->tab[rang]=aux;
}
return tas;
}
tas_t * ajouter_tas_max(tas_t * tas,typetas x)
{
typetas aux;
tas->taille+=1;
int rang=tas->taille;
tas->tab[rang]=x;
while (rang>0 && plusgrand(x,tas->tab[(rang-1)/2]))
{
aux=tas->tab[rang];
tas->tab[rang]=tas->tab[(rang-1)/2];
rang=(rang-1)/2;
tas->tab[rang]=aux;
}
return tas;
}
tas_t * percolation_bas_tas_max(tas_t *tas, int indice)
{
typetas valeur=tas->tab[indice];
int rang=indice;
typetas aux;
int fin=1;
while (fin)
{
printf("dans le while\n");
if (2*rang+1==(tas->taille) && plusgrand(tas->tab[2*rang +1],valeur))
{
aux=tas->tab[rang];
tas->tab[rang]=tas->tab[2*rang +1];
rang=2*rang +1;
tas->tab[rang]=aux;
}
if (2*rang + 1<=(tas->taille) && plusgrand(tas->tab[2*rang +1],tas->tab[2*rang +2]) && plusgrand(tas->tab[2*rang +1],valeur))
{
printf("dans le 1er if\n");
aux=tas->tab[rang];
tas->tab[rang]=tas->tab[2*rang +1];
rang=2*rang +1;
tas->tab[rang]=aux;
}
else if (2*rang + 2<=(tas->taille) && plusgrand(tas->tab[2*rang +2],tas->tab[2*rang +1]) && plusgrand(tas->tab[2*rang +2],valeur))
{
printf("dans le 2er if\n");
aux=tas->tab[rang];
tas->tab[rang]=tas->tab[2*rang +2];
rang=2*rang +2;
tas->tab[rang]=aux;
}
else
fin=0;
}
return tas;
}
tas_t * percolation_bas_tas_min(tas_t *tas, int indice)
{
typetas valeur=tas->tab[indice];
int rang=indice;
typetas aux;
int fin=1;
while (fin)
{
printf("dans le while\n");
if (2*rang+1==(tas->taille) && !plusgrand(tas->tab[2*rang +1],valeur))
{
aux=tas->tab[rang];
tas->tab[rang]=tas->tab[2*rang +1];
rang=2*rang +1;
tas->tab[rang]=aux;
}
if (2*rang + 1<=(tas->taille) && !plusgrand(tas->tab[2*rang +1],tas->tab[2*rang +2]) && !plusgrand(tas->tab[2*rang +1],valeur))
{
printf("dans le 1er if\n");
aux=tas->tab[rang];
tas->tab[rang]=tas->tab[2*rang +1];
rang=2*rang +1;
tas->tab[rang]=aux;
}
else if (2*rang + 2<=(tas->taille) && !plusgrand(tas->tab[2*rang +2],tas->tab[2*rang +1]) && !plusgrand(tas->tab[2*rang +2],valeur))
{
printf("dans le 2er if\n");
aux=tas->tab[rang];
tas->tab[rang]=tas->tab[2*rang +2];
rang=2*rang +2;
tas->tab[rang]=aux;
}
else
fin=0;
}
return tas;
}
tas_t * construire_tas_min(tas_t * tas, typetas *t, int pos)
{
if (pos==0)
return tas;
else
return construire_tas_min(ajouter_tas_min(tas, t[pos-1]), t,pos-1);
}
tas_t * construire_tas_max(tas_t * tas, typetas *t, int pos)
{
if (pos==0)
return tas;
else
return construire_tas_max(ajouter_tas_max(tas, t[pos-1]), t,pos-1);
}
tas_t* tri_par_tas(typetas *t, int n)
{
tas_t* tas=init_tas(1000);
tas=construire_tas_max(tas,t,n);
while(tas->taille>0)
{
//printf("dans le while ");
//printf("%d\n",tas->taille);
typetas aux=tas->tab[0];
tas->tab[0]=tas->tab[tas->taille];
tas->tab[tas->taille]=aux;
affichage(tas);
tas->taille+=-1;
percolation_bas_tas_max(tas,0);
affichage(tas);
}
tas->taille=n-1;
return tas;
}
void affichage(tas_t* tas)
{
for (int i=0; i<=tas->taille; i++)
{
printf("valuation :%d ",tas->tab[i].valuation);
printf("sommet un :%d ",tas->tab[i].un);
printf("sommet deux :%d ",tas->tab[i].deux);
printf("\n");
}
printf("\n");
}
/*
int main()
{
typetas tableau[8];
tas_t* tas=init_tas(1000);
tas_t * tas2;
tableau[0].un=1, tableau[0].deux=4, tableau[0].valuation=2;
tableau[1].un=5, tableau[1].deux=1, tableau[1].valuation=3;
tableau[2].un=2, tableau[2].deux=3, tableau[2].valuation=5;
tableau[3].un=2, tableau[3].deux=3, tableau[3].valuation=7;
tableau[4].un=2, tableau[4].deux=3, tableau[4].valuation=6;
tableau[5].un=2, tableau[5].deux=3, tableau[5].valuation=4;
tableau[6].un=2, tableau[6].deux=3, tableau[6].valuation=1;
tableau[7].un=2, tableau[7].deux=3, tableau[7].valuation=0; */
/* tableau[0]=5, tableau[1]=15, tableau[2]=2, tableau[3]=7, tableau[4]=3, tableau[5]=6, tableau[6]=5, tableau[7]=65, tableau[8]=52, tableau[9]=512; */
/* tas2=tri_par_tas(tableau,8);
printf("%d\n",tas2->taille);
affichage(tas2);
}*/
#ifndef _Tri_gard_
#define _Tri_gard_
#include<stdio.h>
#include<stdlib.h>
#include "graph_aretes_tab.h"
typedef aretes_t typetas;
typedef struct tas
{
int taille_max;
typetas * tab;
int taille;
}tas_t;
tas_t * init_tas(int t);
void liberer(tas_t* tas);
tas_t * ajouter_tas_max(tas_t * tas,typetas x);
tas_t * ajouter_tas_min(tas_t * tas,typetas x);
tas_t * percolation_bas_tas_min(tas_t *tas, int indice);
tas_t * percolation_bas_tas_max(tas_t *tas, int indice);
tas_t * construire_tas_min(tas_t * tas, typetas *t, int pos);
tas_t * construir_tas_max(tas_t * tas, typetas *t, int pos);
tas_t* tri_par_tas(typetas *t ,int n);
void affichage(tas_t* tas);
int main();
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment