1 // (C) Copyright 2007-2009 Andrew Sutton
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0 (See accompanying file
5 // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_GRAPH_CLIQUE_HPP
8 #define BOOST_GRAPH_CLIQUE_HPP
12 #include <boost/config.hpp>
14 #include <boost/concept/assert.hpp>
16 #include <boost/graph/graph_concepts.hpp>
17 #include <boost/graph/lookup_edge.hpp>
19 #include <boost/concept/detail/concept_def.hpp>
22 BOOST_concept(CliqueVisitor,(Visitor)(Clique)(Graph))
24 BOOST_CONCEPT_USAGE(CliqueVisitor)
33 } /* namespace concepts */
34 using concepts::CliqueVisitorConcept;
35 } /* namespace boost */
36 #include <boost/concept/detail/concept_undef.hpp>
40 // The algorithm implemented in this paper is based on the so-called
41 // Algorithm 457, published as:
44 // author = {Coen Bron and Joep Kerbosch},
45 // title = {Algorithm 457: finding all cliques of an undirected graph},
46 // journal = {Communications of the ACM},
50 // issn = {0001-0782},
51 // pages = {575--577},
52 // doi = {http://doi.acm.org/10.1145/362342.362367},
53 // publisher = {ACM Press},
54 // address = {New York, NY, USA},
57 // Sort of. This implementation is adapted from the 1st version of the
58 // algorithm and does not implement the candidate selection optimization
59 // described as published - it could, it just doesn't yet.
61 // The algorithm is given as proportional to (3.14)^(n/3) power. This is
62 // not the same as O(...), but based on time measures and approximation.
64 // Unfortunately, this implementation may be less efficient on non-
65 // AdjacencyMatrix modeled graphs due to the non-constant implementation
66 // of the edge(u,v,g) functions.
68 // TODO: It might be worthwhile to provide functionality for passing
69 // a connectivity matrix to improve the efficiency of those lookups
70 // when needed. This could simply be passed as a BooleanMatrix
71 // s.t. edge(u,v,B) returns true or false. This could easily be
72 // abstracted for adjacency matricies.
74 // The following paper is interesting for a number of reasons. First,
75 // it lists a number of other such algorithms and second, it describes
76 // a new algorithm (that does not appear to require the edge(u,v,g)
77 // function and appears fairly efficient. It is probably worth investigating.
79 // @article{DBLP:journals/tcs/TomitaTT06,
80 // author = {Etsuji Tomita and Akira Tanaka and Haruhisa Takahashi},
81 // title = {The worst-case time complexity for generating all maximal cliques and computational experiments},
82 // journal = {Theor. Comput. Sci.},
87 // ee = {http://dx.doi.org/10.1016/j.tcs.2006.06.015}
91 * The default clique_visitor supplies an empty visitation function.
95 template <typename VertexSet, typename Graph>
96 void clique(const VertexSet&, Graph&)
101 * The max_clique_visitor records the size of the maximum clique (but not the
104 struct max_clique_visitor
106 max_clique_visitor(std::size_t& max)
110 template <typename Clique, typename Graph>
111 inline void clique(const Clique& p, const Graph& g)
113 BOOST_USING_STD_MAX();
114 maximum = max BOOST_PREVENT_MACRO_SUBSTITUTION (maximum, p.size());
116 std::size_t& maximum;
119 inline max_clique_visitor find_max_clique(std::size_t& max)
120 { return max_clique_visitor(max); }
124 template <typename Graph>
126 is_connected_to_clique(const Graph& g,
127 typename graph_traits<Graph>::vertex_descriptor u,
128 typename graph_traits<Graph>::vertex_descriptor v,
129 typename graph_traits<Graph>::undirected_category)
131 return lookup_edge(u, v, g).second;
134 template <typename Graph>
136 is_connected_to_clique(const Graph& g,
137 typename graph_traits<Graph>::vertex_descriptor u,
138 typename graph_traits<Graph>::vertex_descriptor v,
139 typename graph_traits<Graph>::directed_category)
141 // Note that this could alternate between using an || to determine
142 // full connectivity. I believe that this should produce strongly
143 // connected components. Note that using && instead of || will
144 // change the results to a fully connected subgraph (i.e., symmetric
145 // edges between all vertices s.t., if a->b, then b->a.
146 return lookup_edge(u, v, g).second && lookup_edge(v, u, g).second;
149 template <typename Graph, typename Container>
151 filter_unconnected_vertices(const Graph& g,
152 typename graph_traits<Graph>::vertex_descriptor v,
156 BOOST_CONCEPT_ASSERT(( GraphConcept<Graph> ));
158 typename graph_traits<Graph>::directed_category cat;
159 typename Container::const_iterator i, end = in.end();
160 for(i = in.begin(); i != end; ++i) {
161 if(is_connected_to_clique(g, v, *i, cat)) {
169 typename Clique, // compsub type
170 typename Container, // candidates/not type
172 void extend_clique(const Graph& g,
179 BOOST_CONCEPT_ASSERT(( GraphConcept<Graph> ));
180 BOOST_CONCEPT_ASSERT(( CliqueVisitorConcept<Visitor,Clique,Graph> ));
181 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
183 // Is there vertex in nots that is connected to all vertices
184 // in the candidate set? If so, no clique can ever be found.
185 // This could be broken out into a separate function.
187 typename Container::iterator ni, nend = nots.end();
188 typename Container::iterator ci, cend = cands.end();
189 for(ni = nots.begin(); ni != nend; ++ni) {
190 for(ci = cands.begin(); ci != cend; ++ci) {
191 // if we don't find an edge, then we're okay.
192 if(!lookup_edge(*ni, *ci, g).second) break;
194 // if we iterated all the way to the end, then *ni
195 // is connected to all *ci
196 if(ci == cend) break;
198 // if we broke early, we found *ni connected to all *ci
199 if(ni != nend) return;
202 // TODO: the original algorithm 457 describes an alternative
203 // (albeit really complicated) mechanism for selecting candidates.
204 // The given optimizaiton seeks to bring about the above
205 // condition sooner (i.e., there is a vertex in the not set
206 // that is connected to all candidates). unfortunately, the
207 // method they give for doing this is fairly unclear.
209 // basically, for every vertex in not, we should know how many
210 // vertices it is disconnected from in the candidate set. if
211 // we fix some vertex in the not set, then we want to keep
212 // choosing vertices that are not connected to that fixed vertex.
213 // apparently, by selecting fix point with the minimum number
214 // of disconnections (i.e., the maximum number of connections
215 // within the candidate set), then the previous condition wil
216 // be reached sooner.
218 // there's some other stuff about using the number of disconnects
219 // as a counter, but i'm jot really sure i followed it.
221 // TODO: If we min-sized cliques to visit, then theoretically, we
222 // should be able to stop recursing if the clique falls below that
225 // otherwise, iterate over candidates and and test
226 // for maxmimal cliquiness.
227 typename Container::iterator i, j;
228 for(i = cands.begin(); i != cands.end(); ) {
229 Vertex candidate = *i;
231 // add the candidate to the clique (keeping the iterator!)
232 // typename Clique::iterator ci = clique.insert(clique.end(), candidate);
233 clique.push_back(candidate);
235 // remove it from the candidate set
238 // build new candidate and not sets by removing all vertices
239 // that are not connected to the current candidate vertex.
240 // these actually invert the operation, adding them to the new
241 // sets if the vertices are connected. its semantically the same.
242 Container new_cands, new_nots;
243 filter_unconnected_vertices(g, candidate, cands, new_cands);
244 filter_unconnected_vertices(g, candidate, nots, new_nots);
246 if(new_cands.empty() && new_nots.empty()) {
247 // our current clique is maximal since there's nothing
248 // that's connected that we haven't already visited. If
249 // the clique is below our radar, then we won't visit it.
250 if(clique.size() >= min) {
251 vis.clique(clique, g);
255 // recurse to explore the new candidates
256 extend_clique(g, clique, new_cands, new_nots, vis, min);
259 // we're done with this vertex, so we need to move it
260 // to the nots, and remove the candidate from the clique.
261 nots.push_back(candidate);
265 } /* namespace detail */
267 template <typename Graph, typename Visitor>
269 bron_kerbosch_all_cliques(const Graph& g, Visitor vis, std::size_t min)
271 BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept<Graph> ));
272 BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph> ));
273 BOOST_CONCEPT_ASSERT(( AdjacencyMatrixConcept<Graph> )); // Structural requirement only
274 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
275 typedef typename graph_traits<Graph>::vertex_iterator VertexIterator;
276 typedef std::vector<Vertex> VertexSet;
277 typedef std::deque<Vertex> Clique;
278 BOOST_CONCEPT_ASSERT(( CliqueVisitorConcept<Visitor,Clique,Graph> ));
280 // NOTE: We're using a deque to implement the clique, because it provides
281 // constant inserts and removals at the end and also a constant size.
283 VertexIterator i, end;
284 boost::tie(i, end) = vertices(g);
285 VertexSet cands(i, end); // start with all vertices as candidates
286 VertexSet nots; // start with no vertices visited
288 Clique clique; // the first clique is an empty vertex set
289 detail::extend_clique(g, clique, cands, nots, vis, min);
292 // NOTE: By default the minimum number of vertices per clique is set at 2
293 // because singleton cliques aren't really very interesting.
294 template <typename Graph, typename Visitor>
296 bron_kerbosch_all_cliques(const Graph& g, Visitor vis)
297 { bron_kerbosch_all_cliques(g, vis, 2); }
299 template <typename Graph>
301 bron_kerbosch_clique_number(const Graph& g)
304 bron_kerbosch_all_cliques(g, find_max_clique(ret));
308 } /* namespace boost */