Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
UndirectedGraph.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 <functional>
15#include <object/Object.h>
16#include <core/normalize.hpp>
17#include <alib/tuple>
18
19#include <common/Normalize.hpp>
22
23namespace graph {
24
25template<typename TNode, typename TEdge>
26class UndirectedGraph : 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, ext::map<TNode, TEdge>> &&getAdjacencyList() &&;
47
48// ---------------------------------------------------------------------------------------------------------------------
49
50// =====================================================================================================================
51// GraphBase interface
52 public:
53 auto operator <=> (const UndirectedGraph &other) const {
54 return std::tie(m_adjacency_list) <=> std::tie(other.getAdjacencyList());
55 }
56
57 bool operator == (const UndirectedGraph &other) const {
59 }
60
61 void operator>>(ext::ostream &ostream) const override;
62
63// =====================================================================================================================
64// Graph interface
65
66 public:
67 void addNode(const TNode &n);
68 void addNode(TNode &&n);
69 template<typename ... Params>
70 void addNode(Params &&... params);
71
72// ---------------------------------------------------------------------------------------------------------------------
73
74 bool addEdge(const TEdge &e);
75 bool addEdge(TEdge &&e);
76 template<typename ... Params>
77 bool addEdge(Params &&... params);
78
79// ---------------------------------------------------------------------------------------------------------------------
80
81// =====================================================================================================================
82// GraphInterface interface
83 public:
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 TEdge>::getAdjacencyList() const &{
113 return m_adjacency_list;
114}
115
116template<typename TNode, typename TEdge>
118 return std::move(m_adjacency_list);
119}
120
121
122// ---------------------------------------------------------------------------------------------------------------------
123
124template<typename TNode, typename TEdge>
126 m_adjacency_list[n];
127}
128
129template<typename TNode, typename TEdge>
131 m_adjacency_list[std::forward<TNode>(n)];
132}
133
134template<typename TNode, typename TEdge>
135template<typename... Params>
136void UndirectedGraph<TNode, TEdge>::addNode(Params &&... params) {
137 addNode(TNode(std::forward<Params>(params) ...));
138}
139
140// ---------------------------------------------------------------------------------------------------------------------
141
142template<typename TNode, typename TEdge>
144 if (e.first == e.second) {
145 return false;
146 }
147
148 if (m_adjacency_list.find(e.first) != m_adjacency_list.end()
149 && m_adjacency_list[e.first].find(e.second) != m_adjacency_list[e.first].end()) {
150 return false;
151 }
152
153 m_adjacency_list[e.first].insert(ext::make_pair(e.second, e));
154 m_adjacency_list[e.second].insert(ext::make_pair(e.first, e));
155
156 return true;
157}
158
159template<typename TNode, typename TEdge>
161 if (e.first == e.second) {
162 return false;
163 }
164
165 if (m_adjacency_list.find(e.first) != m_adjacency_list.end()
166 && m_adjacency_list[e.first].find(e.second) != m_adjacency_list[e.first].end()) {
167 return false;
168 }
169
170 m_adjacency_list[e.first].insert(ext::make_pair(e.second, e));
171 m_adjacency_list[e.second].insert(ext::make_pair(e.first, std::forward<TEdge>(e)));
172
173 return true;
174}
175
176template<typename TNode, typename TEdge>
177template<typename... Params>
178bool UndirectedGraph<TNode, TEdge>::addEdge(Params &&... params) {
179 return addEdge(TEdge(std::forward<Params>(params) ...));
180}
181
182// ---------------------------------------------------------------------------------------------------------------------
183
184template<typename TNode, typename TEdge>
186 return m_adjacency_list.size();
187}
188
189template<typename TNode, typename TEdge>
191 size_t cnt = 0;
192
193 for (const auto &i: m_adjacency_list) {
194 cnt += i.second.size();
195 }
196
197 return cnt / 2;
198}
199
200// ---------------------------------------------------------------------------------------------------------------------
201
202template<typename TNode, typename TEdge>
204 ext::set<TNode> set;
205
206 for (const auto &i: m_adjacency_list) {
207 set.insert(i.first);
208 }
209
210 return set;
211}
212
213template<typename TNode, typename TEdge>
216
217 for (const auto &i:m_adjacency_list) {
218 for (const auto &j : i.second) {
219 vec.push_back(j.second);
220 }
221 }
222
223 return vec;
224}
225
226// ---------------------------------------------------------------------------------------------------------------------
227
228template<typename TNode, typename TEdge>
230 ext::set<TNode> set;
231
232 if (m_adjacency_list.count(n) == 0) {
233 return set;
234 }
235
236 for (const auto &i: m_adjacency_list.at(n)) {
237 set.insert(i.first);
238 }
239
240 return set;
241}
242
243template<typename TNode, typename TEdge>
246
247 if (m_adjacency_list.count(n) == 0) {
248 return vec;
249 }
250
251 for (const auto &i: m_adjacency_list.at(n)) {
252 vec.push_back(i.second);
253 }
254
255 return vec;
256}
257
258template<typename TNode, typename TEdge>
260 return successors(n);
261}
262
263template<typename TNode, typename TEdge>
265 return successorEdges(n);
266}
267
268// ---------------------------------------------------------------------------------------------------------------------
269
270template<typename TNode, typename TEdge>
272 return "UndirectedGraph";
273}
274
275// ---------------------------------------------------------------------------------------------------------------------
276
277template<typename TNode, typename TEdge>
279 ostream << "(" << name() << " ";
280 for (const auto &i : m_adjacency_list) {
281 ostream << i.first << " <-->" << std::endl;
282
283 for (const auto &j : i.second) {
284 ostream << "\t\t" << j.first << " " << j.second << std::endl;
285 }
286 }
287 ostream << ")";
288}
289
290// ---------------------------------------------------------------------------------------------------------------------
291
292} // namespace graph
293
294// =====================================================================================================================
295
296namespace core {
297
304template<typename TNode, typename TEdge>
305struct normalize < graph::UndirectedGraph < TNode, TEdge > > {
308
309 for (auto &&i: ext::make_mover(std::move(value).getAdjacencyList())) {
311 for (auto &&j: i.second) {
314
315 graph.addNode(first);
316 graph.addNode(second);
317 graph.addEdge(edge);
318 }
319 }
320
321 return graph;
322 }
323};
324
325}
326
327// =====================================================================================================================
328
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: UndirectedGraph.hpp:26
ext::vector< TEdge > predecessorEdges(const TNode &n) const override
Definition: UndirectedGraph.hpp:264
ext::map< TNode, ext::map< TNode, TEdge > > m_adjacency_list
Definition: UndirectedGraph.hpp:36
ext::set< TNode > successors(const TNode &n) const override
Definition: UndirectedGraph.hpp:229
bool addEdge(Params &&... params)
Definition: UndirectedGraph.hpp:178
void addNode(Params &&... params)
Definition: UndirectedGraph.hpp:136
size_t nodeCount() const override
Definition: UndirectedGraph.hpp:185
ext::set< TNode > getNodes() const override
Definition: UndirectedGraph.hpp:203
void addNode(TNode &&n)
Definition: UndirectedGraph.hpp:130
size_t edgeCount() const override
Definition: UndirectedGraph.hpp:190
ext::vector< TEdge > getEdges() const override
Definition: UndirectedGraph.hpp:214
const ext::map< TNode, ext::map< TNode, TEdge > > & getAdjacencyList() const &
Definition: UndirectedGraph.hpp:112
ext::vector< TEdge > successorEdges(const TNode &n) const override
Definition: UndirectedGraph.hpp:244
std::string name() const override
Definition: UndirectedGraph.hpp:271
ext::set< TNode > predecessors(const TNode &n) const override
Definition: UndirectedGraph.hpp:259
void addNode(const TNode &n)
Definition: UndirectedGraph.hpp:125
void operator>>(ext::ostream &ostream) const override
Definition: UndirectedGraph.hpp:278
bool addEdge(TEdge &&e)
Definition: UndirectedGraph.hpp:160
bool operator==(const UndirectedGraph &other) const
Definition: UndirectedGraph.hpp:57
bool addEdge(const TEdge &e)
Definition: UndirectedGraph.hpp:143
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::UndirectedGraph< node::Node, edge::CapacityEdge< node::Node, int > > UndirectedGraph
Definition: FordFulkerson.hpp:26
Definition: ReconstructPath.hpp:14
static graph::UndirectedGraph eval(graph::UndirectedGraph< TNode, TEdge > &&value)
Definition: UndirectedGraph.hpp:306
Definition: normalize.hpp:13