110 const Iterator &begin,
112 const std::function<std::vector<types::global_dof_index>(
113 const Iterator &)> &get_conflict_indices)
116 unsigned int n_iterators = 0;
119 std::unordered_map<types::global_dof_index, std::vector<Iterator>>
120 indices_to_iterators;
121 for (Iterator it = begin; it != end; ++it)
123 const std::vector<types::global_dof_index> conflict_indices =
124 get_conflict_indices(it);
125 const unsigned int n_conflict_indices = conflict_indices.size();
126 for (
unsigned int i = 0; i < n_conflict_indices; ++i)
127 indices_to_iterators[conflict_indices[i]].push_back(it);
134 std::vector<std::vector<Iterator>> zones(1,
135 std::vector<Iterator>(1, begin));
136 std::set<Iterator> used_it;
137 used_it.insert(begin);
138 while (used_it.size() != n_iterators)
143 typename std::vector<Iterator>::iterator previous_zone_it(
144 zones.back().begin());
145 typename std::vector<Iterator>::iterator previous_zone_end(
147 std::vector<Iterator> new_zone;
148 for (; previous_zone_it != previous_zone_end; ++previous_zone_it)
150 const std::vector<types::global_dof_index> conflict_indices =
151 get_conflict_indices(*previous_zone_it);
153 const unsigned int n_conflict_indices(conflict_indices.size());
154 for (
unsigned int i = 0; i < n_conflict_indices; ++i)
156 const std::vector<Iterator> &conflicting_elements =
157 indices_to_iterators[conflict_indices[i]];
158 for (
unsigned int j = 0; j < conflicting_elements.size(); ++j)
166 if ((conflicting_elements[j] != *previous_zone_it) &&
167 (used_it.count(conflicting_elements[j]) == 0))
169 new_zone.push_back(conflicting_elements[j]);
170 used_it.insert(conflicting_elements[j]);
181 if (new_zone.size() != 0)
182 zones.push_back(new_zone);
184 for (Iterator it = begin; it != end; ++it)
185 if (used_it.count(it) == 0)
187 zones.push_back(std::vector<Iterator>(1, it));
223 std::vector<Iterator> &partition,
224 const std::function<std::vector<types::global_dof_index>(
225 const Iterator &)> &get_conflict_indices,
226 std::vector<std::vector<Iterator>> &partition_coloring)
228 partition_coloring.clear();
231 const unsigned int partition_size(partition.size());
232 std::vector<unsigned int> sorted_vertices(partition_size);
233 std::vector<int> degrees(partition_size);
234 std::vector<std::vector<types::global_dof_index>> conflict_indices(
236 std::vector<std::vector<unsigned int>> graph(partition_size);
241 for (
unsigned int i = 0; i < partition_size; ++i)
243 conflict_indices[i] = get_conflict_indices(partition[i]);
244 std::sort(conflict_indices[i].begin(), conflict_indices[i].end());
249 for (
unsigned int i = 0; i < partition_size; ++i)
250 for (
unsigned int j = i + 1; j < partition_size; ++j)
254 conflict_indices[j]))
258 graph[i].push_back(j);
259 graph[j].push_back(i);
263 std::vector<int>::iterator degrees_it;
264 for (
unsigned int i = 0; i < partition_size; ++i)
267 degrees_it = std::max_element(degrees.begin(), degrees.end());
268 sorted_vertices[i] = degrees_it - degrees.begin();
274 std::vector<std::unordered_set<unsigned int>> colors_used;
275 for (
unsigned int i = 0; i < partition_size; ++i)
277 const unsigned int current_vertex(sorted_vertices[i]);
278 bool new_color(
true);
282 for (
unsigned int j = 0; j < partition_coloring.size(); ++j)
287 bool unused_color(
true);
288 for (
const auto adjacent_vertex : graph[current_vertex])
289 if (colors_used[j].count(adjacent_vertex) == 1)
291 unused_color =
false;
296 partition_coloring[j].push_back(partition[current_vertex]);
297 colors_used[j].insert(current_vertex);
305 partition_coloring.push_back(
306 std::vector<Iterator>(1, partition[current_vertex]));
307 std::unordered_set<unsigned int> tmp;
308 tmp.insert(current_vertex);
309 colors_used.push_back(tmp);
328 const std::vector<std::vector<std::vector<Iterator>>> &partition_coloring)
330 std::vector<std::vector<Iterator>> coloring;
333 const unsigned int partition_size(partition_coloring.size());
334 std::vector<std::vector<unsigned int>> colors_counter(partition_size);
335 for (
unsigned int i = 0; i < partition_size; ++i)
337 const unsigned int n_colors(partition_coloring[i].size());
338 colors_counter[i].resize(n_colors);
339 for (
unsigned int j = 0; j < n_colors; ++j)
340 colors_counter[i][j] = partition_coloring[i][j].size();
345 unsigned int i_color(0);
346 unsigned int max_even_n_colors(0);
347 const unsigned int colors_size(colors_counter.size());
348 for (
unsigned int i = 0; i < colors_size; i += 2)
350 if (max_even_n_colors < colors_counter[i].size())
352 max_even_n_colors = colors_counter[i].size();
356 coloring.resize(max_even_n_colors);
357 for (
unsigned int j = 0; j < colors_counter[i_color].size(); ++j)
358 coloring[j] = partition_coloring[i_color][j];
360 for (
unsigned int i = 0; i < partition_size; i += 2)
364 std::unordered_set<unsigned int> used_k;
365 for (
unsigned int j = 0; j < colors_counter[i].size(); ++j)
369 std::vector<unsigned int>::iterator it;
370 it = std::max_element(colors_counter[i].begin(),
371 colors_counter[i].end());
372 unsigned int min_iterators(
static_cast<unsigned int>(-1));
376 for (
unsigned int k = 0; k < max_even_n_colors; ++k)
377 if (used_k.count(k) == 0)
378 if (colors_counter[i_color][k] < min_iterators)
380 min_iterators = colors_counter[i_color][k];
383 colors_counter[i_color][pos] += *it;
385 coloring[pos].insert(
387 partition_coloring[i][it - colors_counter[i].begin()]
389 partition_coloring[i][it - colors_counter[i].begin()]
400 if (partition_size > 1)
402 unsigned int max_odd_n_colors(0);
403 for (
unsigned int i = 1; i < partition_size; i += 2)
405 if (max_odd_n_colors < colors_counter[i].size())
407 max_odd_n_colors = colors_counter[i].size();
411 coloring.resize(max_even_n_colors + max_odd_n_colors);
412 for (
unsigned int j = 0; j < colors_counter[i_color].size(); ++j)
413 coloring[max_even_n_colors + j] = partition_coloring[i_color][j];
415 for (
unsigned int i = 1; i < partition_size; i += 2)
419 std::unordered_set<unsigned int> used_k;
420 for (
unsigned int j = 0; j < colors_counter[i].size(); ++j)
424 std::vector<unsigned int>::iterator it;
425 it = std::max_element(colors_counter[i].begin(),
426 colors_counter[i].end());
427 unsigned int min_iterators(
static_cast<unsigned int>(-1));
431 for (
unsigned int k = 0; k < max_odd_n_colors; ++k)
432 if (used_k.count(k) == 0)
433 if (colors_counter[i_color][k] < min_iterators)
435 min_iterators = colors_counter[i_color][k];
438 colors_counter[i_color][pos] += *it;
441 coloring[max_even_n_colors + pos].insert(
442 coloring[max_even_n_colors + pos].end(),
443 partition_coloring[i][it - colors_counter[i].begin()]
445 partition_coloring[i][it - colors_counter[i].begin()]