How can I list all cliques of an Undirected Graph ? (Not all maximal cliques, like the Bron-Kerbosch algorithm)
Asked
Active
Viewed 2,226 times
-1
-
I'm still searching for an algorithm. But I found some codes for the Bron-Kerbosch algorithm (but the problem, that this algorithm returns all **maximal** cliques, not all cliques) – Chadi Jun 23 '16 at 18:40
-
http://www.boost.org/doc/libs/1_46_1/boost/graph/bron_kerbosch_all_cliques.hpp – Chadi Jun 23 '16 at 18:42
-
this is not how StackOverflow work. We help you solve specific code questions, not homework-type algorithm questions. If you have specific code you tried using, and doesn't work for some reason, please share it. Otherwise, try asking at http://programmers.stackexchange.com/. – Traveling Tech Guy Jun 23 '16 at 18:46
1 Answers
0
The optimal solution is like this because in a complete graph there are 2^n cliques. Consider all subsets of nodes using a recursive function. And for each subset if all the edges are present between nodes of the subset, add 1 to your counter: (This is almost a pseudocode in C++)
int clique_counter = 0;
int n; //number of nodes in graph
//i imagine nodes are numbered from 1 to n
void f(int x, vector <int> &v){ //x is the current node
if(x == n){
bool is_clique = true;
for(int i = 0; i < v.size(); i++){
for(int j = i + 1; j < v.size(); j++){
if(there is not an edge between node v[i] and v[j]) //it can't be clique
is_clique = false;
}
}
if(is_clique == true){
clique_counter++;
}
return;
}
//if x < n
f(x + 1, v);
v.push_back(x);
f(x + 1, v);
v.pop_back();
}
int main(){
vector <int> v;
f(1, v);
cout << clique_counter << endl;
return 0;
}

A. Mashreghi
- 1,729
- 2
- 20
- 33