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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
/*
* Copyright (c) 2018, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* \file
* \brief Adaptor that presents an undirected view of a bidirectional BGL graph.
*
* Analogous to the reverse_graph adapter. You can construct one of these for
* bidirectional graph g with:
*
* auto ug = make_undirected_graph(g);
*
* The vertex descriptor type is the same as that of the underlying graph, but
* the edge descriptor is different.
*/
#ifndef GRAPH_UNDIRECTED_H
#define GRAPH_UNDIRECTED_H
#include "util/operators.h"
#include <boost/graph/adjacency_iterator.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/properties.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <type_traits>
#include <utility>
namespace ue2 {
struct undirected_graph_tag {};
template <class BidirectionalGraph, class GraphRef>
class undirected_graph;
namespace undirected_detail {
template <typename BidirectionalGraph>
class undirected_graph_edge_descriptor
: totally_ordered<undirected_graph_edge_descriptor<BidirectionalGraph>> {
using base_graph_type = BidirectionalGraph;
using base_graph_traits = typename boost::graph_traits<base_graph_type>;
using base_edge_type = typename base_graph_traits::edge_descriptor;
using base_vertex_type = typename base_graph_traits::vertex_descriptor;
base_edge_type underlying_edge;
const base_graph_type *g = nullptr;
bool reverse = false; // if true, reverse vertices in source() and target()
inline std::pair<base_vertex_type, base_vertex_type>
canonical_edge() const {
auto u = std::min(source(underlying_edge, *g),
target(underlying_edge, *g));
auto v = std::max(source(underlying_edge, *g),
target(underlying_edge, *g));
return std::make_pair(u, v);
}
template <class BidiGraph, class GraphRef>
friend class ::ue2::undirected_graph;
public:
undirected_graph_edge_descriptor() = default;
undirected_graph_edge_descriptor(base_edge_type edge,
const base_graph_type &g_in,
bool reverse_in)
: underlying_edge(std::move(edge)), g(&g_in), reverse(reverse_in) {}
bool operator==(const undirected_graph_edge_descriptor &other) const {
return canonical_edge() == other.canonical_edge();
}
bool operator<(const undirected_graph_edge_descriptor &other) const {
return canonical_edge() < other.canonical_edge();
}
base_vertex_type get_source() const {
return reverse ? target(underlying_edge, *g)
: source(underlying_edge, *g);
}
base_vertex_type get_target() const {
return reverse ? source(underlying_edge, *g)
: target(underlying_edge, *g);
}
};
} // namespace undirected_detail
template <class BidirectionalGraph, class GraphRef = const BidirectionalGraph &>
class undirected_graph {
private:
using Self = undirected_graph<BidirectionalGraph, GraphRef>;
using Traits = boost::graph_traits<BidirectionalGraph>;
public:
using base_type = BidirectionalGraph;
using base_ref_type = GraphRef;
explicit undirected_graph(GraphRef g_in) : g(g_in) {}
// Graph requirements
using vertex_descriptor = typename Traits::vertex_descriptor;
using edge_descriptor =
undirected_detail::undirected_graph_edge_descriptor<base_type>;
using directed_category = boost::undirected_tag;
using edge_parallel_category = boost::disallow_parallel_edge_tag;
using traversal_category = typename Traits::traversal_category;
// IncidenceGraph requirements
/**
* \brief Templated iterator used for out_edge_iterator and
* in_edge_iterator, depending on the value of Reverse.
*/
template <bool Reverse>
class adj_edge_iterator
: public boost::iterator_facade<
adj_edge_iterator<Reverse>, edge_descriptor,
boost::forward_traversal_tag, edge_descriptor> {
vertex_descriptor u;
const base_type *g;
typename Traits::in_edge_iterator in_it;
typename Traits::out_edge_iterator out_it;
bool done_in = false;
public:
adj_edge_iterator() = default;
adj_edge_iterator(vertex_descriptor u_in, const base_type &g_in,
bool end_iter)
: u(std::move(u_in)), g(&g_in) {
auto pi = in_edges(u, *g);
auto po = out_edges(u, *g);
if (end_iter) {
in_it = pi.second;
out_it = po.second;
done_in = true;
} else {
in_it = pi.first;
out_it = po.first;
if (in_it == pi.second) {
done_in = true;
find_first_valid_out();
}
}
}
private:
friend class boost::iterator_core_access;
void find_first_valid_out() {
auto out_end = out_edges(u, *g).second;
for (; out_it != out_end; ++out_it) {
auto v = target(*out_it, *g);
if (!edge(v, u, *g).second) {
break;
}
}
}
void increment() {
if (!done_in) {
auto in_end = in_edges(u, *g).second;
assert(in_it != in_end);
++in_it;
if (in_it == in_end) {
done_in = true;
find_first_valid_out();
}
} else {
++out_it;
find_first_valid_out();
}
}
bool equal(const adj_edge_iterator &other) const {
return in_it == other.in_it && out_it == other.out_it;
}
edge_descriptor dereference() const {
if (done_in) {
return edge_descriptor(*out_it, *g, Reverse);
} else {
return edge_descriptor(*in_it, *g, !Reverse);
}
}
};
using out_edge_iterator = adj_edge_iterator<false>;
using in_edge_iterator = adj_edge_iterator<true>;
using degree_size_type = typename Traits::degree_size_type;
// AdjacencyGraph requirements
using adjacency_iterator =
typename boost::adjacency_iterator_generator<Self, vertex_descriptor,
out_edge_iterator>::type;
using inv_adjacency_iterator =
typename boost::inv_adjacency_iterator_generator<
Self, vertex_descriptor, in_edge_iterator>::type;
// VertexListGraph requirements
using vertex_iterator = typename Traits::vertex_iterator;
// EdgeListGraph requirements
enum {
is_edge_list = std::is_convertible<traversal_category,
boost::edge_list_graph_tag>::value
};
/** \brief Iterator used for edges(). */
class edge_iterator
: public boost::iterator_facade<edge_iterator, edge_descriptor,
boost::forward_traversal_tag,
edge_descriptor> {
const base_type *g;
typename Traits::edge_iterator it;
public:
edge_iterator() = default;
edge_iterator(typename Traits::edge_iterator it_in,
const base_type &g_in)
: g(&g_in), it(std::move(it_in)) {
find_first_valid_edge();
}
private:
friend class boost::iterator_core_access;
void find_first_valid_edge() {
const auto end = edges(*g).second;
for (; it != end; ++it) {
const auto &u = source(*it, *g);
const auto &v = target(*it, *g);
if (!edge(v, u, *g).second) {
break; // No reverse edge, we must visit this one
}
if (u <= v) {
// We have a reverse edge, but we'll return this one (and
// skip the other). Note that (u, u) shouldn't be skipped.
break;
}
}
}
void increment() {
assert(it != edges(*g).second);
++it;
find_first_valid_edge();
}
bool equal(const edge_iterator &other) const {
return it == other.it;
}
edge_descriptor dereference() const {
return edge_descriptor(*it, *g, false);
}
};
using vertices_size_type = typename Traits::vertices_size_type;
using edges_size_type = typename Traits::edges_size_type;
using graph_tag = undirected_graph_tag;
using vertex_bundle_type =
typename boost::vertex_bundle_type<base_type>::type;
using edge_bundle_type = typename boost::edge_bundle_type<base_type>::type;
vertex_bundle_type &operator[](const vertex_descriptor &d) {
return const_cast<base_type &>(g)[d];
}
const vertex_bundle_type &operator[](const vertex_descriptor &d) const {
return g[d];
}
edge_bundle_type &operator[](const edge_descriptor &d) {
return const_cast<base_type &>(g)[d.underlying_edge];
}
const edge_bundle_type &operator[](const edge_descriptor &d) const {
return g[d.underlying_edge];
}
static vertex_descriptor null_vertex() { return Traits::null_vertex(); }
// Accessor free functions follow
friend std::pair<vertex_iterator, vertex_iterator>
vertices(const undirected_graph &ug) {
return vertices(ug.g);
}
friend std::pair<edge_iterator, edge_iterator>
edges(const undirected_graph &ug) {
auto e = edges(ug.g);
return std::make_pair(edge_iterator(e.first, ug.g),
edge_iterator(e.second, ug.g));
}
friend std::pair<out_edge_iterator, out_edge_iterator>
out_edges(const vertex_descriptor &u, const undirected_graph &ug) {
return std::make_pair(out_edge_iterator(u, ug.g, false),
out_edge_iterator(u, ug.g, true));
}
friend vertices_size_type num_vertices(const undirected_graph &ug) {
return num_vertices(ug.g);
}
friend edges_size_type num_edges(const undirected_graph &ug) {
auto p = edges(ug);
return std::distance(p.first, p.second);
}
friend degree_size_type out_degree(const vertex_descriptor &u,
const undirected_graph &ug) {
return degree(u, ug);
}
friend vertex_descriptor vertex(vertices_size_type n,
const undirected_graph &ug) {
return vertex(n, ug.g);
}
friend std::pair<edge_descriptor, bool> edge(const vertex_descriptor &u,
const vertex_descriptor &v,
const undirected_graph &ug) {
auto e = edge(u, v, ug.g);
if (e.second) {
return std::make_pair(edge_descriptor(e.first, ug.g, false), true);
}
auto e_rev = edge(v, u, ug.g);
if (e_rev.second) {
return std::make_pair(edge_descriptor(e_rev.first, ug.g, true),
true);
}
return std::make_pair(edge_descriptor(), false);
}
friend std::pair<in_edge_iterator, in_edge_iterator>
in_edges(const vertex_descriptor &v, const undirected_graph &ug) {
return std::make_pair(in_edge_iterator(v, ug.g, false),
in_edge_iterator(v, ug.g, true));
}
friend std::pair<adjacency_iterator, adjacency_iterator>
adjacent_vertices(const vertex_descriptor &u, const undirected_graph &ug) {
out_edge_iterator oi, oe;
std::tie(oi, oe) = out_edges(u, ug);
return std::make_pair(adjacency_iterator(oi, &ug),
adjacency_iterator(oe, &ug));
}
friend std::pair<inv_adjacency_iterator, inv_adjacency_iterator>
inv_adjacent_vertices(const vertex_descriptor &v,
const undirected_graph &ug) {
in_edge_iterator ei, ee;
std::tie(ei, ee) = in_edges(v, ug);
return std::make_pair(inv_adjacency_iterator(ei, &ug),
inv_adjacency_iterator(ee, &ug));
}
friend degree_size_type in_degree(const vertex_descriptor &v,
const undirected_graph &ug) {
return degree(v, ug);
}
friend vertex_descriptor source(const edge_descriptor &e,
const undirected_graph &) {
return e.get_source();
}
friend vertex_descriptor target(const edge_descriptor &e,
const undirected_graph &) {
return e.get_target();
}
friend degree_size_type degree(const vertex_descriptor &u,
const undirected_graph &ug) {
auto p = out_edges(u, ug);
return std::distance(p.first, p.second);
}
// Property accessors.
template <typename Property>
using prop_map = typename boost::property_map<undirected_graph, Property>;
template <typename Property>
friend typename prop_map<Property>::type
get(Property p, undirected_graph &ug) {
return get(p, ug.g);
}
template <typename Property>
friend typename prop_map<Property>::const_type
get(Property p, const undirected_graph &ug) {
return get(p, ug.g);
}
template <typename Property, typename Key>
friend typename boost::property_traits<
typename prop_map<Property>::const_type>::value_type
get(Property p, const undirected_graph &ug, const Key &k) {
return get(p, ug.g, get_underlying_descriptor(k));
}
template <typename Property, typename Value, typename Key>
friend void put(Property p, const undirected_graph &ug,
const Key &k, const Value &val) {
put(p, const_cast<BidirectionalGraph &>(ug.g),
get_underlying_descriptor(k), val);
}
private:
// Accessors are here because our free friend functions (above) cannot see
// edge_descriptor's private members.
static typename base_type::vertex_descriptor
get_underlying_descriptor(const vertex_descriptor &v) {
return v;
}
static typename base_type::edge_descriptor
get_underlying_descriptor(const edge_descriptor &e) {
return e.underlying_edge;
}
// Reference to underlying bidirectional graph
GraphRef g;
};
template <class BidirectionalGraph>
undirected_graph<BidirectionalGraph>
make_undirected_graph(const BidirectionalGraph &g) {
return undirected_graph<BidirectionalGraph>(g);
}
} // namespace ue2
namespace boost {
/* Derive all the property map specializations from the underlying
* bidirectional graph. */
template <typename BidirectionalGraph, typename GraphRef, typename Property>
struct property_map<ue2::undirected_graph<BidirectionalGraph, GraphRef>,
Property> {
using base_map_type = property_map<BidirectionalGraph, Property>;
using type = typename base_map_type::type;
using const_type = typename base_map_type::const_type;
};
template <class BidirectionalGraph, class GraphRef>
struct vertex_property_type<ue2::undirected_graph<BidirectionalGraph, GraphRef>>
: vertex_property_type<BidirectionalGraph> {};
template <class BidirectionalGraph, class GraphRef>
struct edge_property_type<ue2::undirected_graph<BidirectionalGraph, GraphRef>>
: edge_property_type<BidirectionalGraph> {};
template <class BidirectionalGraph, class GraphRef>
struct graph_property_type<ue2::undirected_graph<BidirectionalGraph, GraphRef>>
: graph_property_type<BidirectionalGraph> {};
template <typename BidirectionalGraph, typename GraphRef>
struct vertex_bundle_type<ue2::undirected_graph<BidirectionalGraph, GraphRef>>
: vertex_bundle_type<BidirectionalGraph> {};
template <typename BidirectionalGraph, typename GraphRef>
struct edge_bundle_type<ue2::undirected_graph<BidirectionalGraph, GraphRef>>
: edge_bundle_type<BidirectionalGraph> {};
template <typename BidirectionalGraph, typename GraphRef>
struct graph_bundle_type<ue2::undirected_graph<BidirectionalGraph, GraphRef>>
: graph_bundle_type<BidirectionalGraph> {};
} // namespace boost
#endif // GRAPH_UNDIRECTED_H
|