All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
cycle_algo.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2013 Piotr Wygocki
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
16 #ifndef PAAL_CYCLE_ALGO_HPP
17 #define PAAL_CYCLE_ALGO_HPP
18 
21 
22 #include <algorithm>
23 #include <numeric>
24 #include <string>
25 
26 namespace paal {
27 
38 template <typename Metric, typename Cycle>
39 typename Metric::DistanceType get_cycle_length(const Metric &m, const Cycle &cm) {
40  typedef typename data_structures::cycle_traits<Cycle>::CycleElem El;
41  typedef typename Metric::DistanceType Dist;
42 
43  auto ebegin =
44  data_structures::make_vertex_to_edge_iterator(cm.vbegin(), cm.vend());
45  auto eend =
47  return std::accumulate(ebegin, eend, Dist(),
48  [&m](Dist a, const std::pair<El, El> & p)->Dist{
49  return a + m(p.first, p.second);
50  });
51 }
52 
54 template <typename Cycle, typename Stream>
55 void print_cycle(const Cycle &cm, Stream &o, const std::string &endl = "\n") {
56  auto ebegin =
57  data_structures::make_vertex_to_edge_iterator(cm.vbegin(), cm.vend());
58  auto eend =
60  typedef typename data_structures::cycle_traits<Cycle>::CycleElem El;
61 
62  for (auto const &p :
63  boost::make_iterator_range(ebegin, eend)) {
64  o << "(" << p.first << "," << p.second << ")->";
65  }
66 
67  o << endl;
68 }
69 
70 }
71 
72 #endif // PAAL_CYCLE_ALGO_HPP
Metric::DistanceType get_cycle_length(const Metric &m, const Cycle &cm)
computes length of the cycle
Definition: cycle_algo.hpp:39
void print_cycle(const Cycle &cm, Stream &o, const std::string &endl="\n")
pints cycle to std out
Definition: cycle_algo.hpp:55
vertex_to_edge_iterator< vertex_iterator > make_vertex_to_edge_iterator(vertex_iterator b, vertex_iterator e)
make for vertex_to_edge_iterator