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

dijsktra fonctionne apres deux jours

parent 412b2244
......@@ -26,12 +26,12 @@ int * dijsktra(graph_t * graph, int noeuds, int nb_aretes, int depart)
for (int i=0; i<noeuds; i++)
tas->tab[i]=distance[i];
tas->taille=noeuds;
int sommet= depart;
ajouter_tas_min(tas,distance[depart],indice_valeur);
int i=0;
int oui=0;
int sauv;
int voisin;
affiche_graph_couple(graph,noeuds,nb_aretes);
while (nb_aretes)
{
......@@ -40,57 +40,61 @@ int * dijsktra(graph_t * graph, int noeuds, int nb_aretes, int depart)
i=0;
while (i<nb_aretes && A[i].un!=sommet && A[i].deux!=sommet)
i++;
if (A[i].un==voisin)
if (A[i].un==sommet)
voisin=A[i].deux;
else if (A[i].deux==voisin)
else if (A[i].deux==sommet)
voisin=A[i].un;
if (indice_valeur[voisin]!=-2)
if (voisin!=-1)
{
if (indice_valeur[voisin]==-1)
if (indice_valeur[voisin]!=-2)
{
distance[voisin].poids=1 + distance[sommet].poids;
ajouter_tas_min(tas,distance[voisin],indice_valeur);
parent[A[i].deux]=voisin;
A[i]=A[nb_aretes-1];
nb_aretes--;
oui=1;
}
// printf("poids :%d \n",tas->tab[indice_valeur[voisin]].poids);
else
if (distance[voisin].poids>1+distance[sommet].poids);
if (indice_valeur[voisin]==-1)
{
distance[voisin].poids=1 + distance[sommet].poids;
percolation_bas_tas_min(tas,indice_valeur[voisin],indice_valeur);
parent[A[i].deux]=voisin;
ajouter_tas_min(tas,distance[voisin],indice_valeur);
parent[voisin]=sommet;
A[i]=A[nb_aretes-1];
nb_aretes--;
voisin=-1;
}
// printf("poids :%d \n",tas->tab[indice_valeur[voisin]].poids);
else
{
if (distance[voisin].poids>1+distance[sommet].poids)
{
distance[voisin].poids=1 + distance[sommet].poids;
percolation_bas_tas_min(tas,indice_valeur[voisin],indice_valeur);
parent[voisin]=sommet;
}
A[i]=A[nb_aretes-1];
voisin=-1;
nb_aretes--;
oui=1;
}
}
}
}
if (oui)
{
oui=0;
indice_valeur[tas->tab[0].val]=indice_valeur[tas->tab[tas->taille-1].val];
indice_valeur[tas->tab[tas->taille-1].val]=-2;
indice_valeur[tas->tab[0].val]=-2;
indice_valeur[tas->tab[tas->taille].val]=0;
typetas aux=tas->tab[0];
tas->tab[0]=tas->tab[tas->taille-1];
tas->tab[tas->taille-1]=aux;
tas->tab[0]=tas->tab[tas->taille];
tas->tab[tas->taille]=aux;
tas->taille+=-1;
percolation_bas_tas_min(tas,0,indice_valeur);
sommet=tas->tab[0].val;
}
}
// affichage(tas);
/* for(int i=0; i<tas->taille; i++)
printf("%d ",tas->tab[i].poids);
//affichage(tas);
for(int i=0; i<noeuds; i++)
printf("%d ",parent[i]);
printf("\n");
printf("taille tas: %d\n",tas->taille);
printf("nb_aretes: %d\n", nb_aretes);
}
// free(distance);
// free(A);
free(distance);
free(indice_valeur);
free(A);
liberer(tas);
return parent;
}
#ifndef _gard_dijsktra_
#define _gard_dijsktra_
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include <time.h>
#include "graph_aretes_tab.h"
#include "tas.h"
int * dijsktra(graph_t *,int , int, int);
#endif
#include "tas.h"
int plusgrand(poids_t a, poids_t b)
{
return a.poids>b.poids;
}
tas_t * init_tas(int t)
{
tas_t * tas= malloc(sizeof(tas_t));
......@@ -15,12 +21,31 @@ void liberer(tas_t* tas)
free(tas);
}
tas_t * ajouter_tas_min(tas_t * tas,typetas x)
tas_t * ajouter_tas_min(tas_t * tas,typetas x, int *indice_valeur)
{
typetas aux;
tas->taille+=1;
int rang=tas->taille;
tas->tab[rang]=x;
indice_valeur[x.val]=rang;
int sauv;
while (rang>0 && plusgrand(tas->tab[(rang-1)/2],x))
{
aux=tas->tab[rang];
sauv=indice_valeur[tas->tab[rang].val];
indice_valeur[tas->tab[rang].val]=indice_valeur[tas->tab[(rang-1)/2].val];
indice_valeur[tas->tab[(rang-1)/2].val]=sauv;
tas->tab[rang]=tas->tab[(rang-1)/2];
rang=(rang-1)/2;
tas->tab[rang]=aux;
}
return tas;
}
tas_t * percolation_haut_min(tas_t * tas, int rang)
{
typetas x = tas->tab[rang];
typetas aux;
while (rang>0 && plusgrand(tas->tab[(rang-1)/2],x))
{
aux=tas->tab[rang];
......@@ -87,18 +112,22 @@ tas_t * percolation_bas_tas_max(tas_t *tas, int indice)
}
tas_t * percolation_bas_tas_min(tas_t *tas, int indice)
tas_t * percolation_bas_tas_min(tas_t *tas, int indice, int * indice_valeur)
{
typetas valeur=tas->tab[indice];
int rang=indice;
typetas aux;
int fin=1;
int sauv;
while (fin)
{
// printf("dans le while\n");
if (2*rang+1==(tas->taille) && !plusgrand(tas->tab[2*rang +1],valeur))
{
aux=tas->tab[rang];
sauv=indice_valeur[tas->tab[rang].val];
indice_valeur[tas->tab[rang].val]=indice_valeur[tas->tab[2*rang+1].val];
indice_valeur[tas->tab[2*rang+1].val]=sauv;
tas->tab[rang]=tas->tab[2*rang +1];
rang=2*rang +1;
tas->tab[rang]=aux;
......@@ -109,6 +138,9 @@ tas_t * percolation_bas_tas_min(tas_t *tas, int indice)
// printf("dans le 1er if\n");
aux=tas->tab[rang];
sauv=indice_valeur[tas->tab[rang].val];
indice_valeur[tas->tab[rang].val]=indice_valeur[tas->tab[rang*2 +1].val];
indice_valeur[tas->tab[2*rang+1].val]=sauv;
tas->tab[rang]=tas->tab[2*rang +1];
rang=2*rang +1;
tas->tab[rang]=aux;
......@@ -117,6 +149,9 @@ tas_t * percolation_bas_tas_min(tas_t *tas, int indice)
{
// printf("dans le 2er if\n");
aux=tas->tab[rang];
sauv=indice_valeur[tas->tab[rang].val];
indice_valeur[tas->tab[rang].val]=indice_valeur[tas->tab[rang*2 + 2].val];
indice_valeur[tas->tab[2*rang+2].val]=sauv;
tas->tab[rang]=tas->tab[2*rang +2];
rang=2*rang +2;
tas->tab[rang]=aux;
......@@ -127,7 +162,7 @@ tas_t * percolation_bas_tas_min(tas_t *tas, int indice)
return tas;
}
tas_t * construire_tas_min(tas_t * tas, typetas *t, int pos)
/*tas_t * construire_tas_min(tas_t * tas, typetas *t, int pos)
{
if (pos==0)
return tas;
......@@ -142,7 +177,8 @@ tas_t * construire_tas_max(tas_t * tas, typetas *t, int pos)
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);
......@@ -162,8 +198,8 @@ tas_t* tri_par_tas(typetas *t, int n)
tas->taille=n-1;
return tas;
}
void affichage(tas_t* tas)
*/
/*void affichage(tas_t* tas)
{
for (int i=0; i<=tas->taille; i++)
{
......@@ -174,7 +210,7 @@ void affichage(tas_t* tas)
}
printf("\n");
}
}*/
/*
int main()
{
......
......@@ -4,10 +4,12 @@
#include<stdio.h>
#include<stdlib.h>
#include "graph_aretes_tab.h"
typedef struct p {
int val;
int poids;
}poids_t;
typedef aretes_t typetas;
typedef poids_t typetas;
typedef struct tas
{
int taille_max;
......@@ -18,12 +20,14 @@ typedef struct tas
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 * ajouter_tas_min(tas_t * tas,typetas x,int * indice_valeur);
tas_t * percolation_haut_min(tas_t *, int rang);
tas_t * percolation_bas_tas_min(tas_t *tas, int indice,int * indice_valeur);
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 plusgrand(typetas,typetas);
/*void affichage(tas_t* tas);*/
#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