Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
DirectedMultiGraph.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/vector>
13#include <alib/tuple>
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 DirectedMultiGraph : 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, std::multimap<TNode, TEdge>> &&getSuccessorList() &&;
47
48 const ext::map<TNode, std::multimap<TNode, TEdge>> &getPredecessorList() const &;
49 ext::map<TNode, std::multimap<TNode, TEdge>> &&getPredecessorList() &&;
50
51// ---------------------------------------------------------------------------------------------------------------------
52
53// =====================================================================================================================
54// GraphBase interface
55 public:
56 auto operator <=> (const DirectedMultiGraph &other) const {
57 return std::tie(m_succ_list, m_pred_list) <=> std::tie(other.getSuccessorList(), other.getPredecessorList());
58 }
59
60 bool operator == (const DirectedMultiGraph &other) const {
62 }
63
64 void operator>>(ext::ostream &ostream) const override;
65
66// =====================================================================================================================
67// Graph interface
68
69 public:
70 virtual void addNode(const TNode &n);
71 virtual void addNode(TNode &&n);
72 template<typename ... Params>
73 void addNode(Params &&... params);
74
75// ---------------------------------------------------------------------------------------------------------------------
76
77 virtual bool addEdge(const TEdge &e);
78 virtual 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>
149 addNode(TNode(std::forward<Params>(params) ...));
150}
151
152// ---------------------------------------------------------------------------------------------------------------------
153
154template<typename TNode, typename TEdge>
156 m_succ_list[e.first].insert(ext::make_pair(e.second, e));
157 m_pred_list[e.second].insert(ext::make_pair(e.first, e));
158 return true;
159}
160
161template<typename TNode, typename TEdge>
163 m_succ_list[e.first].insert(ext::make_pair(e.second, e));
164 m_pred_list[e.second].insert(ext::make_pair(e.first, std::forward<TEdge>(e)));
165 return true;
166}
167
168template<typename TNode, typename TEdge>
169template<typename... Params>
171 return addEdge(TEdge(std::forward<Params>(params) ...));
172}
173
174// ---------------------------------------------------------------------------------------------------------------------
175
176template<typename TNode, typename TEdge>
178 return m_succ_list.size();
179}
180template<typename TNode, typename TEdge>
182 size_t cnt = 0;
183
184 for (const auto &i: m_succ_list) {
185 cnt += i.second.size();
186 }
187
188 return cnt;
189}
190
191// ---------------------------------------------------------------------------------------------------------------------
192
193template<typename TNode, typename TEdge>
195 ext::set<TNode> set;
196
197 for (const auto &i: m_succ_list) {
198 set.insert(i.first);
199 }
200
201 return set;
202}
203
204template<typename TNode, typename TEdge>
207
208 for (const auto &i: m_succ_list) {
209 for (const auto &j : i.second) {
210 vec.push_back(j.second);
211 }
212 }
213
214 return vec;
215}
216
217// ---------------------------------------------------------------------------------------------------------------------
218
219template<typename TNode, typename TEdge>
221 ext::set<TNode> set;
222
223 if (m_succ_list.count(n) == 0) {
224 return set;
225 }
226
227 for (const auto &i: m_succ_list.at(n)) {
228 set.insert(i.first);
229 }
230
231 return set;
232}
233
234template<typename TNode, typename TEdge>
237
238 if (m_succ_list.count(n) == 0) {
239 return vec;
240 }
241
242 for (const auto &i: m_succ_list.at(n)) {
243 vec.push_back(i.second);
244 }
245
246 return vec;
247}
248
249template<typename TNode, typename TEdge>
251 ext::set<TNode> set;
252
253 if (m_pred_list.count(n) == 0) {
254 return set;
255 }
256
257 for (const auto &i: m_pred_list.at(n)) {
258 set.insert(i.first);
259 }
260
261 return set;
262}
263
264template<typename TNode, typename TEdge>
267
268 if (m_pred_list.count(n) == 0) {
269 return vec;
270 }
271
272 for (const auto &i: m_pred_list.at(n)) {
273 vec.push_back(i.second);
274 }
275
276 return vec;
277}
278
279// ---------------------------------------------------------------------------------------------------------------------
280
281template<typename TNode, typename TEdge>
283 return "DirectedMultiGraph";
284}
285
286// ---------------------------------------------------------------------------------------------------------------------
287
288template<typename TNode, typename TEdge>
290 ostream << "(" << name() << " ";
291 for (const auto &i : m_succ_list) {
292 ostream << i.first << " -->" << std::endl;
293
294 for (const auto &j : i.second) {
295 ostream << "\t\t" << j.first << " " << j.second << std::endl;
296 }
297 }
298 ostream << ")";
299}
300
301// ---------------------------------------------------------------------------------------------------------------------
302
303} // namespace graph
304
305// =====================================================================================================================
306
307namespace core {
308
315template<typename TNode, typename TEdge>
316struct normalize < graph::DirectedMultiGraph < TNode, TEdge > > {
319
320 // Create successor
321 for (auto &&i: ext::make_mover(std::move(value).getSuccessorList())) {
323 for (auto &&j: i.second) {
326
327 graph.addNode(first);
328 graph.addNode(second);
329 graph.addEdge(edge);
330 }
331 }
332
333 // Create predecessors
334 for (auto &&i: ext::make_mover(std::move(value).getPredecessorList())) {
336 for (auto &&j: i.second) {
339
340 graph.addNode(first);
341 graph.addNode(second);
342 graph.addEdge(edge);
343 }
344 }
345
346 return graph;
347 }
348};
349
350// ---------------------------------------------------------------------------------------------------------------------
351
352}
353
354// =====================================================================================================================
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: DirectedMultiGraph.hpp:26
ext::set< TNode > successors(const TNode &n) const override
Definition: DirectedMultiGraph.hpp:220
TNode node_type
Definition: DirectedMultiGraph.hpp:30
bool operator==(const DirectedMultiGraph &other) const
Definition: DirectedMultiGraph.hpp:60
size_t nodeCount() const override
Definition: DirectedMultiGraph.hpp:177
size_t edgeCount() const override
Definition: DirectedMultiGraph.hpp:181
bool addEdge(Params &&... params)
Definition: DirectedMultiGraph.hpp:170
virtual void addNode(const TNode &n)
Definition: DirectedMultiGraph.hpp:135
void addNode(Params &&... params)
Definition: DirectedMultiGraph.hpp:148
ext::vector< TEdge > successorEdges(const TNode &n) const override
Definition: DirectedMultiGraph.hpp:235
ext::map< TNode, std::multimap< TNode, TEdge > > m_succ_list
Definition: DirectedMultiGraph.hpp:36
ext::set< TNode > predecessors(const TNode &n) const override
Definition: DirectedMultiGraph.hpp:250
virtual bool addEdge(const TEdge &e)
Definition: DirectedMultiGraph.hpp:155
virtual void addNode(TNode &&n)
Definition: DirectedMultiGraph.hpp:141
std::string name() const override
Definition: DirectedMultiGraph.hpp:282
ext::vector< TEdge > getEdges() const override
Definition: DirectedMultiGraph.hpp:205
ext::vector< TEdge > predecessorEdges(const TNode &n) const override
Definition: DirectedMultiGraph.hpp:265
virtual bool addEdge(TEdge &&e)
Definition: DirectedMultiGraph.hpp:162
ext::map< TNode, std::multimap< TNode, TEdge > > m_pred_list
Definition: DirectedMultiGraph.hpp:37
void operator>>(ext::ostream &ostream) const override
Definition: DirectedMultiGraph.hpp:289
const ext::map< TNode, std::multimap< TNode, TEdge > > & getSuccessorList() const &
Definition: DirectedMultiGraph.hpp:113
const ext::map< TNode, std::multimap< TNode, TEdge > > & getPredecessorList() const &
Definition: DirectedMultiGraph.hpp:123
ext::set< TNode > getNodes() const override
Definition: DirectedMultiGraph.hpp:194
TEdge edge_type
Definition: DirectedMultiGraph.hpp:31
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
Definition: ReconstructPath.hpp:14
Definition: FordFulkerson.hpp:16
static graph::DirectedMultiGraph eval(graph::DirectedMultiGraph< TNode, TEdge > &&value)
Definition: DirectedMultiGraph.hpp:317
Definition: normalize.hpp:13