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
|
#include <cstdlib>
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
typedef struct coord{int abs; int ord;} coord;
void pointRandom(int n, coord point[]);
void distances(int n, int m, coord point[], int edge[][3]);
void tri(int m, int edge[][3]);
void affichageGraphique(int n, int m, coord point[], int arbre[][2], const char * filename);
void kruskal (int n, int edge[][3], int arbre[][2]);
void fusion(int tableau[][3],int deb1,int fin1,int fin2);
void tri_fusion(int tableau[][3],int longueur);
void tri_fusion_bis(int tableau[][3],int deb,int fin);
using namespace std;
int
main()
{
int n; //Le nombre de points.
cout << "Entrer le nombre de points: ";
cin >> n;
int m=n*(n-1)/2; // Le nombre de paires de points.
coord point[n]; // Les coordonnees des points dans le plan.
int edge[m][3]; // Les paires de points et le carre de leur longueur.
int arbre[n-1][2]; // Les aretes de l'arbre de Kruskal.
pointRandom(n,point);
distances(n,m,point,edge);
tri_fusion(edge,m);
// for (int i =0; i < m; i ++){
// cout << "point "<< edge[i][0]<<":"<<edge[i][1]<<" coords : ("<< point[edge[i][0]].abs<<";"<<point[edge[i][0]].ord <<") ; ("<<point[edge[i][1]].abs<<" ; "<<point[edge[i][1]].ord<<") norme : "<<edge[i][2]<< endl;
// }
int edge2[m][2];
for(int i =0; i < m; i ++){
edge2[i][0]=edge[i][0];
edge2[i][1]=edge[i][1];
}
affichageGraphique(n,m, point,edge2, "graphe.ps");
kruskal(n,edge,arbre);
cout<<"Kruskal OK"<<endl;
affichageGraphique(n,n-1,point,arbre, "arbre.ps");
// system("evince graphe.ps");
return EXIT_SUCCESS;
}
void pointRandom(int n, coord point[]){
srand(time(NULL));
for (int i =0; i < n; i ++){
point[i].abs = rand()%612;
point[i].ord =rand()%792;
//cout << "Created point "<<i<<" coords : "<< point[i].abs <<" : "<<point[i].ord<<endl;
}
}
void distances(int n, int m, coord point[], int edge[][3]){
int k = 0;
for (int i =0; i < n; i ++){
for(int j = i+1; j < n; j ++){
edge[k][0]=i;
edge[k][1]=j;
edge[k][2]=pow((point[i].abs - point[i].ord)-(point[j].abs - point[j].ord),2);
k++;
}
}
cout<<"calcul des distance : OK"<<endl;
}
void fusion(int tableau[][3],int deb1,int fin1,int fin2)
{
int table1[fin1-deb1+1][3];
int deb2=fin1+1;
int compt1=deb1;
int compt2=deb2;
int i;
for(i=deb1;i<=fin1;i++)
{
swap(table1[i-deb1],tableau[i]);
}
for(i=deb1;i<=fin2;i++)
{
if (compt1==deb2)
{
break;
}
else if (compt2==(fin2+1))
{
swap(tableau[i],table1[compt1-deb1]);
compt1++;
}
else if (table1[compt1-deb1][2]<tableau[compt2][2])
{
swap(tableau[i],table1[compt1-deb1]);
compt1++;
}
else
{
swap(tableau[i],tableau[compt2]);
compt2++;
}
}
}
void tri_fusion_bis(int tableau[][3],int deb,int fin)
{
if (deb!=fin)
{
int milieu=(fin+deb)/2;
tri_fusion_bis(tableau,deb,milieu);
tri_fusion_bis(tableau,milieu+1,fin);
fusion(tableau,deb,milieu,fin);
}
}
void tri_fusion(int tableau[][3],int longueur)
{
if (longueur>0)
{
tri_fusion_bis(tableau,0,longueur-1);
}
}
void tri(int m, int edge[][3]){
for (int i = 0 ; i < m ; i ++){
for(int j=i+1; j < m; j ++){
if ( edge[i][2]>edge[j][2]){
swap(edge[i][0],edge[j][0]);
swap(edge[i][1],edge[j][1]);
swap(edge[i][2],edge[j][2]);
}
}
}
}
void affichageGraphique(int n, int m, coord point[], int arbre[][2], const char * filename)
// Cree le fichier Exemple.ps qui affiche
// les points et l'arbre de Kruskal.
{
ofstream output;
output.open(filename,ios::out);
output << "%!PS-Adobe-3.0" << endl;
output << "%%BoundingBox: 0 0 612 792" << endl;
output << endl;
for(int i=0;i<n;i++)
{
output << point[i].abs << " " << point[i].ord << " 3 0 360 arc" <<endl;
output << "0 setgray" <<endl;
output << "fill" <<endl;
output << "stroke"<<endl;
output << endl;
}
output << endl;
for(int i=0;i<m;i++)
{
output << point[arbre[i][0]].abs << " " << point[arbre[i][0]].ord
<< " moveto" << endl;
output << point[arbre[i][1]].abs << " " << point[arbre[i][1]].ord
<< " lineto" << endl;
output << "stroke" << endl;
output << endl;
}
output << "showpage";
output << endl;
}
void kruskal (int n, int edge[][3], int arbre[][2]){
int comp[n];
int indicea=0;
for(int i =0; i < n-1; i ++){
arbre[i][0]=0;
arbre[i][1]=0;
}
for (int i = 0; i < n; i ++){
comp[i]=i;
}
for(int i = 0 ; i < n*(n-1)/2;i++ ){
if ( comp[edge[i][0]]!=comp[edge[i][1]]){
int aux = comp[edge[i][0]];
arbre[indicea][0]=edge[i][0];
arbre[indicea][1]=edge[i][1];
for(int j = 0; j < n; j ++){
if (comp[j]==aux){
comp[j]=comp[edge[i][1]];
}
}
indicea++;
}
}
}
|