-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
306 lines (243 loc) · 10.4 KB
/
graph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// MIT License
// graph - http://www.github/ilariom/graph
// Copyright (c) 2020 - Ilario Mangano
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef graph_h
#define graph_h
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <limits>
#include <algorithm>
namespace estd
{
#include "search_algorithm.inl"
template <typename T, typename V = ssize_t>
class graph
{
public:
using value_type = T;
using weight_type = V;
using size_type = size_t;
using id_type = size_t;
using nodes_container = std::vector<id_type>;
using parent_array = std::vector<id_type>;
using path_array = std::vector<id_type>;
static constexpr const id_type null_id = std::numeric_limits<id_type>::max();
public:
class path
{
public:
path(
parent_array&& parents,
std::vector<weight_type>&& distances,
graph<T, V>::id_type root
)
: parents_(parents), distances_(distances), root_(root)
{ }
path() = default;
public:
graph<T, V>::id_type root() const { return root_; }
path_array path_to(id_type node) const;
weight_type distance_to(id_type node) const { return distances_[node]; }
private :
parent_array parents_;
std::vector<weight_type> distances_;
graph<T, V>::id_type root_ = graph<T, V>::null_id;
};
template <typename container_type>
class search_iterator
{
public:
search_iterator(
const graph<T, V>& G,
const std::vector<std::vector<graph<T, V>::id_type>>& children,
graph<T, V>::id_type root = graph<T, V>::null_id
) : G_(G), children_(children), curr_(root), root_(root)
{
frontier_.set_graph(G_);
frontier_.push(root);
step();
}
search_iterator(const search_iterator& it)
: G_(it.G_), children_(it.children_), curr_(it.curr_), root_(it.root_)
{
frontier_.set_graph(G_);
frontier_.push(curr_);
step();
}
public:
graph<T, V>::id_type operator*() const { return curr_; }
search_iterator& operator++();
graph<T, V>::weight_type operator-(const search_iterator& other) const;
std::vector<graph<T, V>::id_type> operator<(const search_iterator& other) const;
std::vector<graph<T, V>::id_type> operator>(const search_iterator& other) const { return other < *this; }
graph<T, V>::path operator>(const graph<T, V>&) const;
bool operator==(const search_iterator& other) const { return curr_ == other.curr_; }
bool operator!=(const search_iterator& other) const { return !(*this == other); }
void prune() { prune_ = true; }
void rewind();
graph<T, V>::id_type peek() const { return frontier_.empty() ? graph<T, V>::null_id : frontier_.top(); }
private:
void step();
private:
const graph<T, V>& G_;
std::unordered_set<id_type> E_;
container_type frontier_;
const std::vector<std::vector<graph<T, V>::id_type>>& children_;
graph<T, V>::id_type curr_;
graph<T, V>::id_type root_;
bool prune_ = false;
};
class node_iterator
{
public:
node_iterator(const graph<T, V>& G, graph<T, V>::id_type v = graph<T, V>::null_id)
: G_(G), v_(v)
{ }
public:
id_type operator*() const { return v_; }
node_iterator& operator++();
node_iterator& operator--();
node_iterator& operator+(size_t n);
node_iterator& operator-(size_t n);
bool operator==(const node_iterator& other) const { return v_ == other.v_; }
bool operator!=(const node_iterator& other) const { return !(*this == other); }
private:
const graph<T, V>& G_;
graph<T, V>::id_type v_;
};
using edge_type = std::pair<graph<T, V>::id_type, graph<T, V>::id_type>;
class edge_iterator
{
public:
edge_iterator(const graph<T, V>& G, graph<T, V>::id_type u = graph<T, V>::null_id)
: G_(G), it_{ G_, u }
{
++*this;
}
public:
edge_type operator*() const { return { u_, v_ }; }
edge_iterator& operator++();
bool operator==(const edge_iterator& other) const { return u_ == other.u_ && v_ == other.v_; }
bool operator!=(const edge_iterator& other) const { return !(*this == other); }
private:
bool ensure_validity();
private:
const graph<T, V>& G_;
node_iterator it_;
size_t adjs_idx_ = 0;
id_type u_ = graph<T, V>::null_id;
id_type v_ = graph<T, V>::null_id;
};
public:
id_type insert(typename std::conditional<std::is_arithmetic<value_type>::value, value_type, const value_type&>::type);
void erase(id_type);
void erase(const nodes_container&);
void erase(id_type, id_type);
size_type degree(id_type node) const { return in(node).size() + out(node).size(); }
size_type order() const { return objs_.size() - removed_nodes_; }
size_type size() const;
bool empty() const { return order() == 0; }
const nodes_container& in(id_type node) const { return radjs_[node]; }
const nodes_container& out(id_type node) const { return adjs_[node]; }
T& operator[](id_type node) { return objs_[node]; }
const T& operator[](id_type node) const { return objs_[node]; }
template <typename search_algorithm>
search_iterator<search_algorithm> begin(id_type root) const { return search_iterator<search_algorithm> { *this, adjs_, root }; }
template <typename search_algorithm>
search_iterator<search_algorithm> end() const { return search_iterator<search_algorithm> { *this, adjs_ }; }
template <typename search_algorithm>
search_iterator<search_algorithm> rbegin(id_type root) const { return search_iterator<search_algorithm> { *this, radjs_, root }; }
template <typename search_algorithm>
search_iterator<search_algorithm> rend() const { return search_iterator<search_algorithm> { *this, radjs_ }; }
void edge(id_type node, id_type child, weight_type w = 1);
weight_type weight(id_type node, id_type child) const;
bool is_weighted() const { return weighted_; }
edge_iterator edges_begin() const { return edge_iterator { *this, 0 }; }
edge_iterator edges_end() const { return edge_iterator { *this }; }
node_iterator nodes_begin() const { return node_iterator { *this, 0 }; }
node_iterator nodes_end() const { return node_iterator { *this }; }
bool is_valid(id_type node) const { return invalid_nodes_.find(node) == invalid_nodes_.end(); }
private:
std::vector<nodes_container> adjs_;
std::vector<nodes_container> radjs_;
std::vector<std::unordered_map<id_type, weight_type>> ws_;
std::vector<std::unordered_map<id_type, weight_type>> rws_;
std::vector<value_type> objs_;
std::unordered_set<id_type> invalid_nodes_;
size_type removed_nodes_ = 0;
bool weighted_ = false;
};
template <typename T, typename V>
class undirected_graph : public graph<T, V>
{
public:
using value_type = typename graph<T, V>::value_type;
using weight_type = typename graph<T, V>::weight_type;
using size_type = typename graph<T, V>::size_type;
using id_type = typename graph<T, V>::id_type;
using nodes_container = typename graph<T, V>::nodes_container;
public:
void edge(undirected_graph::id_type node, undirected_graph::id_type o, undirected_graph::weight_type w = 1)
{
graph<T, V>::edge(node, o, w);
graph<T, V>::edge(o, node, w);
}
const nodes_container& adjs(id_type node) const { return graph<T, V>::out(node); }
};
template <typename value_type, typename weight_type>
using weighted_digraph = graph<value_type, weight_type>;
template <typename value_type>
using digraph = graph<value_type>;
template <typename value_type, typename weight_type>
using weighted_undirected_graph = undirected_graph<value_type, weight_type>;
template <typename T>
class tree : private graph<T>
{
public:
using value_type = typename graph<T>::value_type;
using size_type = typename graph<T>::size_type;
using id_type = typename graph<T>::id_type;
using nodes_container = typename graph<T>::nodes_container;
using graph<T>::null_id;
public:
size_type order() const { return graph<T>::order(); }
size_type size() const { return order() > 0 ? order() - 1 : 0; }
id_type parent(id_type node) const { return graph<T>::in(node).size() > 0 ? graph<T>::out(node)[0] : tree<T>::null_id; }
const nodes_container& children(id_type node) const { return graph<T>::out(node); }
void append(id_type node, id_type child) { graph<T>::edge(node, child); };
using graph<T>::insert;
using graph<T>::erase;
using graph<T>::edges_begin;
using graph<T>::edges_end;
using graph<T>::nodes_begin;
using graph<T>::nodes_end;
using graph<T>::begin;
using graph<T>::end;
using graph<T>::operator[];
using graph<T>::is_valid;
};
template <typename T, typename V>
typename graph<T, V>::path bfs_distance(const graph<T, V>& G, typename graph<T, V>::id_type root);
template <typename T, typename V>
typename graph<T, V>::path bellman_ford(const graph<T, V>& G, typename graph<T, V>::id_type root);
#include "graph.inl"
} // namespace estd
#endif /* graph_h */