-
Notifications
You must be signed in to change notification settings - Fork 1
/
CColoreado.cs
executable file
·237 lines (211 loc) · 7.51 KB
/
CColoreado.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
namespace Editor_de_Gafos
{
public class CColoreado
{
private CGrafo G;
private Color[] colores;
private int num_cromatico;
public int cont =0;
private bool band,band2;
private const int AZUL = 0,ROJO = 1,VERDE = 2,AMARILLO = 3,NARANJA = 4,CAFE = 5,MORADO = 6,ROSA = 7,AQUA = 8,NEGRO = 9;
//Constructor
public CColoreado(CGrafo grafo)
{
colores = new Color[10];
colores[AZUL]= Color.DeepSkyBlue;
colores[ROJO]= Color.Red;
colores[VERDE]= Color.Lime;
colores[AMARILLO]= Color.Yellow;
colores[NARANJA]= Color.Orange;
colores[CAFE]= Color.Peru;
colores[MORADO]= Color.DarkOrchid;
colores[ROSA]= Color.HotPink;
colores[AQUA]= Color.Aqua;
colores[NEGRO]= Color.Black;
band = band2=false;
G = grafo;
foreach (CNodoVertice cnv in G.getListaAdyacencia())
cnv.getVertice().setGradoError(cnv.getVertice().getGrado());
}
public bool hayNodoSinPintar()
{
foreach (CNodoVertice cnv in G.getListaAdyacencia())
{
if (cnv.getVertice().estaPintado() == false)
return true;
}
return false;
}
public CVertice getNodoDeMayorGradoDeErrorSinPintar()
{
CVertice encontrado = null;
int mayorGE = 0;
foreach (CNodoVertice cnv in G.getListaAdyacencia())
{
if (!cnv.getVertice().estaPintado() && cnv.getVertice().getGradoError() >= mayorGE)
{
mayorGE = cnv.getVertice().getGrado();
encontrado = cnv.getVertice();
}
}
return encontrado;
}
public bool hayColorDisponibe(CVertice vertice,ref int color)
{
bool band = false;
for (int colori = AZUL; colori < NARANJA; colori++)
{
if (!hayVecinoConColor(vertice, colori))
{
color = colori;
band = true;
break;
}
}
return band;
}
public bool hayColorDisponibeNColores(CVertice vertice, ref int color)
{
bool band = false;
for (int colori = AZUL; colori < NEGRO; colori++)
{
if (!hayVecinoConColor(vertice, colori))
{
color = colori;
band = true;
break;
}
}
return band;
}
public void pintaNodo4Colores(CVertice vertice,int color_asignado)
{
if (num_cromatico < (color_asignado + 1))
num_cromatico++;
vertice.setRelleno(colores[color_asignado].ToArgb());
vertice.setPintado(true);
} //Con teorema de los 4 colores
public void despintarNodosAdyacentesA(CVertice vertice)
{
foreach(CVertice v in vertice.getVecinos())
{
v.setRelleno(Color.LightGoldenrodYellow.ToArgb());
v.setPintado(false);
}
}
public bool hayVecinoConColor(CVertice vertice, int color)
{
foreach (CVertice vecino in vertice.getVecinos())
{
if (vecino.estaPintado())
{
if (vecino.getArgbRelleno() == colores[color].ToArgb())
return true;
}
}
return false;
}
public void coloreoDeGrafoNColores(TabPage tp)
{
if (hayNodoSinPintar() && !band2)
{
CVertice vert_mge = getNodoDeMayorGradoDeErrorSinPintar();
if (vert_mge != null)
{
int color = -1;
if (hayColorDisponibeNColores(vert_mge, ref color) && color != -1)
{
pintaNodo4Colores(vert_mge, color);
coloreoDeGrafoNColores(tp);
}
else
{
band2 = true;
MessageBox.Show("\n El Grafo " + G.getId().ToString() + " ha sobrepasado el limite de colores disponible (10)...",
"Número cromatico arbitrario!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
public void coloreoDeGrafo4Colores(TabPage tp)
{
if (hayNodoSinPintar() && !band)
{
CVertice vert_mge = getNodoDeMayorGradoDeErrorSinPintar();
if (vert_mge != null)
{
int color = -1;
if (hayColorDisponibe(vert_mge, ref color) && color != -1)
pintaNodo4Colores(vert_mge, color);
else
{
if (vert_mge.getVecinos().Count != G.getNumeroVertices() - 1)
{
vert_mge.aumentaGE();
despintarNodosAdyacentesA(vert_mge);
}
else
band = true;
}
coloreoDeGrafo4Colores(tp);
}
}
else
{
if (hayNodoSinPintar())
coloreoDeGrafoNColores(tp);
G.dibujate(tp, G.getBMP());
}
}
public bool esElUltimoEnPintar(CVertice v)
{
foreach (CNodoVertice cnv in G.getListaAdyacencia())
{
if (cnv.getVertice().getId() != v.getId() && !v.estaPintado())
return false;
}
return true;
}
public void pintaNodoNColores(CVertice vertice)
{
int color_asignado = AZUL;
foreach (CVertice vecino in vertice.getVecinos())
{
if (vecino.estaPintado())
{
if (vecino.getArgbRelleno() == colores[color_asignado].ToArgb())
{
if (color_asignado != NEGRO)
{
color_asignado++;
}
}
}
}
vertice.setRelleno(colores[color_asignado].ToArgb());
vertice.setPintado(true);
} //General numero cromatico arbitrario
public void regresaAEstadoOriginal()
{
foreach (CNodoVertice cnv in G.getListaAdyacencia())
{
cnv.getVertice().setPintado(false);
cnv.getVertice().setRelleno(Color.LightGoldenrodYellow.ToArgb());
}
}
//Setters Getters
public void setNC(int nc)
{
num_cromatico = nc;
}
public int getNC()
{
return num_cromatico;
}
}
}