-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_Graf-Izomorf.c
282 lines (266 loc) · 5.53 KB
/
2_Graf-Izomorf.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 100
#define NUM 8
#define NUM1 5
int out[NUM1-1];
int cont=0;
int verif=0;
int iter=0;
int c_K;
int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
int choose(int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
void Combinari_aux(int vector[], int n, int r, int index,
int data[], int i,int** mat_data)
{
if (index == r) {
for (int j = 0; j < r; j++)
mat_data[cont][j]=data[j];
cont++;
return;
}
if (i >= n)
return;
data[index] = vector[i];
Combinari_aux(vector, n, r, index + 1, data, i + 1,mat_data);
Combinari_aux(vector, n, r, index, data, i + 1,mat_data);
}
void Combinari(int n, int r,int** mat_data)
{
int data[r];
int vector[n];
for(int i=0;i<n;i++)
vector[i]=i+1;
Combinari_aux(vector, n, r, 0, data, 0,mat_data);
}
// Verifica daca cele 2 grafuri sunt izomorfe cu ajutorul matricelor de adiacenta.
void testIsomorphism(int*** Vector,int*** Data,int* Vertex,int* Edges,int K,FILE* file_out,int K2,int z,int y)
{ for(;z<choose(Vertex[iter],K2);z++)
{ if(verif==1)
{
y=0;
verif=0;
}
printf("%d\n",z);
for(;y<choose(Vertex[iter+1],K2);y++)
{ printf("%d\n",y);
for(int i=0;i<K2;i++)
for(int j=0;j<K2;j++)
{
if(Vector[iter][Data[iter][z][i]-1][Data[iter][z][j]-1]==0 &&
Vector[iter+1][Data[iter+1][y][i]-1][Data[iter+1][y][j]-1]==1)
{
//printf("DA");
verif=1;
goto exit;
}
if(Vector[iter][Data[iter][z][i]-1][Data[iter][z][j]-1]==1 &&
Vector[iter+1][Data[iter+1][y][i]-1][Data[iter+1][y][j]-1]==0)
{ //printf("NU");
verif=1;
goto exit;
}
}
exit:
if(verif==0)
{
out[cont]=z;
cont++;
out[cont]=y;
cont++;
if(iter==K-2)
{
int u=0;
for(int k=0;k<K;k++)
{
for(int p=0;p<K2;p++)
fprintf(file_out,"%d ",Data[k][out[u]][p]);
fprintf(file_out,"\n");
u++;
}
}
else
{
iter++;
z=0;
testIsomorphism(Vector,Data,Vertex,Edges,K,file_out,K2,z,y);
fprintf(file_out,"-1");
}
}
}
}
}
void sort(int grad_nod[MAX], int *vertex)
{
int i=0, j, temp=0;
for(;i<(*vertex);i++)
{
j=0;
for(;j<(*vertex)-i-1;j++)
{
if(grad_nod[j]>grad_nod[j+1])
{
temp= grad_nod[j];
grad_nod[j]= grad_nod[j+1];
grad_nod[j+1]= temp;
}
}
}
return;
}
int checkdegree(int** A, int *vertex, int* grad_nod)
{
int i=0, j, degreeA, k=0;
for(;i<(*vertex);i++, k++)
{
j=0, degreeA=0;
for(;j<(*vertex);j++)
{
if(A[i][j] == 1)
degreeA++;
}
grad_nod[k]= degreeA;
}
degreeA=0;
for(k=0;k<(*vertex);k++)
{
degreeA= (degreeA + grad_nod[k]);
}
return(degreeA);
}
int isomorphic(int* grad_nod, int* degB, int *vertex)
{
int decizie=0, i=0, j=0;
for(;i<(*vertex);i++, j++)
{
if(grad_nod[i] != degB[j])
{
decizie= 1;
break;
}
}
return(decizie);
}
int main()
{
int*** Vector;
int*** Data;
int deg[NUM];
int dimension;
int Vertex[NUM1],Edges[NUM1];
FILE* f=fopen("graf.in","r");
FILE* f1=fopen("graf.out","w");
char *reader;
reader=(char *)malloc(MAX*sizeof(char));
int K,K2,V,E;
fgets(reader,MAX,f);
K=reader[0]-'0';
c_K=K;
fgets(reader,MAX,f);
K2=reader[0]-'0';
Vector=(int ***)malloc(K*sizeof(int**));
Data=(int ***)malloc(K*sizeof(int**));
for(int i=0;i<K;i++)
{
Vector[i]=(int **)malloc(NUM*sizeof(int*));
for(int j=0;j<NUM;j++)
Vector[i][j]=(int *)malloc(NUM*sizeof(int));
}
for(int i=0;i<K;i++)
for(int j=0;j<NUM;j++)
for(int k=0;k<NUM;k++)
Vector[i][j][k]=0;
int cnt=0,cnt1=0;
int cnt3=0;
while(fgets(reader,MAX,f)!=NULL)
{
if(cnt==Edges[cnt3]+1)
{
cnt1++;
cnt=0;
cnt3++;
}
if(reader[1]==' ')
{
Vertex[cnt3]=reader[0]-'0';
Edges[cnt3]=reader[2]-'0';
}
if(reader[2]==',')
{
Vector[cnt1][reader[1]-1-'0'][reader[3]-1-'0']=1;
Vector[cnt1][reader[3]-1-'0'][reader[1]-1-'0']=1;
}
cnt++;
}
for(int i=0;i<K;i++)
{
Data[i]=(int **)malloc(choose(Vertex[i],K2)*sizeof(int*));
for(int j=0;j<choose(Vertex[i],K2);j++)
Data[i][j]=(int *)malloc(K2*sizeof(int));
}
for(int j=0;j<K;j++)
{
Combinari(Vertex[j],K2,Data[j]);
cont=0;
}
testIsomorphism(Vector,Data,Vertex,Edges,K,f1,K2,0,0);
printf("Combinations\n");
for(int k=0;k<K;k++)
{
for(int i=0;i<choose(Vertex[k],K2);i++)
{
for(int j=0;j<K2;j++)
printf("%d ",Data[k][i][j]);
printf("\n");
}
printf("Another Matrix\n");
}
printf("==========\n");
printf("Vertex from input:\n");
for(int i=0;i<K;i++)
printf("%d ",Vertex[i]);
printf("\n");
printf("Edges:\n");
for(int i=0;i<K;i++)
printf("%d ",Edges[i]);
printf("\n");
printf("Adjacency Matrix:\n");
for(int i=0;i<K;i++)
{
for(int j=0;j<Vertex[i];j++)
{
for(int k=0;k<Vertex[i];k++)
printf("%d ",Vector[i][j][k]);
printf("\n");
}
printf("Another Adjacency Matrix:\n");
}
for(int i=0;i<K;i++)
{
for(int j=0;j<NUM;j++)
free(Vector[i][j]);
free(Vector[i]);
}
free(Vector);
for(int i=0;i<K;i++)
{
for(int j=0;j<choose(Vertex[i],K2);j++)
free(Data[i][j]);
free(Data[i]);
}
free(Data);
free(reader);
fclose(f);
fclose(f1);
return 0;
}