1// (C) Copyright 2007-2009 Andrew Sutton
2//
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)
6
7#ifndef BOOST_GRAPH_CLIQUE_HPP
8#define BOOST_GRAPH_CLIQUE_HPP
9
10#include <vector>
11#include <deque>
12#include <boost/config.hpp>
13
14#include <boost/concept/assert.hpp>
15
16#include <boost/graph/graph_concepts.hpp>
17#include <boost/graph/lookup_edge.hpp>
18
19#include <boost/concept/detail/concept_def.hpp>
20namespace boost {
21 namespace concepts {
22 BOOST_concept(CliqueVisitor,(Visitor)(Clique)(Graph))
23 {
24 BOOST_CONCEPT_USAGE(CliqueVisitor)
25 {
26 vis.clique(k, g);
27 }
28 private:
29 Visitor vis;
30 Graph g;
31 Clique k;
32 };
33 } /* namespace concepts */
34using concepts::CliqueVisitorConcept;
35} /* namespace boost */
36#include <boost/concept/detail/concept_undef.hpp>
37
38namespace boost
39{
40// The algorithm implemented in this paper is based on the so-called
41// Algorithm 457, published as:
42//
43// @article{362367,
44// author = {Coen Bron and Joep Kerbosch},
45// title = {Algorithm 457: finding all cliques of an undirected graph},
46// journal = {Communications of the ACM},
47// volume = {16},
48// number = {9},
49// year = {1973},
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},
55// }
56//
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.
60//
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.
63//
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.
67//
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.
73//
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.
78//
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.},
83// volume = {363},
84// number = {1},
85// year = {2006},
86// pages = {28-42}
87// ee = {http://dx.doi.org/10.1016/j.tcs.2006.06.015}
88// }
89
90/**
91 * The default clique_visitor supplies an empty visitation function.
92 */
93struct clique_visitor
94{
95 template <typename VertexSet, typename Graph>
96 void clique(const VertexSet&, Graph&)
97 { }
98};
99
100/**
101 * The max_clique_visitor records the size of the maximum clique (but not the
102 * clique itself).
103 */
104struct max_clique_visitor
105{
106 max_clique_visitor(std::size_t& max)
107 : maximum(max)
108 { }
109
110 template <typename Clique, typename Graph>
111 inline void clique(const Clique& p, const Graph& g)
112 {
113 BOOST_USING_STD_MAX();
114 maximum = max BOOST_PREVENT_MACRO_SUBSTITUTION (maximum, p.size());
115 }
116 std::size_t& maximum;
117};
118
119inline max_clique_visitor find_max_clique(std::size_t& max)
120{ return max_clique_visitor(max); }
121
122namespace detail
123{
124 template <typename Graph>
125 inline bool
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)
130 {
131 return lookup_edge(u, v, g).second;
132 }
133
134 template <typename Graph>
135 inline bool
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)
140 {
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;
147 }
148
149 template <typename Graph, typename Container>
150 inline void
151 filter_unconnected_vertices(const Graph& g,
152 typename graph_traits<Graph>::vertex_descriptor v,
153 const Container& in,
154 Container& out)
155 {
156 BOOST_CONCEPT_ASSERT(( GraphConcept<Graph> ));
157
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)) {
162 out.push_back(*i);
163 }
164 }
165 }
166
167 template <
168 typename Graph,
169 typename Clique, // compsub type
170 typename Container, // candidates/not type
171 typename Visitor>
172 void extend_clique(const Graph& g,
173 Clique& clique,
174 Container& cands,
175 Container& nots,
176 Visitor vis,
177 std::size_t min)
178 {
179 BOOST_CONCEPT_ASSERT(( GraphConcept<Graph> ));
180 BOOST_CONCEPT_ASSERT(( CliqueVisitorConcept<Visitor,Clique,Graph> ));
181 typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
182
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.
186 {
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;
193 }
194 // if we iterated all the way to the end, then *ni
195 // is connected to all *ci
196 if(ci == cend) break;
197 }
198 // if we broke early, we found *ni connected to all *ci
199 if(ni != nend) return;
200 }
201
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.
208
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.
217
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.
220
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
223 // size - maybe?
224
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;
230
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);
234
235 // remove it from the candidate set
236 i = cands.erase(i);
237
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);
245
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);
252 }
253 }
254 else {
255 // recurse to explore the new candidates
256 extend_clique(g, clique, new_cands, new_nots, vis, min);
257 }
258
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);
262 clique.pop_back();
263 }
264 }
265} /* namespace detail */
266
267template <typename Graph, typename Visitor>
268inline void
269bron_kerbosch_all_cliques(const Graph& g, Visitor vis, std::size_t min)
270{
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> ));
279
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.
282
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
287
288 Clique clique; // the first clique is an empty vertex set
289 detail::extend_clique(g, clique, cands, nots, vis, min);
290}
291
292// NOTE: By default the minimum number of vertices per clique is set at 2
293// because singleton cliques aren't really very interesting.
294template <typename Graph, typename Visitor>
295inline void
296bron_kerbosch_all_cliques(const Graph& g, Visitor vis)
297{ bron_kerbosch_all_cliques(g, vis, 2); }
298
299template <typename Graph>
300inline std::size_t
301bron_kerbosch_clique_number(const Graph& g)
302{
303 std::size_t ret = 0;
304 bron_kerbosch_all_cliques(g, find_max_clique(max&: ret));
305 return ret;
306}
307
308} /* namespace boost */
309
310#endif
311

source code of boost/boost/graph/bron_kerbosch_all_cliques.hpp