Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
DirectedGraph.hpp
Go to the documentation of this file.
1
6// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved.
7
8#pragma once
9
10#include <alib/map>
11#include <alib/set>
12#include <alib/tuple>
13#include <alib/vector>
14#include <functional>
15#include <alib/pair>
16#include <object/Object.h>
17#include <core/normalize.hpp>
18
19#include <common/Normalize.hpp>
22
23namespace graph {
24
25template<typename TNode, typename TEdge>
26class DirectedGraph : public GraphInterface<TNode, TEdge> {
27// ---------------------------------------------------------------------------------------------------------------------
28
29 public:
30 using node_type = TNode;
31 using edge_type = TEdge;
32
33// ---------------------------------------------------------------------------------------------------------------------
34
35 protected:
38
39// =====================================================================================================================
40// Constructor, Destructor, Operators
41 public:
42
43// ---------------------------------------------------------------------------------------------------------------------
44
46 ext::map<TNode, ext::map<TNode, TEdge>> &&getSuccessorList() &&;
47
48 const ext::map<TNode, ext::map<TNode, TEdge>> &getPredecessorList() const &;
49 ext::map<TNode, ext::map<TNode, TEdge>> &&getPredecessorList() &&;
50
51// ---------------------------------------------------------------------------------------------------------------------
52
53// =====================================================================================================================
54// GraphBase interface
55 public:
56 auto operator <=> (const DirectedGraph &other) const {
57 return std::tie(m_succ_list, m_pred_list) <=> std::tie(other.getSuccessorList(), other.getPredecessorList());
58 }
59
60 bool operator == (const DirectedGraph &other) const {
62 }
63
64 void operator>>(ext::ostream &ostream) const override;
65
66// =====================================================================================================================
67// Graph interface
68
69 public:
70 void addNode(const TNode &n);
71 void addNode(TNode &&n);
72 template<typename ... Params>
73 void addNode(Params &&... params);
74
75// ---------------------------------------------------------------------------------------------------------------------
76
77 bool addEdge(const TEdge &e);
78 bool addEdge(TEdge &&e);
79 template<typename ... Params>
80 bool addEdge(Params &&... params);
81
82// ---------------------------------------------------------------------------------------------------------------------
83
84// =====================================================================================================================
85// GraphInterface interface
86
87 size_t nodeCount() const override;
88 size_t edgeCount() const override;
89
90// ---------------------------------------------------------------------------------------------------------------------
91
92 ext::set<TNode> getNodes() const override;
93 ext::vector<TEdge> getEdges() const override;
94
95// ---------------------------------------------------------------------------------------------------------------------
96
97 ext::set<TNode> successors(const TNode &n) const override;
98 ext::vector<TEdge> successorEdges(const TNode &n) const override;
99 ext::set<TNode> predecessors(const TNode &n) const override;
100 ext::vector<TEdge> predecessorEdges(const TNode &n) const override;
101
102// ---------------------------------------------------------------------------------------------------------------------
103
104 std::string name() const override;
105
106// ---------------------------------------------------------------------------------------------------------------------
107
108};
109
110// =====================================================================================================================
111
112template<typename TNode, typename TEdge>
114 return m_succ_list;
115}
116
117template<typename TNode, typename TEdge>
119 return std::move(m_succ_list);
120}
121
122template<typename TNode, typename TEdge>
124 return m_pred_list;
125}
126
127template<typename TNode, typename TEdge>
129 return std::move(m_pred_list);
130}
131
132// ---------------------------------------------------------------------------------------------------------------------
133
134template<typename TNode, typename TEdge>
136 m_succ_list[n];
137 m_pred_list[n];
138}
139
140template<typename TNode, typename TEdge>
142 m_succ_list[n];
143 m_pred_list[std::forward<TNode>(n)];
144}
145
146template<typename TNode, typename TEdge>
147template<typename... Params>
148void DirectedGraph<TNode, TEdge>::addNode(Params &&... params) {
149 addNode(TNode(std::forward<Params>(params) ...));
150}
151
152// ---------------------------------------------------------------------------------------------------------------------
153
154template<typename TNode, typename TEdge>
156 if (e.first == e.second) {
157 return false;
158 }
159
160 auto search_succ = m_succ_list.find(e.first);
161 if (search_succ != m_succ_list.end() && search_succ->second.find(e.second) != search_succ->second.end()) {
162 return false;
163 }
164
165 addNode(e.first);
166 addNode(e.second);
167 m_succ_list[e.first].insert(ext::make_pair(e.second, e));
168 m_pred_list[e.second].insert(ext::make_pair(e.first, e));
169
170 return true;
171}
172
173template<typename TNode, typename TEdge>
175 if (e.first == e.second) {
176 return false;
177 }
178
179 auto search_succ = m_succ_list.find(e.first);
180 if (search_succ != m_succ_list.end() && search_succ->second.find(e.second) != search_succ->second.end()) {
181 return false;
182 }
183
184 addNode(e.first);
185 addNode(e.second);
186 m_succ_list[e.first].insert(ext::make_pair(e.second, e));
187 m_pred_list[e.second].insert(ext::make_pair(e.first, std::forward<TEdge>(e)));
188
189 return true;
190}
191
192template<typename TNode, typename TEdge>
193template<typename... Params>
194bool DirectedGraph<TNode, TEdge>::addEdge(Params &&... params) {
195 return addEdge(TEdge(std::forward<Params>(params) ...));
196}
197
198// ---------------------------------------------------------------------------------------------------------------------
199
200template<typename TNode, typename TEdge>
202 return m_succ_list.size();
203}
204template<typename TNode, typename TEdge>
206 size_t cnt = 0;
207
208 for (const auto &i: m_succ_list) {
209 cnt += i.second.size();
210 }
211
212 return cnt;
213}
214
215// ---------------------------------------------------------------------------------------------------------------------
216
217template<typename TNode, typename TEdge>
219 ext::set<TNode> set;
220
221 for (const auto &i: m_succ_list) {
222 set.insert(i.first);
223 }
224
225 return set;
226}
227
228template<typename TNode, typename TEdge>
231
232 for (const auto &i: m_succ_list) {
233 for (const auto &j : i.second) {
234 vec.push_back(j.second);
235 }
236 }
237
238 return vec;
239}
240
241// ---------------------------------------------------------------------------------------------------------------------
242
243template<typename TNode, typename TEdge>
245 ext::set<TNode> set;
246
247 if (m_succ_list.count(n) == 0) {
248 return set;
249 }
250
251 for (const auto &i: m_succ_list.at(n)) {
252 set.insert(i.first);
253 }
254
255 return set;
256}
257
258template<typename TNode, typename TEdge>
261
262 if (m_succ_list.count(n) == 0) {
263 return vec;
264 }
265
266 for (const auto &i: m_succ_list.at(n)) {
267 vec.push_back(i.second);
268 }
269
270 return vec;
271}
272
273template<typename TNode, typename TEdge>
275 ext::set<TNode> set;
276
277 if (m_pred_list.count(n) == 0) {
278 return set;
279 }
280
281 for (const auto &i: m_pred_list.at(n)) {
282 set.insert(i.first);
283 }
284
285 return set;
286}
287
288template<typename TNode, typename TEdge>
291
292 if (m_pred_list.count(n) == 0) {
293 return vec;
294 }
295
296 for (const auto &i: m_pred_list.at(n)) {
297 vec.push_back(i.second);
298 }
299
300 return vec;
301}
302
303// ---------------------------------------------------------------------------------------------------------------------
304
305template<typename TNode, typename TEdge>
307 return "DirectedGraph";
308}
309
310// ---------------------------------------------------------------------------------------------------------------------
311
312template<typename TNode, typename TEdge>
314 ostream << "(" << name() << " ";
315 for (const auto &i : m_succ_list) {
316 ostream << i.first << " -->" << std::endl;
317
318 for (const auto &j : i.second) {
319 ostream << "\t\t" << j.first << " " << j.second << std::endl;
320 }
321 }
322 ostream << ")";
323}
324
325// ---------------------------------------------------------------------------------------------------------------------
326
327} // namespace graph
328
329// =====================================================================================================================
330
331namespace core {
332
339template<typename TNode, typename TEdge>
340struct normalize < graph::DirectedGraph < TNode, TEdge > > {
343
344 // Create successor
345 for (auto &&i: ext::make_mover(std::move(value).getSuccessorList())) {
347 for (auto &&j: i.second) {
350
351 graph.addNode(first);
352 graph.addNode(second);
353 graph.addEdge(edge);
354 }
355 }
356
357 // Create predecessors
358 for (auto &&i: ext::make_mover(std::move(value).getPredecessorList())) {
360 for (auto &&j: i.second) {
363
364 graph.addNode(first);
365 graph.addNode(second);
366 graph.addEdge(edge);
367 }
368 }
369
370 return graph;
371 }
372};
373
374}
375
376// =====================================================================================================================
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Definition: ostream.h:14
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Definition: set.hpp:44
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Definition: DirectedGraph.hpp:26
ext::set< TNode > predecessors(const TNode &n) const override
Definition: DirectedGraph.hpp:274
const ext::map< TNode, ext::map< TNode, TEdge > > & getSuccessorList() const &
Definition: DirectedGraph.hpp:113
void addNode(TNode &&n)
Definition: DirectedGraph.hpp:141
TNode node_type
Definition: DirectedGraph.hpp:30
size_t edgeCount() const override
Definition: DirectedGraph.hpp:205
std::string name() const override
Definition: DirectedGraph.hpp:306
void addNode(const TNode &n)
Definition: DirectedGraph.hpp:135
bool addEdge(const TEdge &e)
Definition: DirectedGraph.hpp:155
ext::vector< TEdge > predecessorEdges(const TNode &n) const override
Definition: DirectedGraph.hpp:289
void operator>>(ext::ostream &ostream) const override
Definition: DirectedGraph.hpp:313
void addNode(Params &&... params)
Definition: DirectedGraph.hpp:148
size_t nodeCount() const override
Definition: DirectedGraph.hpp:201
ext::vector< TEdge > successorEdges(const TNode &n) const override
Definition: DirectedGraph.hpp:259
ext::vector< TEdge > getEdges() const override
Definition: DirectedGraph.hpp:229
bool addEdge(TEdge &&e)
Definition: DirectedGraph.hpp:174
bool addEdge(Params &&... params)
Definition: DirectedGraph.hpp:194
bool operator==(const DirectedGraph &other) const
Definition: DirectedGraph.hpp:60
ext::map< TNode, ext::map< TNode, TEdge > > m_pred_list
Definition: DirectedGraph.hpp:37
ext::set< TNode > getNodes() const override
Definition: DirectedGraph.hpp:218
TEdge edge_type
Definition: DirectedGraph.hpp:31
ext::map< TNode, ext::map< TNode, TEdge > > m_succ_list
Definition: DirectedGraph.hpp:36
const ext::map< TNode, ext::map< TNode, TEdge > > & getPredecessorList() const &
Definition: DirectedGraph.hpp:123
ext::set< TNode > successors(const TNode &n) const override
Definition: DirectedGraph.hpp:244
Definition: GraphInterface.hpp:19
static DefaultNodeType normalizeNode(TNode &&node)
Definition: Normalize.hpp:47
static DefaultEdgeType normalizeEdge(TEdge &&edge)
Definition: Normalize.hpp:52
Definition: Object.h:16
p second
Definition: ToRegExpAlgebraic.h:126
int i
Definition: AllEpsilonClosure.h:118
Definition: normalize.hpp:10
Definition: CapacityEdge.hpp:18
Definition: sigHandler.cpp:20
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
reference_mover< T > make_mover(T &param)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
graph::DirectedGraph< node::Node, edge::CapacityEdge< node::Node, int > > DirectedGraph
Definition: FordFulkerson.hpp:27
Definition: ReconstructPath.hpp:14
static graph::DirectedGraph eval(graph::DirectedGraph< TNode, TEdge > &&value)
Definition: DirectedGraph.hpp:341
Definition: normalize.hpp:13