Skip to content

Commit c0b94d4

Browse files
committed
added welsh-powell in c
1 parent 08f662d commit c0b94d4

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

welsh.c

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define maxV 11
5+
#define maxC 5
6+
// a b c d e f g h i j k
7+
int graph[maxV][maxV] = {
8+
{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0}, // a
9+
{1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, // b
10+
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, // c
11+
{0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1}, // d
12+
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, // e
13+
{0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0}, // f
14+
{0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1}, // g
15+
{1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1}, // h
16+
{0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0}, // i
17+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1}, // j
18+
{0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0} // k
19+
};
20+
21+
int degree[maxV]; // grau
22+
int color[maxV]; // cor dos vértices
23+
int order[maxV]; // ordem
24+
25+
void sort()
26+
{ // ordenação
27+
int i, j, aux;
28+
for (i = 0; i < maxV; i++)
29+
{
30+
for (j = maxV - 1; j > i; j--)
31+
{
32+
if (degree[order[j]] > degree[order[j - 1]])
33+
{
34+
aux = order[j];
35+
order[j] = order[j - 1];
36+
order[j - 1] = aux;
37+
}
38+
}
39+
}
40+
}
41+
42+
void init()
43+
{
44+
int i;
45+
for (i = 0; i < maxV; i++)
46+
{
47+
degree[i] = 0;
48+
}
49+
for (i = 0; i < maxV; i++)
50+
{
51+
order[i] = i;
52+
}
53+
for (i = 0; i < maxV; i++)
54+
{
55+
color[i] = 0;
56+
}
57+
}
58+
59+
void countDegree()
60+
{
61+
int i, j;
62+
init();
63+
for (i = 0; i < maxV; i++)
64+
{
65+
degree[i] = 0;
66+
}
67+
for (i = 0; i < maxV; i++)
68+
{
69+
order[i] = i;
70+
}
71+
72+
for (i = 0; i < maxV; i++)
73+
{
74+
for (j = 0; j < maxV; j++)
75+
{
76+
if (graph[i][j] == 1)
77+
degree[i]++;
78+
}
79+
}
80+
sort();
81+
}
82+
83+
void printWP()
84+
{
85+
int i;
86+
printf("Vértices em ordem de 0 a %d \n\n", maxV - 1);
87+
for (i = 0; i < maxV; i++)
88+
{
89+
printf("Vértice %d tem grau %d e a cor dele é %d\n", i, degree[i], color[i]);
90+
}
91+
printf("\n-----------------------------------------\n");
92+
printf("Vértices ordenados por grau\n\n");
93+
for (i = 0; i < maxV; i++)
94+
{
95+
printf("Vértice %d tem grau %d e a cor dele é %d\n", order[i], degree[order[i]], color[order[i]]);
96+
}
97+
}
98+
99+
void welshPowell()
100+
{
101+
int i, j, k, o;
102+
int vColor[maxC];
103+
countDegree();
104+
for (i = 0; i < maxV; i++)
105+
{ // percorrer a ordem dos vetores
106+
o = order[i];
107+
memset(vColor, 0, sizeof(vColor));
108+
for (j = 0; j < maxV; j++)
109+
{ // percorrer as adjacencias
110+
if (graph[o][j] == 1 && color[j] != 0)
111+
{ // verificar a cor do vizinho
112+
vColor[color[j]] = 1;
113+
}
114+
}
115+
k = 1;
116+
while (color[o] == 0)
117+
{
118+
if (vColor[k] == 0)
119+
{
120+
color[o] = k;
121+
}
122+
k++;
123+
}
124+
}
125+
printWP();
126+
}
127+
128+
int main()
129+
{
130+
welshPowell();
131+
return 0;
132+
}

0 commit comments

Comments
 (0)