/
CIsomorfismo.cs
executable file
·283 lines (243 loc) · 8.58 KB
/
CIsomorfismo.cs
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
using System;
using System.Collections.Generic;
using System.Text;
namespace Editor_de_Gafos
{
class CIsomorfismo : CAlgoritmo
{
private CGrafo G;
private CGrafo H;
public CIsomorfismo(CGrafo grafo_1, CGrafo grafo_2): base()
{
G = grafo_1;
H = grafo_2;
}
//Principal
public bool sonIsomorficos()
{
bool band = false;
if (!G.getListaAdyacencia().Exists(hayVerticeGradoCero)
&& !H.getListaAdyacencia().Exists(hayVerticeGradoCero)
&& mismosNVyNA(G,H) && mismasRelacionesGrafo()
&& algoritmoMatrices())
{
band = true;
}
return band;
}
//Verificacion de Relaciones (basico)
public bool mismasRelacionesGrafo()
{
List<List<CVertice>> lg = new List<List<CVertice>>();
List<List<CVertice>> lh = new List<List<CVertice>>();
bool band = false;
int cont = 0;
getGrados(G);
foreach (int i in grados)
{
lg.Add(buscaVertConNVecinos(G.getListaAdyacencia(),i));
}
lg.Sort(ordenaPorlongitud);
foreach (int i in grados)
{
lh.Add(buscaVertConNVecinos(H.getListaAdyacencia(),i));
}
lh.Sort(ordenaPorlongitud);
for(int i = 0;i<lg.Count;i++)
{
if(lg[i].Count == lh[i].Count)
cont++;
}
if (cont == lg.Count && cont == lh.Count)
band = true;
return band;
}
public void getGrados(CGrafo gx) //Obtiene los grados (sin repetirse) que tiene el grafo
{
foreach (CNodoVertice cnv in gx.getListaAdyacencia())
{
if (!existeItem(grados, cnv.getVertice().getVecinos().Count))
{
grados.Add(cnv.getVertice().getVecinos().Count);
}
}
grados.Sort();
}
public bool existeItem(List<int> lint, int buscado)
{
bool band = false;
foreach (int xn in lint)
{
if (xn == buscado)
{
band = true;
break;
}
}
return band;
} //Utilizado getgrados para verificar si se repiten grados
public List<CVertice> buscaVertConNVecinos(List<CNodoVertice> lady, int n)
{
List<CVertice> verts = new List<CVertice>();
foreach (CNodoVertice cnv in lady)
{
if (cnv.getVertice().getVecinos().Count == n)
verts.Add(cnv.getVertice());
}
return verts;
}
public int ordenaPorlongitud(List<CVertice> lg, List<CVertice> lh)
{
int comp = 0;
comp = lg.Count.CompareTo(lh.Count);
return comp;
}
//Algoritmo de matrices de G y H
public bool algoritmoMatrices()
{
bool isomorficos = false,continua = true;
int colG = 0, n = G.getListaAdyacencia().Count;
CMatrizAdyacencia MG = G.getMatrizAdyacencia();
CMatrizAdyacencia MH = new CMatrizAdyacencia(n);
MH.setMatriz(copiaMatriz(H.getMatrizAdyacencia().getMatriz(), n));
while (colG < n && continua)
{
if (encuentraSemejante(MG, ref MH, colG, n))
{
if (matricesIguales(MG, MH, n))
{
isomorficos = true;
continua = false;
}
else
colG++;
}
else
continua = false;
}
if (colG == n || !matricesIguales(MG, MH,n))//Si se recorrieron todas las columnas de G
{
isomorficos = false;
}
return isomorficos;
}
public int claculaUnosFila(int[,] matriz, int fila,int n)
{
int unos = 0;
for (int columna = 0; columna < n; columna++)
{
if (matriz[fila, columna] == 1)
unos++;
}
return unos;
}
public int calculaUnosColumna(int[,] matriz, int columna,int n,ref List<int> numunosenfila)
{
int unos = 0;
for (int fila = 0; fila < n ; fila++)
{
if (matriz[fila, columna] == 1)
{
numunosenfila.Add(claculaUnosFila(matriz, fila, n));
unos++;
}
}
return unos;
}
public bool encuentraSemejante(CMatrizAdyacencia m1,ref CMatrizAdyacencia m2, int col_m1,int n)
{
int ic2 = col_m1 + 1;
bool semejante = false;
if (ic2 == n)
ic2 = 0;
while (col_m1 != ic2)
{
if ((semejante = columnaYRenglonesIguales(m1, m2, col_m1, ic2, n)))
{
aplicarIntercambioDeFilasYColumnas(ref m2, col_m1, ic2, n);
return semejante;
}
ic2++;
if (ic2 == n)
ic2 = 0;
}
return semejante;
}
public bool matricesIguales(CMatrizAdyacencia m1, CMatrizAdyacencia m2,int tam_mat)
{
bool band = true;
for (int fila = 0; fila < tam_mat && band != false; fila++)
{
for (int columna = 0; columna < tam_mat; columna++)
{
if (m1.getMatriz()[fila, columna] != m2.getMatriz()[fila, columna])
{
band = false;
break;
}
}
}
return band;
} //Valida matrices isomorficas
public bool columnaYRenglonesIguales(CMatrizAdyacencia m1, CMatrizAdyacencia m2, int col_m1, int col_m2, int n)
{
bool iguales = false;
int num_unos_columna_m1 = 0, num_unos_columna_m2 = 0;
List<int> nuef_m1 = new List<int>();
List<int> nuef_m2 = new List<int>();
num_unos_columna_m1 = calculaUnosColumna(m1.getMatriz(), col_m1, n, ref nuef_m1);
num_unos_columna_m2 = calculaUnosColumna(m2.getMatriz(), col_m2, n, ref nuef_m2);
if (num_unos_columna_m1 == num_unos_columna_m2 && igualNuef(nuef_m1,nuef_m2))
iguales = true;
return iguales;
}
public bool igualNuef(List<int> nuef1, List<int> nuef2)
{
bool band = false;
int cont = 0;
List<int> n1aux = copiaNuef(nuef1);
List<int> n2aux = copiaNuef(nuef2);
n1aux.Sort();
n2aux.Sort();
if (n1aux.Count == n2aux.Count)
{
for (int i = 0; i < n1aux.Count; i++)
{
if (n1aux[i] == n2aux[i])
cont++;
else
break;
}
}
if (cont == n1aux.Count)
band = true;
return band;
}
public List<int> copiaNuef(List<int> nuef)
{
List<int> copia = new List<int>();
foreach (int i in nuef)
{
copia.Add(i);
}
return copia;
}
public void aplicarIntercambioDeFilasYColumnas(ref CMatrizAdyacencia m, int col_m1,int col_m2, int n)
{
int[,] maux = copiaMatriz(m.getMatriz(), n);
//Cambio columnas
for (int i = 0; i < n; i++)
{
m.getMatriz()[i, col_m1] = maux[i, col_m2];
m.getMatriz()[i, col_m2] = maux[i, col_m1];
}
maux = copiaMatriz(m.getMatriz(), n);
//Cambio renglones
for (int i = 0; i < n; i++)
{
m.getMatriz()[col_m1, i] = maux[col_m2, i];
m.getMatriz()[col_m2, i] = maux[col_m1, i];
}
}
}
}