Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
UndirectedMultiGraph.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/pair>
12#include <alib/set>
13#include <alib/vector>
14#include <alib/tuple>
15#include <functional>
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 UndirectedMultiGraph : public GraphInterface<TNode, TEdge> {
27// ---------------------------------------------------------------------------------------------------------------------
28
29 public:
30 using node_type = TNode;
31 using edge_type = TEdge;
32
33// ---------------------------------------------------------------------------------------------------------------------
34
35 protected:
37
38// =====================================================================================================================
39// Constructor, Destructor, Operators
40 public:
41
42// ---------------------------------------------------------------------------------------------------------------------
43
45
46 ext::map<TNode, std::multimap<TNode, TEdge>> &&getAdjacencyList() &&;
47
48// ---------------------------------------------------------------------------------------------------------------------
49
50// =====================================================================================================================
51// GraphBase interface
52 public:
53 auto operator <=> (const UndirectedMultiGraph &other) const {
54 return std::tie(m_adjacency_list) <=> std::tie(other.getAdjacencyList());
55 }
56
57 bool operator == (const UndirectedMultiGraph &other) const {
59 }
60
61 void operator>>(ext::ostream &ostream) const override;
62
63// =====================================================================================================================
64// Graph interface
65// ---------------------------------------------------------------------------------------------------------------------
66
67 public:
68 void addNode(const TNode &n);
69 void addNode(TNode &&n);
70 template<typename ... Params>
71 void addNode(Params &&... params);
72
73// ---------------------------------------------------------------------------------------------------------------------
74
75 bool addEdge(const TEdge &e);
76 bool addEdge(TEdge &&e);
77 template<typename ... Params>
78 bool addEdge(Params &&... params);
79
80// ---------------------------------------------------------------------------------------------------------------------
81
82// =====================================================================================================================
83// GraphInterface interface
84
85 size_t nodeCount() const override;
86 size_t edgeCount() const override;
87
88// ---------------------------------------------------------------------------------------------------------------------
89
90 ext::set<TNode> getNodes() const override;
91 ext::vector<TEdge> getEdges() const override;
92
93// ---------------------------------------------------------------------------------------------------------------------
94
95 ext::set<TNode> successors(const TNode &n) const override;
96 ext::vector<TEdge> successorEdges(const TNode &n) const override;
97 ext::set<TNode> predecessors(const TNode &n) const override;
98 ext::vector<TEdge> predecessorEdges(const TNode &n) const override;
99
100// ---------------------------------------------------------------------------------------------------------------------
101
102 std::string name() const override;
103
104// ---------------------------------------------------------------------------------------------------------------------
105
106};
107
108// =====================================================================================================================
109
110template<typename TNode, typename TEdge>
112 return m_adjacency_list;
113}
114
115template<typename TNode, typename TEdge>
117 return std::move(m_adjacency_list);
118}
119
120// ---------------------------------------------------------------------------------------------------------------------
121
122template<typename TNode, typename TEdge>
124 m_adjacency_list[n];
125}
126
127template<typename TNode, typename TEdge>
129 m_adjacency_list[std::forward<TNode>(n)];
130}
131
132template<typename TNode, typename TEdge>
133template<typename... Params>
135 addNode(TNode(std::forward<Params>(params) ...));
136}
137
138// ---------------------------------------------------------------------------------------------------------------------
139
140template<typename TNode, typename TEdge>
142 m_adjacency_list[e.first].insert(ext::make_pair(e.second, e));
143 m_adjacency_list[e.second].insert(ext::make_pair(e.first, e));
144 return true;
145}
146
147template<typename TNode, typename TEdge>
149 m_adjacency_list[e.first].insert(ext::make_pair(e.second, e));
150 m_adjacency_list[e.second].insert(ext::make_pair(e.first, std::forward<TEdge>(e)));
151 return true;
152}
153
154template<typename TNode, typename TEdge>
155template<typename... Params>
157 return addEdge(TEdge(std::forward<Params>(params) ...));
158}
159
160// ---------------------------------------------------------------------------------------------------------------------
161
162template<typename TNode, typename TEdge>
164 return m_adjacency_list.size();
165}
166
167template<typename TNode, typename TEdge>
169 size_t cnt = 0;
170
171 for (const auto &i: m_adjacency_list) {
172 cnt += i.second.size();
173 }
174
175 return cnt / 2;
176}
177
178// ---------------------------------------------------------------------------------------------------------------------
179
180template<typename TNode, typename TEdge>
182 ext::set<TNode> set;
183
184 for (const auto &i: m_adjacency_list) {
185 set.insert(i.first);
186 }
187
188 return set;
189}
190
191template<typename TNode, typename TEdge>
194
195 for (const auto &i:m_adjacency_list) {
196 for (const auto &j : i.second) {
197 vec.push_back(j.second);
198 }
199 }
200
201 return vec;
202}
203
204// ---------------------------------------------------------------------------------------------------------------------
205
206template<typename TNode, typename TEdge>
208 ext::set<TNode> set;
209
210 if (m_adjacency_list.count(n) == 0) {
211 return set;
212 }
213
214 for (const auto &i: m_adjacency_list.at(n)) {
215 set.insert(i.first);
216 }
217
218 return set;
219}
220
221template<typename TNode, typename TEdge>
224
225 if (m_adjacency_list.count(n) == 0) {
226 return vec;
227 }
228
229 for (const auto &i: m_adjacency_list.at(n)) {
230 vec.push_back(i.second);
231 }
232
233 return vec;
234}
235
236template<typename TNode, typename TEdge>
238 return successors(n);
239}
240
241template<typename TNode, typename TEdge>
243 return successorEdges(n);
244}
245
246// ---------------------------------------------------------------------------------------------------------------------
247
248template<typename TNode, typename TEdge>
250 return "UndirectedMultiGraph";
251}
252
253// ---------------------------------------------------------------------------------------------------------------------
254
255template<typename TNode, typename TEdge>
257 ostream << "(" << name() << " ";
258 for (const auto &i : m_adjacency_list) {
259 ostream << i.first << " <-->" << std::endl;
260
261 for (const auto &j : i.second) {
262 ostream << "\t\t" << j.first << " " << j.second << std::endl;
263 }
264 }
265 ostream << ")";
266}
267
268// ---------------------------------------------------------------------------------------------------------------------
269
270} // namespace graph
271
272// =====================================================================================================================
273
274namespace core {
275
282template<typename TNode, typename TEdge>
283struct normalize < graph::UndirectedMultiGraph < TNode, TEdge > > {
286
287 for (auto &&i: ext::make_mover(std::move(value).getAdjacencyList())) {
289 for (auto &&j: i.second) {
292
293 graph.addNode(first);
294 graph.addNode(second);
295 graph.addEdge(edge);
296 }
297 }
298
299 return graph;
300 }
301};
302
303}
304
305// =====================================================================================================================
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: GraphInterface.hpp:19
TEdge edge_type
Definition: GraphInterface.hpp:23
TNode node_type
Definition: GraphInterface.hpp:22
static DefaultNodeType normalizeNode(TNode &&node)
Definition: Normalize.hpp:47
static DefaultEdgeType normalizeEdge(TEdge &&edge)
Definition: Normalize.hpp:52
Definition: UndirectedMultiGraph.hpp:26
void operator>>(ext::ostream &ostream) const override
Definition: UndirectedMultiGraph.hpp:256
bool addEdge(Params &&... params)
Definition: UndirectedMultiGraph.hpp:156
void addNode(Params &&... params)
Definition: UndirectedMultiGraph.hpp:134
size_t edgeCount() const override
Definition: UndirectedMultiGraph.hpp:168
void addNode(const TNode &n)
Definition: UndirectedMultiGraph.hpp:123
size_t nodeCount() const override
Definition: UndirectedMultiGraph.hpp:163
const ext::map< TNode, std::multimap< TNode, TEdge > > & getAdjacencyList() const &
Definition: UndirectedMultiGraph.hpp:111
ext::set< TNode > getNodes() const override
Definition: UndirectedMultiGraph.hpp:181
ext::set< TNode > successors(const TNode &n) const override
Definition: UndirectedMultiGraph.hpp:207
ext::vector< TEdge > getEdges() const override
Definition: UndirectedMultiGraph.hpp:192
bool operator==(const UndirectedMultiGraph &other) const
Definition: UndirectedMultiGraph.hpp:57
std::string name() const override
Definition: UndirectedMultiGraph.hpp:249
bool addEdge(TEdge &&e)
Definition: UndirectedMultiGraph.hpp:148
ext::vector< TEdge > successorEdges(const TNode &n) const override
Definition: UndirectedMultiGraph.hpp:222
void addNode(TNode &&n)
Definition: UndirectedMultiGraph.hpp:128
ext::set< TNode > predecessors(const TNode &n) const override
Definition: UndirectedMultiGraph.hpp:237
bool addEdge(const TEdge &e)
Definition: UndirectedMultiGraph.hpp:141
ext::vector< TEdge > predecessorEdges(const TNode &n) const override
Definition: UndirectedMultiGraph.hpp:242
ext::map< TNode, std::multimap< TNode, TEdge > > m_adjacency_list
Definition: UndirectedMultiGraph.hpp:36
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::UndirectedMultiGraph eval(graph::UndirectedMultiGraph< TNode, TEdge > &&value)
Definition: UndirectedMultiGraph.hpp:284
Definition: normalize.hpp:13