dijsktra.c 1.87 KB
Newer Older
Valentin MEUNIER's avatar
Valentin MEUNIER committed
1
2
3
#include "dijsktra.h"


Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
4
int * dijsktra(int ** laby, int noeuds, int depart)
Valentin MEUNIER's avatar
Valentin MEUNIER committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
	int * parent = malloc(noeuds*sizeof(int));
	parent[depart]=depart;
	
	poids_t * distance = malloc(noeuds*sizeof(poids_t));
	for (int i=0; i<noeuds; i++)
	{
		distance[i].poids=noeuds;
		distance[i].val=i;
	}
	distance[depart].poids=0;

	int* indice_valeur = malloc(noeuds*sizeof(int));
	for (int i=0; i<noeuds; i++)
		indice_valeur[i]=-1;

	tas_t * tas=init_tas(noeuds+1);
	for (int i=0; i<noeuds; i++)
		tas->tab[i]=distance[i];

Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
25
26
27
28
	int * voisin=malloc(4*sizeof(int));
	for (int i=0; i<4; i++)
		voisin[i]=-1;

Valentin MEUNIER's avatar
Valentin MEUNIER committed
29
	int sommet= depart;
30
	ajouter_tas_min(tas,distance[depart],indice_valeur);
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
31
32

	while (tas->taille>-1)
Valentin MEUNIER's avatar
Valentin MEUNIER committed
33
	{
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
34
35
36
37
38
39
40
41
		if (laby[sommet%P][sommet/P] & FLAG_N)
			voisin[0]=sommet-P;
		if (laby[sommet%P][sommet/P] & FLAG_S)
			voisin[1]=sommet+P;
		if (laby[sommet%P][sommet/P] & FLAG_O)
			voisin[2]=sommet-1;
		if (laby[sommet%P][sommet/P] & FLAG_E)
			voisin[3]=sommet+1;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
42
43
		for (int j=0; j<4; j++)
		{	
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
44
45
			
			if (voisin[j]!=-1)
Valentin MEUNIER's avatar
Valentin MEUNIER committed
46
			{
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
47
				if (indice_valeur[voisin[j]]!=-2)
Valentin MEUNIER's avatar
Valentin MEUNIER committed
48
				{
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
49
					if (indice_valeur[voisin[j]]==-1)
Valentin MEUNIER's avatar
Valentin MEUNIER committed
50
					{
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
51
52
53
						distance[voisin[j]].poids=1 + distance[sommet].poids;
						ajouter_tas_min(tas,distance[voisin[j]],indice_valeur);
						parent[voisin[j]]=sommet;
54
55
56
					}
					else
					{
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
57
						if (distance[voisin[j]].poids>1+distance[sommet].poids)
58
						{
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
59
60
61
							distance[voisin[j]].poids=1 + distance[sommet].poids;
							percolation_bas_tas_min(tas,indice_valeur[voisin[j]],indice_valeur);
							parent[voisin[j]]=sommet;
62
						}
Valentin MEUNIER's avatar
Valentin MEUNIER committed
63
					}
64
				}
Valentin MEUNIER's avatar
Valentin MEUNIER committed
65
			}
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
66
			voisin[j]=-1;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
67
		}
68
69
70
		indice_valeur[tas->tab[0].val]=-2;
		indice_valeur[tas->tab[tas->taille].val]=0;
		
Valentin MEUNIER's avatar
Valentin MEUNIER committed
71
72

		typetas aux=tas->tab[0];
73
74
75
		tas->tab[0]=tas->tab[tas->taille];
		tas->tab[tas->taille]=aux;
	
Valentin MEUNIER's avatar
Valentin MEUNIER committed
76
77
78
		tas->taille+=-1;
		percolation_bas_tas_min(tas,0,indice_valeur);
		sommet=tas->tab[0].val;
79
		
Valentin MEUNIER's avatar
Valentin MEUNIER committed
80
	}
81
82
83
	free(distance);
	free(indice_valeur);
	liberer(tas);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
84
85
	return parent;	
}