#include<stdio.h>
#include<stdlib.h>
#define max 20
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
330
331
332
333
334
335
336
337
338
339
#define infini 1000001
 
typedef struct graphe
{
 int debut;
 int fin;
 float poid;
}graphe;
typedef graphe tab[max];
tab tree,arcs,arcs_f;
 
void init();
void saisie();
void dfs(int);
int connexe();
void prim();
void remplir();
void heapSort();
void echanger(graphe *,graphe *);
void pushdown(int,int);
void construireArbre();
void AfficherArbre();
int n,nbarc,visite[max];
float m[max][max];
 
main()
{
 int choix,test,rep;
 do
   {
     printf("\n########## - Menu - ###########\n");
     printf("\n Introduire :\n");
     printf("\n\t 1--> L'implémentation du Prim.");
     printf("\n\t 2--> L'implémentation du kruskal.");
     printf("\n\t 3--> Quitter.");
     printf("\n\n\t\t Votre choix : ");
     scanf("%d",&choix);
     while(choix!=1 && choix!=2 && choix!=3 )
      {
        printf("\n\t Introduire 1, 2 ou 3");
        printf("\n\t\t Votre choix : ");
        scanf("%d",&choix);
      }
     if(choix==3)
       exit(0);
     saisie();
     test=connexe();
     switch(choix)
      {
        case 1:
          ret_Prim:
          if(test==1)
           {
             printf("\n Voici les arcs de l\'arbre par Prim:\n");
             prim();
           }
          else
             printf("\n\n Le graphe n\'est pas connexe\n");
          break;
       case 2:
         if(test==1)
           {
             printf("\n Voici les arcs de l\'arbre par Kruskal :\n");
             ret_kruskal:
             remplir();
             heapSort();
             construireArbre();
             AfficherArbre();
           }
          else
             printf("\n\n Le graphe n\'est pas connexe\n");
         break;
      }
     printf("\n Voulez vous recommencer ?");
     printf("\n  Introduire :");
     if(choix==1)
       printf("\n\t  1--> L'implémentation du kruskal.");
     else
       printf("\n\t  1--> L'implémentation du Prim.");
     printf("\n\t  2--> Recommencer.");
     printf("\n\t  3--> Quitter.");
     printf("\n\t\t Votre choix : ");
     scanf("%d",&rep);
     while(rep!=1 && rep!=2 && rep!=3)
      {
        printf("\n\t Introduire 1 , 2 ou 3");
        printf("\n\t\t Votre choix : ");
        scanf("%d",&rep);
      }
     if(rep==1 && choix==1)
       {
         choix=2;
         goto ret_kruskal;
       }
     else
       if(rep==1 && choix==2)
         {
           choix=1;
           goto ret_Prim;
         }
   }
 while(rep!=3);
 return 0;
}
 
/*------------------------------------------------- */
void init()
{
 int i,j;
 for(i=0;i<=n-1;i++)
  visite[i]=0;
 for(i=0;i<=n-1;i++)
   for(j=0;j<=n-1;j++)
     m[i][j]=0;
}
 
/*------------------------------------------------- */
void saisie()
{
 int i,som1,som2;
 float poids;
 do
   {
     printf("\n Introduire le nombre de noeuds du graphe  "
             " (min.2 max.%d) : ",max);
     scanf("%d",&n);
   }
 while(n<2 || n>max);
 init();
 do
   {
     printf("\n Introduire le nombre d'arcs du graphe (min.1 max.%d) : "
              ,n*(n-1));
     scanf("%d",&nbarc);
   }
 while(nbarc<1 || nbarc>n*(n-1));
 for(i=0;i<=nbarc-1;i++)
   {
     arc_incorrect:
     printf("\n L\'arc n°%d :",i+1);
     printf("\n  Noeud de depart : ");
     scanf("%d",&som1);
     while(som1<0 || som1>=n)
        {
            printf("\n Cet arc est incorrect",som1,som2);
            printf("\n  Noeud de depart : ");
            scanf("%d",&som1);
        }
     printf("\n  Noeud d\'arrivé : ");
     scanf("%d",&som2);
     while(som2<0 || som2>=n)
        {
           printf("\n Cet arc est incorrect",som1,som2);
           printf("\n  Noeud d\'arrivé : ");
           scanf("%d",&som2);
        }
     if(som1==som2)
       goto arc_incorrect;
     else
       if(m[som1][som2]>0)
         {
           printf("\n L\'arc <%d,%d> existe deja",som1,som2);
           goto arc_incorrect;
         }
     printf(" Introduire le poids de l\'arc <%d,%d> (min.1 max.%ld) : "
             ,som1,som2,infini-1);
     scanf("%f",&poids);
     while(poids<=0 || poids >(infini-1))
       {
          printf(" Le poids doit être entre 0 et %ld.",infini-1);
          printf("\n Introduire un autre poids : ");
          scanf("%f",&poids);
       }
     m[som1][som2]=poids;
     m[som2][som1]=poids;
   } //fin for
}
 
/*------------------------------------------------- */
void dfs(int i)
{
 int j;
 visite[i]=1;
 for(j=0;j<=n-1;j++)
   if(m[i][j]>0 && visite[j]==0)
     dfs(j);
}
 
/*------------------------------------------------- */
int connexe()
{
 int i,val=0;
 dfs(0);
 for(i=1;i<=n-1;i++)
   if(visite[i]==0)
     {
       val=1;
       break;
     }
 if(val==1)
    return 0;
 else
    return 1;
}
 
/*------------------------------------------------- */
void prim()
{
 float low[max],min;
 int closest[max];
 int i,j,k;
 for(i=1;i<=n-1;i++)
  {
    closest[i]=0;
    low[i]=m[0][1];
  }
 for(i=1;i<=n-1;i++)
  {
    min=low[i];
    k=i;
    for(j=i+1;j<=n-1;j++)
      if(low[j]<min)
       {
         min=low[j];
         k=j;
       }
    printf("\n <%d,%d> poids %.2f",k,closest[k],low[k]);
    for(j=1;j<=n-1;j++)
      if((m[j][k]<low[j])&&(closest[j]<infini))
        {
          low[k]=m[j][k];
          closest[j]=k;
        }
    low[k]=infini;
  }
}
 
/*------------------------------------------------- */
void remplir()
{
 int i,j,y=0;
 for(i=0;i<=n-1;i++)
  for(j=i+1;j<=n-1;j++)
    if(m[i][j]>0)
      {
        arcs[y].debut=i;
        arcs[y].fin=j;
        arcs[y].poid=m[i][j];
        y++;
      }
}
 
/*------------------------------------------------- */
void echanger(graphe *x,graphe *y)
 {
  graphe temp;
  temp=*x;
  *x=*y;
  *y=temp;
 }
 
 /*------------------------------------------------- */
void heapSort()
 {
	int i,l=nbarc-1,j=0;
	for(i=(l-1)/2;i>=0;i--)
		pushdown(i,l);
	for(i=l;i>=1;i--)
	 {
		arcs_f[j++]=arcs[0];
		echanger(&arcs[0],&arcs[l]);
		l--;
		pushdown(0,l);
	 }
	arcs_f[j]=arcs[0];
 }
 
/*------------------------------------------------- */
void pushdown(int first, int last)
 {
   int r=first;
   while(r<(last+1)/2)
     {
        if((2*r)+1==last)
          {
             if(arcs[r].poid>arcs[(2*r)+1].poid)
                echanger(&arcs[r],&arcs[(2*r)+1]);
             r=last;
          }
        else
          {
if((arcs[r].poid>=arcs[(2*r)+1].poid)&&(arcs[(2*r)+1].poid<=arcs[(2*r)+2].poid))
              {
                 echanger(&arcs[r],&arcs[(2*r)+1]);
                 r=(2*r)+1;
              }
            else
if((arcs[r].poid>=arcs[(2*r)+2].poid)&&(arcs[(2*r)+2].poid<=arcs[(2*r)+1].poid))
                 {
                    echanger(&arcs[r],&arcs[(2*r)+2]);
                    r=(2*r)+2;
                 }
               else
                 r=last;
          }
     }
 }
 
/*------------------------------------------------- */
void construireArbre()
{
 int NumComp[max];
 int sommet,num1,num2,i=0,j;
 for (sommet=0;sommet<=n-1;sommet++)
   NumComp[sommet]=sommet;
 for(j=0;j<=n-1;j++)
  {
   while((i<=nbarc-1)&&(NumComp[arcs_f[i].debut]==NumComp[arcs_f[i].fin]))
       i++;
    if(i==nbarc-1)
       break;
    tree[j]=arcs_f[i];
    num1=NumComp[arcs_f[i].debut];
    num2=NumComp[arcs_f[i].fin];
    for(sommet=0;sommet<=n-1;sommet++)
       if(NumComp[sommet]==num2)
         NumComp[sommet]=num1;
  }
}
 
/*------------------------------------------------- */
void AfficherArbre()
{
 int i;
 printf("\n Voici les arêtes de l'arbre couvrant de poids minimum "
         " par KRUSKAL :\n");
 for (i=0;i<=n-2;i++)
   printf("<%d,%d>\t", tree[i].debut,tree[i].fin);
}