|
| 1 | +/* |
| 2 | + * Bron–Kerbosch algorithm to find maximum clque in graph |
| 3 | + */ |
| 4 | + |
| 5 | +typedef struct { |
| 6 | + int size; |
| 7 | + int nodes[MAX_NODES]; |
| 8 | +} List; |
| 9 | + |
| 10 | +static void list_append(List* list, int n) |
| 11 | +{ |
| 12 | + nodes[list->size++] = n; |
| 13 | +} |
| 14 | + |
| 15 | +static void list_copy(List* dst, List* src) |
| 16 | +{ |
| 17 | + int i; |
| 18 | + int n = src->size; |
| 19 | + dst->size = n; |
| 20 | + for (i = 0; i < n; i++) { |
| 21 | + dst->nodes[i] = src->nodes[i]; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +static void findMaximumIndependentSet(List* cur, List* result, nodemask_t* graph, int* oldSet, int ne, int ce) |
| 27 | +{ |
| 28 | + int nod = 0; |
| 29 | + int minnod = ce; |
| 30 | + int fixp = -1; |
| 31 | + int s = -1; |
| 32 | + int i, j; |
| 33 | + int newSet[MAX_NODES]; |
| 34 | + |
| 35 | + for (i = 0; i < ce && minnod != 0; i++) { |
| 36 | + int p = oldSet[i]; |
| 37 | + int cnt = 0; |
| 38 | + int pos = -1; |
| 39 | + |
| 40 | + for (j = ne; j < ce; j++) { |
| 41 | + if (!BIT_CHECK(graph[p], oldSet[j])) { |
| 42 | + if (++cnt == minnod) |
| 43 | + break; |
| 44 | + pos = j; |
| 45 | + } |
| 46 | + } |
| 47 | + if (minnod > cnt) { |
| 48 | + minnod = cnt; |
| 49 | + fixp = p; |
| 50 | + if (i < ne) { |
| 51 | + s = pos; |
| 52 | + } else { |
| 53 | + s = i; |
| 54 | + nod = 1; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + for (int k = minnod + nod; k >= 1; k--) { |
| 61 | + int sel = oldSet[s]; |
| 62 | + oldSet[s] = oldSet[ne]; |
| 63 | + oldSet[ne] = sel; |
| 64 | + |
| 65 | + int newne = 0; |
| 66 | + for (int i = 0; i < ne; i++) { |
| 67 | + if (BIT_CHECK(graph[sel], oldSet[i])) { |
| 68 | + newSet[newne++] = oldSet[i]; |
| 69 | + } |
| 70 | + } |
| 71 | + int newce = newne; |
| 72 | + for (int i = ne + 1; i < ce; i++) { |
| 73 | + if (BIT_CHECK(graph[sel], oldSet[i])) { |
| 74 | + newSet[newce++] = oldSet[i]; |
| 75 | + } |
| 76 | + } |
| 77 | + list_append(cur, sel); |
| 78 | + if (newce == 0) { |
| 79 | + if (result->size < cur->size) { |
| 80 | + list_copy(result, cur); |
| 81 | + } |
| 82 | + } else if (newne < newce) { |
| 83 | + if (cur->size + newce - newne > result->size) { |
| 84 | + findMaximumIndependentSet(cur, result, graph, newSet, newne, newce); |
| 85 | + } |
| 86 | + } |
| 87 | + cur.size -= 1; |
| 88 | + if (k > 1) { |
| 89 | + for (s = ++ne; BIT_CHECK(graph[fixp], oldSet[s]); s++); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +nodemask_t MtmFindMaxClique(nodemask_t* graphs, in n_nodes); |
| 95 | +{ |
| 96 | + List tmp; |
| 97 | + List result; |
| 98 | + nodemask_t mask = 0; |
| 99 | + int all[MAX_NODES]; |
| 100 | + int i; |
| 101 | + tmp.size = 0; |
| 102 | + result.size = 0; |
| 103 | + for (i = 0; i < n_nodes; i++) { |
| 104 | + all[i]= i; |
| 105 | + } |
| 106 | + findMaximumIndependentSet(&tmp, &result, graph, all, 0, n_nodes); |
| 107 | + for (i = 0; i < result.size; i++) { |
| 108 | + mask |= (nodemask_t)1 << result.nodes[i]; |
| 109 | + } |
| 110 | + return ~mask; |
| 111 | +} |
0 commit comments