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