CGRA-ME
GraphAlgorithms.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * The software programs comprising "CGRA-ME" and the documentation provided
3  * with them are copyright by its authors and the University of Toronto. Only
4  * non-commercial, not-for-profit use of this software is permitted without ex-
5  * plicit permission. This software is provided "as is" with no warranties or
6  * guarantees of support. See the LICENCE for more details. You should have re-
7  * ceived a copy of the full licence along with this software. If not, see
8  * <http://cgra-me.ece.utoronto.ca/license/>.
9  ******************************************************************************/
10 
11 #ifndef __GRAPH_ALGORITHMS_H__
12 #define __GRAPH_ALGORITHMS_H__
13 
16 
17 #include <algorithm>
18 #include <array>
19 #include <list>
20 #include <map>
21 #include <queue>
22 #include <set>
23 #include <set>
24 #include <stack>
25 #include <thread>
26 #include <type_traits>
27 #include <unordered_map>
28 #include <unordered_set>
29 #include <utility>
30 #include <vector>
31 
67 template<
68  typename VertexID,
71 >
73 private:
74  int num_threads = 1; //< Max number of concurrent threads (may not be used by all algorithms)
75 
83  MapGen vertex_map_gen = {};
84 
91  SetGen vertex_set_gen = {};
92 
93 public:
94  GraphAlgorithms() { }
95 
96  template<typename NewMapGen, typename NewSetGen>
98  int num_threads,
99  NewMapGen&& vertex_map_gen,
100  NewSetGen&& vertex_set_gen
101  )
103  , vertex_map_gen(std::forward<NewMapGen>(vertex_map_gen))
104  , vertex_set_gen(std::forward<NewSetGen>(vertex_set_gen))
105  { }
106 
112  auto withThreads(int num_threads) const {
113  return GraphAlgorithms<
114  VertexID,
115  MapGen
116  >(
117  num_threads,
120  );
121  }
122 
128  template<typename NewMapGen>
129  auto withMapGen(NewMapGen&& newVertexMapGen) const {
130  return GraphAlgorithms<
131  VertexID,
132  std::decay_t<NewMapGen>,
133  SetGen
134  >(
135  num_threads,
136  std::forward<NewMapGen>(newVertexMapGen),
138  );
139  }
140 
146  template<typename NewSetGen>
147  auto withSetGen(NewSetGen&& newVertexSetGen) const {
148  return GraphAlgorithms<
149  VertexID,
150  MapGen,
151  std::decay_t<NewSetGen>
152  >(
153  num_threads,
155  std::forward<NewSetGen>(newVertexSetGen)
156  );
157  }
158 
159  template<typename... Params>
160  auto makeVertexMap() const {
161  return this->vertex_map_gen.template operator()<Params...>();
162  }
163 
164  template<typename... Params>
165  auto makeVertexSet() const {
166  return this->vertex_set_gen.template operator()<Params...>();
167  }
168 
169  template<typename... Params>
170  using VertexMap = std::decay_t<decltype(std::declval<MapGen>().template operator()<Params...>())>;
171  using VertexList = std::vector<VertexID>;
172  using VertexPath = VertexList;
173  template<typename... Params>
174  using VertexSet = std::decay_t<decltype(std::declval<SetGen>().template operator()<Params...>())>;
175  using PathList = std::vector<VertexPath>;
176  using EdgeSet = std::set<std::pair<VertexID, VertexID>>;
177 
180  bool success;
181 
182  template<typename STREAM, typename GRAPH>
183  void print(STREAM&& os, const GRAPH& g) const {
184  os << "{success = " << success << "; ";
185  print_container(os, path, [&](auto&& os, auto& v) { os << g.getNodeRef(v); });
186  os << '}';
187  }
188 
189  template<typename STREAM>
190  friend STREAM& operator<<(STREAM& os, const TracebackResult& tbr) { tbr.print(os); return os; }
191  };
192 
193  // this file has algorithms related to searching,
194  // eg. depth- and breadth-first visits
196 
197  // this file has algorithms related to finding paths,
198  // eg. tracing back through a search, and finding n shortest paths
200 
201 }; // end class GraphAlgorithms
202 
203 #endif // __GRAPH_ALGORITHMS_H__
StandardMapMaker< std::unordered_map, VertexID >
GraphAlgorithms::num_threads
int num_threads
Definition: GraphAlgorithms.h:82
GraphAlgorithms::TracebackResult::print
void print(STREAM &&os, const GRAPH &g) const
Definition: GraphAlgorithms.h:191
GraphAlgorithms
Definition: GraphAlgorithms.h:72
GraphAlgorithms::EdgeSet
std::set< std::pair< VertexID, VertexID > > EdgeSet
Definition: GraphAlgorithms.h:184
GraphAlgorithms::TracebackResult::path
VertexPath path
Definition: GraphAlgorithms.h:187
GraphAlgorithms::VertexPath
VertexList VertexPath
Definition: GraphAlgorithms.h:180
GraphAlgorithms::TracebackResult::success
bool success
Definition: GraphAlgorithms.h:188
GraphAlgorithms::GraphAlgorithms
GraphAlgorithms()
Definition: GraphAlgorithms.h:102
GraphAlgorithms::withMapGen
auto withMapGen(NewMapGen &&newVertexMapGen) const
Definition: GraphAlgorithms.h:137
GraphAlgorithmHelpers.h
GraphAlgorithms::VertexList
std::vector< VertexID > VertexList
Definition: GraphAlgorithms.h:179
GraphAlgorithms::TracebackResult
Definition: GraphAlgorithms.h:186
GraphAlgorithms::VertexMap
std::decay_t< decltype(std::declval< MapGen >().template operator()<Params... >())> VertexMap
Definition: GraphAlgorithms.h:178
GraphAlgorithms::PathList
std::vector< VertexPath > PathList
Definition: GraphAlgorithms.h:183
GraphAlgorithms::withSetGen
auto withSetGen(NewSetGen &&newVertexSetGen) const
Definition: GraphAlgorithms.h:155
GraphAlgorithms::VertexSet
std::decay_t< decltype(std::declval< SetGen >().template operator()<Params... >())> VertexSet
Definition: GraphAlgorithms.h:182
GraphAlgorithms_Paths.inl
GraphAlgorithms::makeVertexMap
auto makeVertexMap() const
Definition: GraphAlgorithms.h:168
Collections.h
print_container
void print_container(OSTREAM &&os, const CONTAINER &c, const T1 &element_separator, const T2 &container_prefix, const T3 &container_suffix, PRINTER &&element_printer=PRINTER{})
Definition: Collections.h:435
GraphAlgorithms_Searching.inl
GraphAlgorithms::withThreads
auto withThreads(int num_threads) const
Definition: GraphAlgorithms.h:120
GraphAlgorithms::makeVertexSet
auto makeVertexSet() const
Definition: GraphAlgorithms.h:173
GraphAlgorithms::vertex_map_gen
MapGen vertex_map_gen
Definition: GraphAlgorithms.h:91
GraphAlgorithms::TracebackResult::operator<<
friend STREAM & operator<<(STREAM &os, const TracebackResult &tbr)
Definition: GraphAlgorithms.h:198
StandardSetMaker< std::unordered_set, VertexID >
GraphAlgorithms::vertex_set_gen
SetGen vertex_set_gen
Definition: GraphAlgorithms.h:99