summaryrefslogtreecommitdiff
path: root/graph/bronKerbosch.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'graph/bronKerbosch.cpp')
-rw-r--r--graph/bronKerbosch.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/graph/bronKerbosch.cpp b/graph/bronKerbosch.cpp
new file mode 100644
index 0000000..caf2421
--- /dev/null
+++ b/graph/bronKerbosch.cpp
@@ -0,0 +1,24 @@
+using bits = bitset<64>;
+vector<bits> adj, cliques;
+
+void addEdge(int a, int b) {
+ if (a != b) adj[a][b] = adj[b][a] = 1;
+}
+
+void bronKerboschRec(bits R, bits P, bits X) {
+ if (!P.any() && !X.any()) {
+ cliques.push_back(R);
+ } else {
+ int q = (P | X)._Find_first();
+ bits cands = P & ~adj[q];
+ for (int i = 0; i < sz(adj); i++) if (cands[i]){
+ R[i] = 1;
+ bronKerboschRec(P & adj[i], X & adj[i], R);
+ R[i] = P[i] = 0;
+ X[i] = 1;
+}}}
+
+void bronKerbosch() {
+ cliques.clear();
+ bronKerboschRec({}, {(1ull << sz(adj)) - 1}, {});
+}