I tried using Boost's implementation of the Bron-Kerbosch algorithm to save the cliques in a graph. I am able to write it to a file but not save it within my code directly in a vector of list of nodes belonging to each clique. Any help is greatly appreciated!
class Visitor {
public:
template<typename Clique, typename Graph>
void clique(const Clique& c, const Graph& g)
{
// Write the clique details to a file with vertices of each clique in a new line
std::ofstream clique_file;
clique_file.open("../output/cliques.txt", std::ios_base::app);
for (auto it = c.begin(); it!=c.end(); ++it)
clique_file << *it << " ";
clique_file << std::endl;
clique_file.close();
// Display the cliques
/*std::cout << "Clique: ";
for (auto vertex : c)
std::cout << vertex << " ";
std::cout << std::endl;*/
}
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> Graph;
Graph g;
boost::add_edge(1, 2, g);
boost::add_edge(2, 3, g);
boost::add_edge(3, 1, g);
std::ofstream clique_file;
clique_file.open("../output/cliques.txt", std::ios_base::trunc);
clique_file.close();
// Run Bron-Kerbosch algorithm to identify all cliques
Visitor visitor;
boost::bron_kerbosch_all_cliques(g, visitor, 1);
You are very close. Just store a reference to the target collection in your visitor, e.g. if defining cliques like
Define a visitor to collect them:
That's all: Live On Coliru
Printing
BONUS
Making things potentially more efficient and C++11 compatible by using
deque<V>
forClique
:Live On Coliru