annotate DEPENDENCIES/generic/include/boost/graph/biconnected_components.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 // Copyright (c) Jeremy Siek 2001
Chris@16 2 // Copyright (c) Douglas Gregor 2004
Chris@16 3 //
Chris@16 4 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 5 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 6 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 7
Chris@16 8 // NOTE: this final is generated by libs/graph/doc/biconnected_components.w
Chris@16 9
Chris@16 10 #ifndef BOOST_GRAPH_BICONNECTED_COMPONENTS_HPP
Chris@16 11 #define BOOST_GRAPH_BICONNECTED_COMPONENTS_HPP
Chris@16 12
Chris@16 13 #include <stack>
Chris@16 14 #include <vector>
Chris@16 15 #include <algorithm> // for std::min and std::max
Chris@16 16 #include <boost/config.hpp>
Chris@16 17 #include <boost/limits.hpp>
Chris@16 18 #include <boost/graph/graph_traits.hpp>
Chris@16 19 #include <boost/graph/graph_concepts.hpp>
Chris@16 20 #include <boost/property_map/property_map.hpp>
Chris@16 21 #include <boost/graph/depth_first_search.hpp>
Chris@16 22 #include <boost/graph/graph_utility.hpp>
Chris@16 23 #include <boost/concept/assert.hpp>
Chris@16 24 #include <boost/assert.hpp>
Chris@16 25
Chris@16 26 namespace boost
Chris@16 27 {
Chris@16 28 namespace detail
Chris@16 29 {
Chris@16 30 template<typename ComponentMap, typename DiscoverTimeMap,
Chris@16 31 typename LowPointMap, typename PredecessorMap,
Chris@16 32 typename OutputIterator, typename Stack,
Chris@16 33 typename ArticulationVector, typename IndexMap,
Chris@16 34 typename DFSVisitor>
Chris@16 35 struct biconnected_components_visitor : public dfs_visitor<>
Chris@16 36 {
Chris@16 37 biconnected_components_visitor
Chris@16 38 (ComponentMap comp, std::size_t& c,
Chris@16 39 std::size_t& children_of_root, DiscoverTimeMap dtm,
Chris@16 40 std::size_t& dfs_time, LowPointMap lowpt, PredecessorMap pred,
Chris@16 41 OutputIterator out, Stack& S,
Chris@16 42 ArticulationVector& is_articulation_point, IndexMap index_map,
Chris@16 43 DFSVisitor vis)
Chris@16 44 : comp(comp), c(c), children_of_root(children_of_root),
Chris@16 45 dtm(dtm), dfs_time(dfs_time), lowpt(lowpt),
Chris@16 46 pred(pred), out(out), S(S),
Chris@16 47 is_articulation_point(is_articulation_point),
Chris@16 48 index_map(index_map), vis(vis) { }
Chris@16 49
Chris@16 50 template <typename Vertex, typename Graph>
Chris@16 51 void initialize_vertex(const Vertex& u, Graph& g)
Chris@16 52 {
Chris@16 53 put(pred, u, u);
Chris@16 54 vis.initialize_vertex(u, g);
Chris@16 55 }
Chris@16 56
Chris@16 57 template <typename Vertex, typename Graph>
Chris@16 58 void start_vertex(const Vertex& u, Graph& g)
Chris@16 59 {
Chris@16 60 children_of_root = 0;
Chris@16 61 vis.start_vertex(u, g);
Chris@16 62 }
Chris@16 63
Chris@16 64 template <typename Vertex, typename Graph>
Chris@16 65 void discover_vertex(const Vertex& u, Graph& g)
Chris@16 66 {
Chris@16 67 put(dtm, u, ++dfs_time);
Chris@16 68 put(lowpt, u, get(dtm, u));
Chris@16 69 vis.discover_vertex(u, g);
Chris@16 70 }
Chris@16 71
Chris@16 72 template <typename Edge, typename Graph>
Chris@16 73 void examine_edge(const Edge& e, Graph& g)
Chris@16 74 {
Chris@16 75 vis.examine_edge(e, g);
Chris@16 76 }
Chris@16 77
Chris@16 78 template <typename Edge, typename Graph>
Chris@16 79 void tree_edge(const Edge& e, Graph& g)
Chris@16 80 {
Chris@16 81 typename boost::graph_traits<Graph>::vertex_descriptor src = source(e, g);
Chris@16 82 typename boost::graph_traits<Graph>::vertex_descriptor tgt = target(e, g);
Chris@16 83
Chris@16 84 S.push(e);
Chris@16 85 put(pred, tgt, src);
Chris@16 86 if ( get(pred, src) == src ) {
Chris@16 87 ++children_of_root;
Chris@16 88 }
Chris@16 89 vis.tree_edge(e, g);
Chris@16 90 }
Chris@16 91
Chris@16 92 template <typename Edge, typename Graph>
Chris@16 93 void back_edge(const Edge& e, Graph& g)
Chris@16 94 {
Chris@16 95 BOOST_USING_STD_MIN();
Chris@16 96
Chris@16 97 typename boost::graph_traits<Graph>::vertex_descriptor src = source(e, g);
Chris@16 98 typename boost::graph_traits<Graph>::vertex_descriptor tgt = target(e, g);
Chris@16 99 if ( tgt != get(pred, src) ) {
Chris@16 100 S.push(e);
Chris@16 101 put(lowpt, src,
Chris@16 102 min BOOST_PREVENT_MACRO_SUBSTITUTION(get(lowpt, src),
Chris@16 103 get(dtm, tgt)));
Chris@16 104 }
Chris@16 105 vis.back_edge(e, g);
Chris@16 106 }
Chris@16 107
Chris@16 108 template <typename Edge, typename Graph>
Chris@16 109 void forward_or_cross_edge(const Edge& e, Graph& g)
Chris@16 110 {
Chris@16 111 vis.forward_or_cross_edge(e, g);
Chris@16 112 }
Chris@16 113
Chris@16 114 template <typename Vertex, typename Graph>
Chris@16 115 void finish_vertex(const Vertex& u, Graph& g)
Chris@16 116 {
Chris@16 117 BOOST_USING_STD_MIN();
Chris@16 118 Vertex parent = get(pred, u);
Chris@16 119 if (parent == u) { // Root of tree is special
Chris@16 120 is_articulation_point[get(index_map, u)] = (children_of_root > 1);
Chris@16 121 } else {
Chris@16 122 put(lowpt, parent,
Chris@16 123 min BOOST_PREVENT_MACRO_SUBSTITUTION(get(lowpt, parent),
Chris@16 124 get(lowpt, u)));
Chris@16 125 if ( get(lowpt, u) >= get(dtm, parent) ) {
Chris@16 126 is_articulation_point[get(index_map, parent)] = true;
Chris@16 127 while ( get(dtm, source(S.top(), g)) >= get(dtm, u) ) {
Chris@16 128 put(comp, S.top(), c);
Chris@16 129 S.pop();
Chris@16 130 }
Chris@16 131 BOOST_ASSERT (source(S.top(), g) == parent);
Chris@16 132 BOOST_ASSERT (target(S.top(), g) == u);
Chris@16 133 put(comp, S.top(), c);
Chris@16 134 S.pop();
Chris@16 135 ++c;
Chris@16 136 }
Chris@16 137 }
Chris@16 138 if ( is_articulation_point[get(index_map, u)] ) {
Chris@16 139 *out++ = u;
Chris@16 140 }
Chris@16 141 vis.finish_vertex(u, g);
Chris@16 142 }
Chris@16 143
Chris@16 144 ComponentMap comp;
Chris@16 145 std::size_t& c;
Chris@16 146 std::size_t& children_of_root;
Chris@16 147 DiscoverTimeMap dtm;
Chris@16 148 std::size_t& dfs_time;
Chris@16 149 LowPointMap lowpt;
Chris@16 150 PredecessorMap pred;
Chris@16 151 OutputIterator out;
Chris@16 152 Stack& S;
Chris@16 153 ArticulationVector& is_articulation_point;
Chris@16 154 IndexMap index_map;
Chris@16 155 DFSVisitor vis;
Chris@16 156 };
Chris@16 157
Chris@16 158 template<typename Graph, typename ComponentMap, typename OutputIterator,
Chris@16 159 typename VertexIndexMap, typename DiscoverTimeMap, typename LowPointMap,
Chris@16 160 typename PredecessorMap, typename DFSVisitor>
Chris@16 161 std::pair<std::size_t, OutputIterator>
Chris@16 162 biconnected_components_impl(const Graph & g, ComponentMap comp,
Chris@16 163 OutputIterator out, VertexIndexMap index_map, DiscoverTimeMap dtm,
Chris@16 164 LowPointMap lowpt, PredecessorMap pred, DFSVisitor dfs_vis)
Chris@16 165 {
Chris@16 166 typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
Chris@16 167 typedef typename graph_traits<Graph>::edge_descriptor edge_t;
Chris@16 168 BOOST_CONCEPT_ASSERT(( VertexListGraphConcept<Graph> ));
Chris@16 169 BOOST_CONCEPT_ASSERT(( IncidenceGraphConcept<Graph> ));
Chris@16 170 BOOST_CONCEPT_ASSERT(( WritablePropertyMapConcept<ComponentMap, edge_t> ));
Chris@16 171 BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept<DiscoverTimeMap,
Chris@16 172 vertex_t> ));
Chris@16 173 BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept<LowPointMap, vertex_t > ));
Chris@16 174 BOOST_CONCEPT_ASSERT(( ReadWritePropertyMapConcept<PredecessorMap,
Chris@16 175 vertex_t> ));
Chris@16 176
Chris@16 177 std::size_t num_components = 0;
Chris@16 178 std::size_t children_of_root;
Chris@16 179 std::size_t dfs_time = 0;
Chris@16 180 std::stack<edge_t> S;
Chris@16 181 std::vector<char> is_articulation_point(num_vertices(g));
Chris@16 182
Chris@16 183 biconnected_components_visitor<ComponentMap, DiscoverTimeMap,
Chris@16 184 LowPointMap, PredecessorMap, OutputIterator, std::stack<edge_t>,
Chris@16 185 std::vector<char>, VertexIndexMap, DFSVisitor>
Chris@16 186 vis(comp, num_components, children_of_root, dtm, dfs_time,
Chris@16 187 lowpt, pred, out, S, is_articulation_point, index_map, dfs_vis);
Chris@16 188
Chris@16 189 depth_first_search(g, visitor(vis).vertex_index_map(index_map));
Chris@16 190
Chris@16 191 return std::pair<std::size_t, OutputIterator>(num_components, vis.out);
Chris@16 192 }
Chris@16 193
Chris@16 194 template <typename PredecessorMap>
Chris@16 195 struct bicomp_dispatch3
Chris@16 196 {
Chris@16 197 template<typename Graph, typename ComponentMap, typename OutputIterator,
Chris@16 198 typename VertexIndexMap, typename DiscoverTimeMap,
Chris@16 199 typename LowPointMap, class P, class T, class R>
Chris@16 200 static std::pair<std::size_t, OutputIterator> apply (const Graph & g,
Chris@16 201 ComponentMap comp, OutputIterator out, VertexIndexMap index_map,
Chris@16 202 DiscoverTimeMap dtm, LowPointMap lowpt,
Chris@16 203 const bgl_named_params<P, T, R>& params, PredecessorMap pred)
Chris@16 204 {
Chris@16 205 return biconnected_components_impl
Chris@16 206 (g, comp, out, index_map, dtm, lowpt, pred,
Chris@16 207 choose_param(get_param(params, graph_visitor),
Chris@16 208 make_dfs_visitor(null_visitor())));
Chris@16 209 }
Chris@16 210 };
Chris@16 211
Chris@16 212 template <>
Chris@16 213 struct bicomp_dispatch3<param_not_found>
Chris@16 214 {
Chris@16 215 template<typename Graph, typename ComponentMap, typename OutputIterator,
Chris@16 216 typename VertexIndexMap, typename DiscoverTimeMap,
Chris@16 217 typename LowPointMap, class P, class T, class R>
Chris@16 218 static std::pair<std::size_t, OutputIterator> apply (const Graph & g,
Chris@16 219 ComponentMap comp, OutputIterator out, VertexIndexMap index_map,
Chris@16 220 DiscoverTimeMap dtm, LowPointMap lowpt,
Chris@16 221 const bgl_named_params<P, T, R>& params,
Chris@16 222 param_not_found)
Chris@16 223 {
Chris@16 224 typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
Chris@16 225 std::vector<vertex_t> pred(num_vertices(g));
Chris@16 226 vertex_t vert = graph_traits<Graph>::null_vertex();
Chris@16 227
Chris@16 228 return biconnected_components_impl
Chris@16 229 (g, comp, out, index_map, dtm, lowpt,
Chris@16 230 make_iterator_property_map(pred.begin(), index_map, vert),
Chris@16 231 choose_param(get_param(params, graph_visitor),
Chris@16 232 make_dfs_visitor(null_visitor())));
Chris@16 233 }
Chris@16 234 };
Chris@16 235
Chris@16 236 template <typename LowPointMap>
Chris@16 237 struct bicomp_dispatch2
Chris@16 238 {
Chris@16 239 template<typename Graph, typename ComponentMap, typename OutputIterator,
Chris@16 240 typename VertexIndexMap, typename DiscoverTimeMap,
Chris@16 241 typename P, typename T, typename R>
Chris@16 242 static std::pair<std::size_t, OutputIterator> apply (const Graph& g,
Chris@16 243 ComponentMap comp, OutputIterator out, VertexIndexMap index_map,
Chris@16 244 DiscoverTimeMap dtm, const bgl_named_params<P, T, R>& params,
Chris@16 245 LowPointMap lowpt)
Chris@16 246 {
Chris@16 247 typedef typename get_param_type< vertex_predecessor_t, bgl_named_params<P,T,R> >::type dispatch_type;
Chris@16 248
Chris@16 249 return bicomp_dispatch3<dispatch_type>::apply
Chris@16 250 (g, comp, out, index_map, dtm, lowpt, params,
Chris@16 251 get_param(params, vertex_predecessor));
Chris@16 252 }
Chris@16 253 };
Chris@16 254
Chris@16 255
Chris@16 256 template <>
Chris@16 257 struct bicomp_dispatch2<param_not_found>
Chris@16 258 {
Chris@16 259 template<typename Graph, typename ComponentMap, typename OutputIterator,
Chris@16 260 typename VertexIndexMap, typename DiscoverTimeMap,
Chris@16 261 typename P, typename T, typename R>
Chris@16 262 static std::pair<std::size_t, OutputIterator> apply (const Graph& g,
Chris@16 263 ComponentMap comp, OutputIterator out, VertexIndexMap index_map,
Chris@16 264 DiscoverTimeMap dtm, const bgl_named_params<P, T, R>& params,
Chris@16 265 param_not_found)
Chris@16 266 {
Chris@16 267 typedef typename graph_traits<Graph>::vertices_size_type
Chris@16 268 vertices_size_type;
Chris@16 269 std::vector<vertices_size_type> lowpt(num_vertices(g));
Chris@16 270 vertices_size_type vst(0);
Chris@16 271
Chris@16 272 typedef typename get_param_type< vertex_predecessor_t, bgl_named_params<P,T,R> >::type dispatch_type;
Chris@16 273
Chris@16 274 return bicomp_dispatch3<dispatch_type>::apply
Chris@16 275 (g, comp, out, index_map, dtm,
Chris@16 276 make_iterator_property_map(lowpt.begin(), index_map, vst),
Chris@16 277 params, get_param(params, vertex_predecessor));
Chris@16 278 }
Chris@16 279 };
Chris@16 280
Chris@16 281 template <typename DiscoverTimeMap>
Chris@16 282 struct bicomp_dispatch1
Chris@16 283 {
Chris@16 284 template<typename Graph, typename ComponentMap, typename OutputIterator,
Chris@16 285 typename VertexIndexMap, class P, class T, class R>
Chris@16 286 static std::pair<std::size_t, OutputIterator> apply(const Graph& g,
Chris@16 287 ComponentMap comp, OutputIterator out, VertexIndexMap index_map,
Chris@16 288 const bgl_named_params<P, T, R>& params, DiscoverTimeMap dtm)
Chris@16 289 {
Chris@16 290 typedef typename get_param_type< vertex_lowpoint_t, bgl_named_params<P,T,R> >::type dispatch_type;
Chris@16 291
Chris@16 292 return bicomp_dispatch2<dispatch_type>::apply
Chris@16 293 (g, comp, out, index_map, dtm, params,
Chris@16 294 get_param(params, vertex_lowpoint));
Chris@16 295 }
Chris@16 296 };
Chris@16 297
Chris@16 298 template <>
Chris@16 299 struct bicomp_dispatch1<param_not_found>
Chris@16 300 {
Chris@16 301 template<typename Graph, typename ComponentMap, typename OutputIterator,
Chris@16 302 typename VertexIndexMap, class P, class T, class R>
Chris@16 303 static std::pair<std::size_t, OutputIterator> apply(const Graph& g,
Chris@16 304 ComponentMap comp, OutputIterator out, VertexIndexMap index_map,
Chris@16 305 const bgl_named_params<P, T, R>& params, param_not_found)
Chris@16 306 {
Chris@16 307 typedef typename graph_traits<Graph>::vertices_size_type
Chris@16 308 vertices_size_type;
Chris@16 309 std::vector<vertices_size_type> discover_time(num_vertices(g));
Chris@16 310 vertices_size_type vst(0);
Chris@16 311
Chris@16 312 typedef typename get_param_type< vertex_lowpoint_t, bgl_named_params<P,T,R> >::type dispatch_type;
Chris@16 313
Chris@16 314 return bicomp_dispatch2<dispatch_type>::apply
Chris@16 315 (g, comp, out, index_map,
Chris@16 316 make_iterator_property_map(discover_time.begin(), index_map, vst),
Chris@16 317 params, get_param(params, vertex_lowpoint));
Chris@16 318 }
Chris@16 319 };
Chris@16 320
Chris@16 321 }
Chris@16 322
Chris@16 323 template<typename Graph, typename ComponentMap, typename OutputIterator,
Chris@16 324 typename DiscoverTimeMap, typename LowPointMap>
Chris@16 325 std::pair<std::size_t, OutputIterator>
Chris@16 326 biconnected_components(const Graph& g, ComponentMap comp,
Chris@16 327 OutputIterator out, DiscoverTimeMap dtm, LowPointMap lowpt)
Chris@16 328 {
Chris@16 329 typedef param_not_found dispatch_type;
Chris@16 330
Chris@16 331 return detail::bicomp_dispatch3<dispatch_type>::apply
Chris@16 332 (g, comp, out,
Chris@16 333 get(vertex_index, g),
Chris@16 334 dtm, lowpt,
Chris@16 335 bgl_named_params<int, buffer_param_t>(0),
Chris@16 336 param_not_found());
Chris@16 337 }
Chris@16 338
Chris@16 339 template <typename Graph, typename ComponentMap, typename OutputIterator,
Chris@16 340 typename P, typename T, typename R>
Chris@16 341 std::pair<std::size_t, OutputIterator>
Chris@16 342 biconnected_components(const Graph& g, ComponentMap comp, OutputIterator out,
Chris@16 343 const bgl_named_params<P, T, R>& params)
Chris@16 344 {
Chris@16 345 typedef typename get_param_type< vertex_discover_time_t, bgl_named_params<P,T,R> >::type dispatch_type;
Chris@16 346
Chris@16 347 return detail::bicomp_dispatch1<dispatch_type>::apply(g, comp, out,
Chris@16 348 choose_const_pmap(get_param(params, vertex_index), g, vertex_index),
Chris@16 349 params, get_param(params, vertex_discover_time));
Chris@16 350 }
Chris@16 351
Chris@16 352 template < typename Graph, typename ComponentMap, typename OutputIterator>
Chris@16 353 std::pair<std::size_t, OutputIterator>
Chris@16 354 biconnected_components(const Graph& g, ComponentMap comp, OutputIterator out)
Chris@16 355 {
Chris@16 356 return biconnected_components(g, comp, out,
Chris@16 357 bgl_named_params<int, buffer_param_t>(0));
Chris@16 358 }
Chris@16 359
Chris@16 360 namespace graph_detail {
Chris@16 361 struct dummy_output_iterator
Chris@16 362 {
Chris@16 363 typedef std::output_iterator_tag iterator_category;
Chris@16 364 typedef void value_type;
Chris@16 365 typedef void pointer;
Chris@16 366 typedef void difference_type;
Chris@16 367
Chris@16 368 struct reference {
Chris@16 369 template<typename T>
Chris@16 370 reference& operator=(const T&) { return *this; }
Chris@16 371 };
Chris@16 372
Chris@16 373 reference operator*() const { return reference(); }
Chris@16 374 dummy_output_iterator& operator++() { return *this; }
Chris@16 375 dummy_output_iterator operator++(int) { return *this; }
Chris@16 376 };
Chris@16 377 } // end namespace graph_detail
Chris@16 378
Chris@16 379 template <typename Graph, typename ComponentMap,
Chris@16 380 typename P, typename T, typename R>
Chris@16 381 std::size_t
Chris@16 382 biconnected_components(const Graph& g, ComponentMap comp,
Chris@16 383 const bgl_named_params<P, T, R>& params)
Chris@16 384 {
Chris@16 385 return biconnected_components(g, comp,
Chris@16 386 graph_detail::dummy_output_iterator(), params).first;
Chris@16 387 }
Chris@16 388
Chris@16 389 template <typename Graph, typename ComponentMap>
Chris@16 390 std::size_t
Chris@16 391 biconnected_components(const Graph& g, ComponentMap comp)
Chris@16 392 {
Chris@16 393 return biconnected_components(g, comp,
Chris@16 394 graph_detail::dummy_output_iterator()).first;
Chris@16 395 }
Chris@16 396
Chris@16 397 template<typename Graph, typename OutputIterator,
Chris@16 398 typename P, typename T, typename R>
Chris@16 399 OutputIterator
Chris@16 400 articulation_points(const Graph& g, OutputIterator out,
Chris@16 401 const bgl_named_params<P, T, R>& params)
Chris@16 402 {
Chris@16 403 return biconnected_components(g, dummy_property_map(), out,
Chris@16 404 params).second;
Chris@16 405 }
Chris@16 406
Chris@16 407 template<typename Graph, typename OutputIterator>
Chris@16 408 OutputIterator
Chris@16 409 articulation_points(const Graph& g, OutputIterator out)
Chris@16 410 {
Chris@16 411 return biconnected_components(g, dummy_property_map(), out,
Chris@16 412 bgl_named_params<int, buffer_param_t>(0)).second;
Chris@16 413 }
Chris@16 414
Chris@16 415 } // namespace boost
Chris@16 416
Chris@16 417 #endif /* BOOST_GRAPH_BICONNECTED_COMPONENTS_HPP */