-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraph.c
329 lines (274 loc) · 9.72 KB
/
graph.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/******************************************************************/
/** **/
/** --- GRAPH.C --- **/
/** **/
/******************************************************************/
#include "graph.h"
// Initialise le tableau des consomations à 0 ---------------------------------
void InitMatrice ( void )
{
int i, j ;
for ( i = 0 ; i < NBR_SOMMET ; i++ )
for ( j = 0 ; j < NBR_SOMMET ; j++ )
Matrice_Conso[i][j] = 0 ;
}
// Charge le fichier et flot en memoire ---------------------------------------
int ChargeFichier (char * fichier, int OPTIONS)
{
FILE *ptr; // Pointeur sur le fichier
size_t s; // Taille du nom lu
int i, j ; // deux lettres de l'alphabete
char *tmp = (char *) calloc ( 1, 50 ) ; // Nom temporaire du sommet : max 50 char
char *tmpNom ; // Pointeur vers la vraie future zone memoire du nom
if ((ptr = fopen(fichier, "r+")) == NULL) {
fprintf(stderr, "Erreur: Lors de l'ouverture du fichier\n");
return 2; // Erreur sur fichier
}
// Lit le nombre de Sommets
fscanf(ptr, "%d", &NBR_SOMMET);
if (NBR_SOMMET <= 0)
return 3;
// Alloue la memoire pour la matrice Max
Matrice_Max = (float **) malloc (sizeof (float *) * NBR_SOMMET);
for (i = 0; i < NBR_SOMMET; i++)
Matrice_Max[i] = malloc(sizeof (float) * NBR_SOMMET);
// Alloue la memoire pour la matrice Conso
Matrice_Conso = (float **) malloc (sizeof (float *) * NBR_SOMMET);
for (i = 0; i < NBR_SOMMET; i++)
Matrice_Conso[i] = malloc(sizeof (float) * NBR_SOMMET);
// Alloue memoire pour les noms des sommets
NomSommet = (char **) malloc(sizeof (char *) * NBR_SOMMET);
// Alloue memoire pour les marquage entre sommets (Qui marque qui)
Sommet = (int *) malloc(sizeof (int) * NBR_SOMMET);
SommetPlus = (int *) malloc(sizeof (int) * NBR_SOMMET);
// Lit le nom des Sommets :
for (i = 0; i < NBR_SOMMET; i++) {
fscanf(ptr, "%49s", tmp);
s = strlen(tmp);
tmpNom = (char *) calloc(1, s + 3) ;
memcpy(tmpNom, tmp, s);
tmpNom[(int) s] = '\0';
NomSommet[i] = tmpNom;
}
// Lit les consommation max entre chaque sommet :
for (i = 0; i < NBR_SOMMET; i++)
for (j = 0; j < NBR_SOMMET; j++)
fscanf(ptr, "%f", &Matrice_Max[i][j]);
// Charge les consos si il y en a :
if (OPTIONS & CONSO) {
fscanf(ptr, "%49s\n", tmp); // lit une ligne de séparation 49 char max
for (i = 0; i < NBR_SOMMET; i++)
for (j = 0; j < NBR_SOMMET; j++)
fscanf(ptr, "%f", &Matrice_Conso[i][j]);
} else {
InitMatrice () ;
}
return 0; // Ok, tout c'est bien passé
}
// Marque le sommet et l'affiche ----------------------------------------------
void MarqueSommet(int i, int vientDe, int Plus, int OPTIONS)
{
if (OPTIONS & VERB)
printf ( "\n\tMarque \033[0;32m%2s\033[0;m venant de \033[1;32m%2s\033[0;m" , NomSommet [ i ], NomSommet [ vientDe ] ) ;
Sommet[i] = vientDe; // Associe les deux sommets
SommetPlus[i] = Plus; // Plus = 1 OU Moins = 0
}
// ----------------------------------------------------------------------------
// Parcour le graphe en LARGEUR ----------------------------------------------
// ----------------------------------------------------------------------------
void Parcour_Largeur(int Som, int OPTIONS)
{
struct Liste L; // Liste des sommets à parcourir
L.prems = NULL;
L.der = NULL;
int i, LeSommet;
AjouteFin(Som , &L);
MarqueSommet(Som, Som, 1, OPTIONS);
while (L.prems) {
if (OPTIONS & G_VERB)
AfficheListe(L.prems); // Affiche la liste de sommet à parcourir
LeSommet = L.prems->Sommet; // Recupere le sommet que l'on traite
SupprimePrems(&L); // Supprime le sommet en cour de la liste
for (i = 0; i < NBR_SOMMET; i++) {
// Si le sommet n'est pas marqué ET si lien entre sommet ET possibilité de GAIN
if ((Sommet[i] == -1) && ((Matrice_Max[LeSommet][i] - Matrice_Conso[LeSommet][i] ) > 0.0)) {
MarqueSommet(i, LeSommet, 1, OPTIONS);
AjouteFin(i, &L);
}
// Si le sommet n'est pas marqué ET si lien entre sommet ET possibilité de BAISSE
else if ((Sommet[i] == -1) && (Matrice_Conso[i][LeSommet] > 0.0)) {
MarqueSommet(i, LeSommet, 0, OPTIONS);
AjouteFin(i, &L);
}
}
}
}
// -------------------------------------------------------------------------------
// Parcour le graphe en PROFFONDEUR ----------------------------------------------
// -------------------------------------------------------------------------------
void Parcour_Proffondeur(int LeSommet, int OPTIONS)
{
int i;
// Pour tout i : SI Capacité > Flot ET Sommet i non marqué ALORS
for (i = 0; i < NBR_SOMMET; i++) {
if ((Matrice_Max[LeSommet][i] > Matrice_Conso[LeSommet][i]) && (Sommet[i] == -1)) {
MarqueSommet(i, LeSommet, 1, OPTIONS); // Marque sommet i venant de LeSommet
Parcour_Proffondeur(i, OPTIONS); // Parcour sommet i
}
}
// Pour tout i : SI Flot > 0 ET Sommet i non marqué ALORS
for (i = 0; i < NBR_SOMMET; i++) {
if ((Matrice_Conso[i][LeSommet] > 0) && (Sommet[i] == -1)) {
MarqueSommet( i, LeSommet, 0, OPTIONS ); // Marque sommet i venant de LeSommet
Parcour_Proffondeur( i, OPTIONS ); // Parcour sommet i
}
}
}
// -------------------------------------------------------------------------------
// Recherche le pacour, le minimum, et si c'est une chaine augmentante -----------
// -------------------------------------------------------------------------------
int RechercheParcour(void)
{
int tmp, Min;
int i = NBR_SOMMET - 1;
// Si P n'est pas marquée, alors il n'y a plus d'optimisation possible
if (Sommet[i] == -1)
return 0;
if (SommetPlus[i])
Min = Matrice_Max[Sommet[i]][i] - Matrice_Conso[Sommet[i]][i];
else
Min = Matrice_Conso[i][Sommet[i]];
// Tant que l'on passe par un sommet marque ET pas par l'entrée
while ((Sommet[i] != -1) && (i != 0)) {
// Difference de consomation
if (SommetPlus[i])
tmp = Matrice_Max[Sommet[i]][i] - Matrice_Conso[Sommet[i]][i];
else
tmp = Matrice_Conso[i][Sommet[i]];
if (tmp < Min)
Min = tmp;
i = Sommet[i];
}
return Min;
}
// --------------------------------------------------------------------------------
// Applique l'augmentation au parcour ---------------------------------------------
// --------------------------------------------------------------------------------
void AppliqueParcour(int AjouteConso)
{
int i = NBR_SOMMET - 1;
while ((Sommet[i] != -1) && (i != 0)) {
if (Matrice_Max[Sommet[i]][i])
Matrice_Conso[Sommet[i]][i] += AjouteConso;
else
Matrice_Conso[i][Sommet[i]] -= AjouteConso;
i = Sommet[i];
}
}
// --------------------------------------------------------------------------------
// Initialise le tableau des marquages des sommets --------------------------------
// --------------------------------------------------------------------------------
void Init_Marquage(void)
{
int i;
Sommet[0] = 0;
for (i = 1; i < NBR_SOMMET; i++)
Sommet[i] = -1;
}
// ----------------------------------------------------------------------------
int main(int agc, char ** argv)
{
int NbrIteration = 1;
int Min;
int OPTIONS;
char * FileName;
FileName = LitOptions(agc, argv, &OPTIONS);
do {
/* 1 */ if (OPTIONS & VERB) {RED printf("\n( %d ) PARCOUR LE FLOT ", NbrIteration); DEFAULT_COLOR}
Init_Marquage(); // Reinitialise le marquage des sommets
if (OPTIONS & PROF)
Parcour_Proffondeur(0, OPTIONS);
else if (OPTIONS & LARG)
Parcour_Largeur(0, OPTIONS);
/* 2 */ if (OPTIONS & VERB) { RED printf("\n( %d ) RECHERCHE CHAINE AUGMENTANTE ", NbrIteration); DEFAULT_COLOR}
if (OPTIONS & VERB)
AfficheParcour(OPTIONS);
Min = RechercheParcour();
/* 3 */ if (OPTIONS & VERB) {RED printf("\n( %d ) APPLIQUE CHAINE AUGMENTANTE DE \033[1;33m%d ", NbrIteration, Min); DEFAULT_COLOR}
AppliqueParcour(Min);
if (OPTIONS & G_VERB)
AfficheConso();
NbrIteration ++;
} while (Min != 0);
if (OPTIONS & VERB) {
CYAN
printf("\n\n -----------------------------------");
printf("\n ---- RESULTAT FINAL ----");
}
DEFAULT_COLOR
AfficheConso();
AfficheTotal();
if (OPTIONS & EXPORT) {
FILE * ptr ;
if ((ptr=fopen(Change_Extention(FileName, ".res"), "w") ) != NULL)
Write_matrix_to_file(ptr, NBR_SOMMET, NomSommet, Matrice_Max, Matrice_Conso, 1, OPTIONS & CONSO);
else
exit(EXIT_FAILURE);
}
return 0;
}
// --------------------------------------------------------------------------------
// Lit les options entrées en arguments -------------------------------------------
// --------------------------------------------------------------------------------
char * LitOptions(int argc, char ** argv, int * OPTIONS)
{
int opt ;
char * NomFichier;
*OPTIONS = 0 ;
while ((opt = getopt(argc, argv, "xcVvlpf:h")) != -1) {
switch (opt) {
case 'x' :
*OPTIONS |= EXPORT;
break;
case 'c' :
*OPTIONS |= CONSO;
break;
case 'v' :
*OPTIONS |= VERB;
break;
case 'V' :
*OPTIONS |= VERB | G_VERB;
break;
case 'p' :
*OPTIONS |= PROF;
break;
case 'l' :
*OPTIONS |= LARG;
break;
case 'f' :
NomFichier = optarg;
*OPTIONS |= FICH;
break;
case 'h' :
AfficheAide();
exit(EXIT_SUCCESS);
break;
default:
fprintf(stderr, "\nErreur: Argument inconnu");
AfficheAide();
exit(EXIT_FAILURE);
}
}
// Si aucun mode de parcoure demamnde, on en fix un
if (!( *OPTIONS & 3))
*OPTIONS |= LARG;
// FICH : Si un fichier bien entre
if (!( *OPTIONS & FICH)) {
AfficheAide();
exit(EXIT_FAILURE);
}
// Chager le fichier avec les graph des conso et max
if (ChargeFichier(NomFichier, *OPTIONS))
exit(EXIT_FAILURE);
return NomFichier;
}