-
Notifications
You must be signed in to change notification settings - Fork 0
/
Submission code
241 lines (209 loc) · 9.38 KB
/
Submission code
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
int vertices = n;
int edges = m;
if (isComplete(vertices, edges)) { // we check if the graph is a complete graph, if it is- the chromatic number equals to the number of vertices.
System.out.println("Chromatic number = " + vertices);
} else {
int[][] matrix = new int[vertices + 1][vertices + 1]; // creating adjacency matrix based on the amount of vertices.
for (int i = 0; i < e.length; i++) {
int firstNumber = e[i].u; // the matrix is created based on the pairs from the graphs provided
int secondNumber = e[i].v;
matrix[firstNumber][secondNumber] = 1;
matrix[secondNumber][firstNumber] = 1;
}
int upperBound = upperBound(matrix);
int lowestDegree = lowestDegree(matrix, upperBound);
System.out.println("The upper bound is " + upperBound);
System.out.println("the lowest degree is " + lowestDegree);
int[] color;
color = makeColorsArray(upperBound);
int[] doneArray = new int[n]; //doneArray is the array that will contain the colorings sequence of the nodes
for (int j = 0; j < n; j++) {
if (j == 0 || j == 1) {
doneArray[j] = 1;
}
doneArray[j] = coloring(color, matrix, doneArray, j);
}
int chromaticNumber = findChromatic(doneArray);
System.out.println("The chromatic number is: " + chromaticNumber);
// in order to take advantage of the randomizer, were running the test multiple times(same as the amount of vertices in this case)
// and output the lowest chromatic number we get.
int minimumChromatic = upperBound;
for (int i = 0; i < vertices; i++) {
int chromaticRandomized = randomizedTest(matrix, n, upperBound, color);
if (chromaticRandomized < minimumChromatic) {
minimumChromatic = chromaticRandomized;
}
}
System.out.println("The chromatic number(randomized) is: " + minimumChromatic);
}
}
/*
this method takes in one @param, which is an int[] and in this case its the doneArray.
it checks the array and returns the biggest number, which will be our chromatic number.
**/
private static int findChromatic(int[] array) {
int finalReturn = 0;
for (int k = 0; k < array.length; k++) {
if (array[k] > finalReturn) {
finalReturn = array[k];
}
}
return finalReturn;
}
/*
upperBound is a method to find the upper bound for the chromatic number, it takes in an integer matrix as @param.
the method is based on the brooks theorem. which says the upper bound for the chromatic number can be at most the highest degree
of edges to one vertex + 1
**/
private static int upperBound(int[][] matrix) {
int max = 0;
int upperBound = 0;
for (int i = 0; i < matrix[0].length; i++) {
for (int j = 0; j < matrix.length; j++) {
if (matrix[i][j] == 1) {
upperBound++;
}
}
if (max < upperBound)
max = upperBound;
upperBound = 0;
}
return max + 1;
}
/*
makeColorsArray is a method that creates an array of numbers from 1 until the upper bound.
it is used later to assign numbers(colors) to each vertex.
**/
private static int[] makeColorsArray(int upperBound) {
int[] colors = new int[upperBound];
for (int i = 1; i < colors.length + 1; i++) {
colors[i - 1] = i;
}
return colors;
}
/*
the coloring method takes in 4 @params:
colors = the integer array that represents all possible colors we can assign to a vertex.
matrix = the adjacency matrix
done = the array that contains the sequence of coloring
index = the vertex we want to color
firstly we create a copy of the colors array, then backtrack and see whether our point was connected to any other
previously checked points. if it does, we find its color in the doneArray then find the same number in the colorsArray
and equaling it to zero so it used.
**/
private static int coloring(int[] colors, int[][] matrix, int[] done, int index) {
int[] array = Arrays.copyOf(colors, colors.length);
for (int j = index - 1; j > 0; j--) {
if (matrix[index][j] == 1) {
int colorToRemove = done[j];
for (int i = 0; i < array.length; i++) {
if (array[i] == colorToRemove) {
array[i] = 0;
}
}
}
}
int lowestColor = findSmallest(array);
return lowestColor;
}
/*
findSmallest takes in one integer array as @param and returns the the first number that is not a zero in the array.
we created this method to check the colors array and find the correct and appropriate coloring.
**/
private static int findSmallest(int[] copyArray) {
int smallestColor = 0;
for (int i = 0; i < copyArray.length; i++) {
if (copyArray[i] != 0) {
smallestColor = copyArray[i];
break;
}
}
if (smallestColor == 0) {
return 1;
}
return smallestColor;
}
/*
randomizedTest method takes in 4 @params
matrix = the adjacency matrix
vertices = number of vertices in the graph
color = an array of possible colors, its length is based on the upperbound
we create an ArrayList which has a length of the number of vertices,
we randomize its order using Collections.shuffle,
the we input our values into the method that actually assigns colors to each vertex
and finally we return the chromatic number using the findChromatic method
**/
private static int randomizedTest(int[][] matrix, int vertices, int upperBound, int[] color) {
color[0] = 1;
ArrayList<Integer> randomOrder = new ArrayList<Integer>(vertices + 1);
for (int i = 1; i < vertices + 1; i++) {
randomOrder.add(i);
}
Collections.shuffle(randomOrder);
int[] doneArray = new int[vertices];
for (int j = 0; j < vertices; j++) {
int randomChoosing = randomOrder.get(j);
if (j == 0) {
doneArray[j] = 1;
} else {
doneArray[j] = coloringRandom(color, matrix, doneArray, randomChoosing, randomOrder);
}
}
int chromaticNumber = findChromatic(doneArray);
return chromaticNumber;
}
/*
lowestDegree is a method to find a basic lower bound to the chromatic number.
the method takes in two @params- the adjacency matrix and upper bound.
it iterates through the matrix and finds the vertex which has the least edges connected to it
this number can be used as a simple lower bound to the chromatic number, but not in all cases
**/
private static int lowestDegree(int[][] matrix, int upperBound) {
int min = upperBound;
int lowerbound = upperBound;
int degrees = 0;
for (int i = 0; i < matrix[0].length; i++) { // based on Brooks Theorem, the upperbound can be at most the highest degree of edges +1
for (int j = 0; j < matrix.length; j++) {
if (matrix[i][j] == 1) {
degrees++;
}
}
if (lowerbound > degrees && degrees < min && degrees > 1) {
min = degrees;
}
degrees = 0;
}
return min;
}
/*
this method is almost the same as the normal coloring method, it just adds another @param which is randomOrder,
an ArrayList that contains the order of the vertex to check.
**/
private static int coloringRandom(int[] colors, int[][] matrix, int[] done, int number, ArrayList<Integer> randomOrder) {
int[] array = Arrays.copyOf(colors, colors.length);
int index = randomOrder.indexOf(number);
for (int j = index - 1; j > 0; j--) {
if (matrix[number][randomOrder.get(j)] == 1) {
int colorToRemove = done[j];
for (int i = 0; i < array.length; i++) {
if (array[i] == colorToRemove) {
array[i] = 0;
}
}
}
}
int lowestColor = findSmallest(array);
return lowestColor;
}
/*
This method checks if the graph is a complete graph or not.
it takes 2 @params- the number of vertices and the number of edges.
if this method finds the graph is complete based on our logic, it will return true.
**/
private static boolean isComplete(int vertices, int edges) {
if ((vertices * (vertices - 1)) / 2 == edges) {
return true;
}
return false;
}
}