I've been practicing my C++ algorithm knowledge, and got stuck on the standard BK implementation. The algorithm outputs way too many cliques, and I doesn't seem to figure out why. I represented the graph as an adjacency list:
vector< list<int> > adjacency_list;
My BK function looks like:
void graph::BronKerbosch(vector<int> R, vector<int> P, vector<int> X){
if (P.empty() && X.empty()){
result_cliques.insert(R);
}
for (int node : P){
vector<int> intersection = {}, intersectionX = {};
//N(P)
for (int nodeP : adjacency_list[node]){
for (int node2 : P){
if (nodeP == node2){
intersection.push_back(nodeP);
}
}
//N(X)
for (int node3 : X){
if (nodeP == node3){
intersectionX.push_back(nodeP);
}
}
}
R.push_back(node);
BronKerbosch(R,intersection,intersectionX);
P.erase(remove(P.begin(),P.end(),node),P.end());
X.push_back(node);
}
}
I call this using:
void graph::run_BronKerbosch(){
vector<int> R,P,X;
for (int i=1; i < adjacency_list.size(); i++) {
P.push_back(i);
}
BronKerbosch(R,P,X);
cout << "................\nClassic: " << result_cliques.size() << endl;
for (auto clique : result_cliques){
cout << "(";
for (int node : clique){
cout << node <<" ";
}
cout << ")\n";
}
}
I am trying to implement the basic version of the algorithm, but I seem to be missing a detail here. Is the problem in:
for (int node : P){
Should I somehow use a copy of P for this first loop? (I've seen this in a related issue)
Thank you for any help.