| 348 | } |
| 349 | |
| 350 | static bool tryAddEdge(int *coloring, int *const *edgeMatrix, int vertexCount, int vertexA, int vertexB, int *coloringBuffer) { |
| 351 | static const int FIRST_POSSIBLE_COLOR[8] = { -1, 0, 1, 0, 2, 2, 1, 0 }; |
| 352 | edgeMatrix[vertexA][vertexB] = 1; |
| 353 | edgeMatrix[vertexB][vertexA] = 1; |
| 354 | if (coloring[vertexA] != coloring[vertexB]) |
| 355 | return true; |
| 356 | int bPossibleColors = vertexPossibleColors(coloring, edgeMatrix[vertexB], vertexCount); |
| 357 | if (bPossibleColors) { |
| 358 | coloring[vertexB] = FIRST_POSSIBLE_COLOR[bPossibleColors]; |
| 359 | return true; |
| 360 | } |
| 361 | memcpy(coloringBuffer, coloring, sizeof(int)*vertexCount); |
| 362 | std::queue<int> uncolored; |
| 363 | { |
| 364 | int *coloring = coloringBuffer; |
| 365 | coloring[vertexB] = FIRST_POSSIBLE_COLOR[7&~(1<<coloring[vertexA])]; |
| 366 | uncolorSameNeighbors(uncolored, coloring, edgeMatrix, vertexB, vertexCount); |
| 367 | int step = 0; |
| 368 | while (!uncolored.empty() && step < MAX_RECOLOR_STEPS) { |
| 369 | int i = uncolored.front(); |
| 370 | uncolored.pop(); |
| 371 | int possibleColors = vertexPossibleColors(coloring, edgeMatrix[i], vertexCount); |
| 372 | if (possibleColors) { |
| 373 | coloring[i] = FIRST_POSSIBLE_COLOR[possibleColors]; |
| 374 | continue; |
| 375 | } |
| 376 | do { |
| 377 | coloring[i] = step++%3; |
| 378 | } while (edgeMatrix[i][vertexA] && coloring[i] == coloring[vertexA]); |
| 379 | uncolorSameNeighbors(uncolored, coloring, edgeMatrix, i, vertexCount); |
| 380 | } |
| 381 | } |
| 382 | if (!uncolored.empty()) { |
| 383 | edgeMatrix[vertexA][vertexB] = 0; |
| 384 | edgeMatrix[vertexB][vertexA] = 0; |
| 385 | return false; |
| 386 | } |
| 387 | memcpy(coloring, coloringBuffer, sizeof(int)*vertexCount); |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | static int cmpDoublePtr(const void *a, const void *b) { |
| 392 | return sign(**reinterpret_cast<const double *const *>(a)-**reinterpret_cast<const double *const *>(b)); |
no test coverage detected