graph_adjacency.hh 26.9 KB
Newer Older
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2014 Tiago de Paula Peixoto <tiago@skewed.de>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

18
19
20
21
22
23
24
25
#ifndef GRAPH_ADJACENCY_HH
#define GRAPH_ADJACENCY_HH

#include <vector>
#include <deque>
#include <utility>
#include <numeric>
#include <iostream>
Tiago Peixoto's avatar
Tiago Peixoto committed
26
#include <tuple>
27
28
29
30
31
32
33
34
#include <boost/iterator.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/range/irange.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/properties.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/iterator/iterator_facade.hpp>

35
36
#include "transform_iterator.hh"

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
namespace boost
{

// ========================================================================
// Forward declarations
// ========================================================================

template <class Vertex>
class adj_list;

// forward declaration of manipulation functions
template <class Vertex>
std::pair<typename adj_list<Vertex>::vertex_iterator,
          typename adj_list<Vertex>::vertex_iterator>
vertices(const adj_list<Vertex>& g);

template <class Vertex>
std::pair<typename adj_list<Vertex>::edge_iterator,
          typename adj_list<Vertex>::edge_iterator>
edges(const adj_list<Vertex>& g);

template <class Vertex>
59
std::pair<typename adj_list<Vertex>::edge_descriptor, bool>
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
edge(Vertex s, Vertex t, const adj_list<Vertex>& g);

template <class Vertex>
size_t out_degree(Vertex v, const adj_list<Vertex>& g);

template <class Vertex>
size_t in_degree(Vertex v, const adj_list<Vertex>& g);

template <class Vertex>
std::pair<typename adj_list<Vertex>::out_edge_iterator,
          typename adj_list<Vertex>::out_edge_iterator>
out_edges(Vertex v, const adj_list<Vertex>& g);

template <class Vertex>
std::pair<typename adj_list<Vertex>::in_edge_iterator,
          typename adj_list<Vertex>::in_edge_iterator>
in_edges(Vertex v, const adj_list<Vertex>& g);

template <class Vertex>
std::pair<typename adj_list<Vertex>::adjacency_iterator,
          typename adj_list<Vertex>::adjacency_iterator>
adjacent_vertices(Vertex v, const adj_list<Vertex>& g);

template <class Vertex>
size_t num_vertices(const adj_list<Vertex>& g);

template <class Vertex>
size_t num_edges(const adj_list<Vertex>& g);

template <class Vertex>
Vertex add_vertex(adj_list<Vertex>& g);

template <class Vertex>
void clear_vertex(Vertex v, adj_list<Vertex>& g);

template <class Vertex>
void remove_vertex(Vertex v, adj_list<Vertex>& g);

98
99
100
template <class Vertex>
void remove_vertex_fast(Vertex v, adj_list<Vertex>& g);

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
template <class Vertex>
std::pair<typename adj_list<Vertex>::edge_descriptor, bool>
add_edge(Vertex s, Vertex t, adj_list<Vertex>& g);

template <class Vertex>
void remove_edge(Vertex s, Vertex t, adj_list<Vertex>& g);

template <class Vertex>
void remove_edge(const typename adj_list<Vertex>::edge_descriptor& e,
                 adj_list<Vertex>& g);

// ========================================================================
// adj_list<Vertex>
// ========================================================================
//
// adj_list is a very simple adjacency list implementation for bidirectional
// graphs based on std::vector, meant to be reasonably efficient both
// memory-wise and computationally. It maintains a list of in and out-edges for
// each vertex, and each edge has a built-in index (which is replicated in both
// lists). For each edge, a total of 4 integers is necessary: the source and
// target vertices, in the in_edges and out_edges lists, respectively, and the
// (same) edge index in both lists. The integer type is given by the Vertex
// template parameter. It achieves about half as much memory as
// boost::adjacency_list with an edge index property map and the same integer
// type.

// The complexity guarantees and iterator invalidation rules are the same as
// boost::adjacency_list with vector storage selectors for both vertex and edge
// lists.

template <class Vertex = size_t>
class adj_list
{
public:
    struct graph_tag {};
    typedef Vertex vertex_t;
Tiago Peixoto's avatar
Tiago Peixoto committed
137
    typedef std::tuple<vertex_t, vertex_t, vertex_t> edge_descriptor;
138
139
140
    typedef std::vector<std::pair<vertex_t, vertex_t> > edge_list_t;
    typedef typename integer_range<Vertex>::iterator vertex_iterator;

141
142
    adj_list(): _n_edges(0), _last_idx(0), _keep_epos(false) {}

143
144
145
146
147
148
149
150
    struct get_vertex
    {
        get_vertex() {}
        typedef Vertex result_type;
        Vertex operator()(const std::pair<vertex_t, vertex_t>& v) const
        { return v.first; }
    };

151
    typedef transform_random_access_iterator<get_vertex, typename edge_list_t::const_iterator>
152
153
154
155
156
157
158
159
160
        adjacency_iterator;

    struct make_out_edge
    {
        make_out_edge(vertex_t src): _src(src) {}
        make_out_edge() {}
        vertex_t _src;
        typedef edge_descriptor result_type;
        edge_descriptor operator()(const std::pair<vertex_t, vertex_t>& v) const
Tiago Peixoto's avatar
Tiago Peixoto committed
161
        { return std::make_tuple(_src, v.first, v.second); }
162
163
164
165
166
167
168
169
170
    };

    struct make_in_edge
    {
        make_in_edge(vertex_t tgt): _tgt(tgt) {}
        make_in_edge() {}
        vertex_t _tgt;
        typedef edge_descriptor result_type;
        edge_descriptor operator()(const std::pair<vertex_t, vertex_t>& v) const
Tiago Peixoto's avatar
Tiago Peixoto committed
171
        { return std::make_tuple(v.first, _tgt, v.second); }
172
173
    };

174
    typedef transform_random_access_iterator<make_out_edge, typename edge_list_t::const_iterator>
175
        out_edge_iterator;
176
    typedef transform_random_access_iterator<make_in_edge, typename edge_list_t::const_iterator>
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
        in_edge_iterator;

    class edge_iterator:
        public boost::iterator_facade<edge_iterator,
                                      edge_descriptor,
                                      boost::forward_traversal_tag,
                                      edge_descriptor>
    {
    public:
        edge_iterator() {}
        explicit edge_iterator(const typename std::vector<edge_list_t>::const_iterator& vi_begin,
                               const typename std::vector<edge_list_t>::const_iterator& vi_end,
                               const typename std::vector<edge_list_t>::const_iterator& vi,
                               const typename edge_list_t::const_iterator& ei)
            : _vi_begin(vi_begin), _vi_end(vi_end), _vi(vi), _ei(ei)
        {
            // move position to first edge
            skip();
        }

    private:
        friend class boost::iterator_core_access;

        void skip()
        {
            //skip empty vertices
            while (_vi != _vi_end && _ei == _vi->end())
            {
                ++_vi;
                if (_vi != _vi_end)
                    _ei = _vi->begin();
            }
        }

        void increment()
        {
            ++_ei;
            skip();
        }

        bool equal(edge_iterator const& other) const
        {
            if (_vi_begin == _vi_end)
                return _vi == other._vi;
            return _vi == other._vi && _ei == other._ei;
        }

        edge_descriptor dereference() const
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
226
            return std::make_tuple(vertex_t(_vi - _vi_begin),
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
                                        _ei->first, _ei->second);
        }

        typename std::vector<edge_list_t>::const_iterator _vi_begin;
        typename std::vector<edge_list_t>::const_iterator _vi_end;
        typename std::vector<edge_list_t>::const_iterator _vi;
        typename edge_list_t::const_iterator _ei;
    };

    void reindex_edges()
    {
        _free_indexes.clear();
        _last_idx = 0;
        _in_edges.clear();
        _in_edges.resize(_out_edges.size());
        for (size_t i = 0; i < _out_edges.size(); ++i)
            for (size_t j = 0; j < _out_edges[i].size(); ++j)
            {
                Vertex v = _out_edges[i][j].first;
                _out_edges[i][j].second = _last_idx;
                _in_edges[v].push_back(std::make_pair(Vertex(i),
                                                      _last_idx));
                _last_idx++;
            }
251
252
253
254
255
256
257
258
259

        if (_keep_epos)
            rebuild_epos();
    }

    void set_keep_epos(bool keep)
    {
        if (keep)
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
260
            if (!_keep_epos)
261
262
263
264
265
266
267
268
269
270
271
272
                rebuild_epos();
        }
        else
        {
            _epos.clear();
        }
        _keep_epos = keep;
    }

    bool get_keep_epos()
    {
        return _keep_epos;
273
274
    }

275
    size_t get_last_index() const { return _last_idx; }
276

277
278
    static Vertex null_vertex() { return std::numeric_limits<Vertex>::max(); }

279
280
281
282
283
284
285
286
287
288
private:
    typedef std::vector<edge_list_t> vertex_list_t;
    vertex_list_t _out_edges;
    vertex_list_t _in_edges;
    size_t _n_edges;
    size_t _last_idx;
    std::deque<size_t> _free_indexes; // indexes of deleted edges to be used up
                                      // for new edges to avoid very large
                                      // indexes, and unnecessary property map
                                      // memory use
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    bool _keep_epos;
    std::vector<std::pair<int32_t, int32_t> > _epos;

    void rebuild_epos()
    {
        _epos.resize(_last_idx + 1);
        for (size_t i = 0; i < _out_edges.size(); ++i)
        {
            for (size_t j = 0; j < _out_edges[i].size(); ++j)
            {
                size_t idx = _out_edges[i][j].second;
                _epos[idx].first = j;
            }

            for (size_t j = 0; j < _in_edges[i].size(); ++j)
            {
                size_t idx = _in_edges[i][j].second;
                _epos[idx].second = j;
            }
        }
    }
310
311
312
313
314
315
316
317

    // manipulation functions
    friend std::pair<vertex_iterator, vertex_iterator>
    vertices<>(const adj_list<Vertex>& g);

    friend std::pair<edge_iterator, edge_iterator>
    edges<>(const adj_list<Vertex>& g);

318
    friend std::pair<edge_descriptor, bool>
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
    edge<>(Vertex s, Vertex t, const adj_list<Vertex>& g);

    friend size_t out_degree<>(Vertex v, const adj_list<Vertex>& g);

    friend size_t in_degree<>(Vertex v, const adj_list<Vertex>& g);

    friend std::pair<out_edge_iterator, out_edge_iterator>
    out_edges<>(Vertex v, const adj_list<Vertex>& g);

    friend std::pair<in_edge_iterator, in_edge_iterator>
    in_edges<>(Vertex v, const adj_list<Vertex>& g);

    friend std::pair<adjacency_iterator, adjacency_iterator>
    adjacent_vertices<>(Vertex v, const adj_list<Vertex>& g);

    friend size_t num_vertices<>(const adj_list<Vertex>& g);

    friend size_t num_edges<>(const adj_list<Vertex>& g);

    friend Vertex add_vertex<>(adj_list<Vertex>& g);

    friend void clear_vertex<>(Vertex v, adj_list<Vertex>& g);

    friend void remove_vertex<>(Vertex v, adj_list<Vertex>& g);

344
345
    friend void remove_vertex_fast<>(Vertex v, adj_list<Vertex>& g);

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
    friend std::pair<edge_descriptor, bool>
    add_edge<>(Vertex s, Vertex t, adj_list<Vertex>& g);

    friend void remove_edge<>(Vertex s, Vertex t, adj_list<Vertex>& g);

    friend void remove_edge<>(const edge_descriptor& e, adj_list<Vertex>& g);
};

//========================================================================
// Graph traits and BGL scaffolding
//========================================================================

struct adj_list_traversal_tag
    : public vertex_list_graph_tag,
      public edge_list_graph_tag,
      public adjacency_graph_tag,
      public bidirectional_graph_tag { };

template <class Vertex>
struct graph_traits<adj_list<Vertex> >
{
    typedef Vertex vertex_descriptor;
    typedef typename adj_list<Vertex>::edge_descriptor edge_descriptor;
    typedef typename adj_list<Vertex>::edge_iterator edge_iterator;
    typedef typename adj_list<Vertex>::adjacency_iterator adjacency_iterator;

    typedef typename adj_list<Vertex>::out_edge_iterator out_edge_iterator;
    typedef typename adj_list<Vertex>::in_edge_iterator in_edge_iterator;

    typedef typename adj_list<Vertex>::vertex_iterator vertex_iterator;

    typedef bidirectional_tag directed_category;
    typedef allow_parallel_edge_tag edge_parallel_category;
    typedef adj_list_traversal_tag traversal_category;

    typedef Vertex vertices_size_type;
    typedef Vertex edges_size_type;
    typedef size_t degree_size_type;

385
    static Vertex null_vertex() { return adj_list<Vertex>::null_vertex(); }
386
387
388
389
390
391
392
private:
    BOOST_STATIC_ASSERT((is_convertible<typename std::iterator_traits<out_edge_iterator>::iterator_category,
                                        std::random_access_iterator_tag>::value));
    BOOST_STATIC_ASSERT((is_convertible<typename std::iterator_traits<in_edge_iterator>::iterator_category,
                                        std::random_access_iterator_tag>::value));
    BOOST_STATIC_ASSERT((is_convertible<typename std::iterator_traits<adjacency_iterator>::iterator_category,
                                        std::random_access_iterator_tag>::value));
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
};

template <class Vertex>
struct graph_traits<const adj_list<Vertex> >
    : public graph_traits<adj_list<Vertex> >
{
};


template <class Vertex>
struct edge_property_type<adj_list<Vertex> >
{
    typedef void type;
};

template <class Vertex>
struct vertex_property_type<adj_list<Vertex> >
{
    typedef void type;
};

template <class Vertex>
struct graph_property_type<adj_list<Vertex> >
{
    typedef void type;
};

//========================================================================
// Graph access and manipulation functions
//========================================================================

template <class Vertex>
inline std::pair<typename adj_list<Vertex>::vertex_iterator,
                 typename adj_list<Vertex>::vertex_iterator>
vertices(const adj_list<Vertex>& g)
{
    typedef typename adj_list<Vertex>::vertex_iterator vi_t;
    return std::make_pair(vi_t(0), vi_t(g._out_edges.size()));
}


template <class Vertex>
inline std::pair<typename adj_list<Vertex>::edge_iterator,
                 typename adj_list<Vertex>::edge_iterator>
edges(const adj_list<Vertex>& g)
{
    typedef typename adj_list<Vertex>::edge_list_t::const_iterator ei_t;
    typedef typename adj_list<Vertex>::vertex_list_t::const_iterator vi_t;
    ei_t ei_begin = g._out_edges.empty() ? ei_t() : g._out_edges[0].begin();
    vi_t last_vi = g._out_edges.end();
    if (!g._out_edges.empty())
        --last_vi;
    ei_t ei_end = g._out_edges.empty() ? ei_t() : last_vi->end();
    typename adj_list<Vertex>::edge_iterator ebegin(g._out_edges.begin(),
                                                    g._out_edges.end(),
                                                    g._out_edges.begin(),
                                                    ei_begin);
    typename adj_list<Vertex>::edge_iterator eend(g._out_edges.begin(),
                                                  g._out_edges.end(),
                                                  last_vi,
                                                  ei_end);
    return std::make_pair(ebegin, eend);
}

template <class Vertex>
inline Vertex vertex(size_t i, const adj_list<Vertex>&)
{
    return i;
}

template <class Vertex>
464
inline std::pair<typename adj_list<Vertex>::edge_descriptor, bool>
465
466
edge(Vertex s, Vertex t, const adj_list<Vertex>& g)
{
Tiago Peixoto's avatar
Tiago Peixoto committed
467
    const auto& oes = g._out_edges[s];
468
469
    for (size_t i = 0; i < oes.size(); ++i)
        if (oes[i].first == t)
Tiago Peixoto's avatar
Tiago Peixoto committed
470
            return std::make_pair(std::make_tuple(s, t, oes[i].second), true);
471
    Vertex v = graph_traits<adj_list<Vertex> >::null_vertex();
Tiago Peixoto's avatar
Tiago Peixoto committed
472
    return std::make_pair(std::make_tuple(v, v, v), false);
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
}

template <class Vertex>
inline size_t out_degree(Vertex v, const adj_list<Vertex>& g)
{
    return g._out_edges[v].size();
}

template <class Vertex>
inline size_t in_degree(Vertex v, const adj_list<Vertex>& g)
{
    return g._in_edges[v].size();
}

template <class Vertex>
inline size_t degree(Vertex v, const adj_list<Vertex>& g)
{
    return in_degree(v, g) + out_degree(v, g);
}

template <class Vertex>
inline std::pair<typename adj_list<Vertex>::out_edge_iterator,
                 typename adj_list<Vertex>::out_edge_iterator>
out_edges(Vertex v, const adj_list<Vertex>& g)
{
    typedef typename adj_list<Vertex>::out_edge_iterator ei_t;
    typedef typename adj_list<Vertex>::make_out_edge mk_edge;
    return std::make_pair(ei_t(g._out_edges[v].begin(), mk_edge(v)),
                          ei_t(g._out_edges[v].end(), mk_edge(v)));
}

template <class Vertex>
inline std::pair<typename adj_list<Vertex>::in_edge_iterator,
                 typename adj_list<Vertex>::in_edge_iterator>
in_edges(Vertex v, const adj_list<Vertex>& g)
{
    typedef typename adj_list<Vertex>::in_edge_iterator ei_t;
    typedef typename adj_list<Vertex>::make_in_edge mk_edge;
    return std::make_pair(ei_t(g._in_edges[v].begin(), mk_edge(v)),
                          ei_t(g._in_edges[v].end(), mk_edge(v)));
}

template <class Vertex>
inline std::pair<typename adj_list<Vertex>::adjacency_iterator,
                 typename adj_list<Vertex>::adjacency_iterator>
adjacent_vertices(Vertex v, const adj_list<Vertex>& g)
{
    typedef typename adj_list<Vertex>::adjacency_iterator ai_t;
    return std::make_pair(ai_t(g._out_edges[v].begin()),
                          ai_t(g._out_edges[v].end()));
}

template <class Vertex>
inline size_t num_vertices(const adj_list<Vertex>& g)
{
    return g._out_edges.size();
}

template <class Vertex>
inline size_t num_edges(const adj_list<Vertex>& g)
{
    return g._n_edges;
}

template <class Vertex>
inline Vertex add_vertex(adj_list<Vertex>& g)
{
    size_t n = g._out_edges.size();
    g._out_edges.resize(n + 1);
    g._in_edges.resize(n + 1);
    return n;
}

template <class Vertex>
inline void clear_vertex(Vertex v, adj_list<Vertex>& g)
{
549
    if (!g._keep_epos)
550
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
551
        auto& oes = g._out_edges[v];
552
        for (size_t i = 0; i < oes.size(); ++i)
553
        {
554
            Vertex t = oes[i].first;
Tiago Peixoto's avatar
Tiago Peixoto committed
555
            auto& ies = g._in_edges[t];
556
            for (size_t j = 0; j < ies.size(); ++j)
557
            {
558
559
560
561
562
                if (ies[j].first == v)
                {
                    g._free_indexes.push_back(ies[j].second);
                    ies.erase(ies.begin() + j);
                }
563
564
            }
        }
565
566
        g._n_edges -= oes.size();
        oes.clear();
567

Tiago Peixoto's avatar
Tiago Peixoto committed
568
569
        auto& ies = g._in_edges[v];
        for (size_t i = 0; i < ies.size(); ++i)
570
        {
571
            Vertex s = ies[i].first;
Tiago Peixoto's avatar
Tiago Peixoto committed
572
            auto& oes = g._out_edges[s];
573
            for (size_t j = 0; j < oes.size(); ++j)
574
            {
575
576
577
578
579
                if (oes[j].first == v)
                {
                    g._free_indexes.push_back(oes[j].second);
                    oes.erase(oes.begin() + j);
                }
580
581
            }
        }
582
583
584
585
586
587
588
589
590
591
        g._n_edges -= ies.size();
        ies.clear();
    }
    else
    {
        typename adj_list<Vertex>::edge_list_t& oes = g._out_edges[v];
        for (size_t i = 0; i < oes.size(); ++i)
        {
            Vertex t = oes[i].first;
            size_t idx = oes[i].second;
Tiago Peixoto's avatar
Tiago Peixoto committed
592
593
            auto& ies = g._in_edges[t];
            const auto& pos = g._epos[idx];
594
595
596
597
598
599
600
601
602
603
604
605
606
607

            g._epos[ies.back().second].second = pos.second;
            ies[pos.second] = ies.back();
            ies.pop_back();
            g._free_indexes.push_back(idx);
        }
        g._n_edges -= oes.size();
        oes.clear();

        typename adj_list<Vertex>::edge_list_t& ies = g._in_edges[v];
        for (size_t i = 0; i < ies.size(); ++i)
        {
            Vertex s = ies[i].first;
            size_t idx = ies[i].second;
Tiago Peixoto's avatar
Tiago Peixoto committed
608
609
            auto& oes = g._out_edges[s];
            const auto& pos = g._epos[idx];
610
611
612
613
614
615
616
617

            g._epos[oes.back().second].first = pos.first;
            oes[pos.first] = oes.back();
            oes.pop_back();
            g._free_indexes.push_back(idx);
        }
        g._n_edges -= ies.size();
        ies.clear();
618
619
620
    }
}

621
// O(V + E)
622
623
624
625
626
627
template <class Vertex>
inline void remove_vertex(Vertex v, adj_list<Vertex>& g)
{
    clear_vertex(v, g);
    g._out_edges.erase(g._out_edges.begin() + v);
    g._in_edges.erase(g._in_edges.begin() + v);
628
629

    int i, N = g._out_edges.size();
Tiago Peixoto's avatar
Tiago Peixoto committed
630
631
    #pragma omp parallel for default(shared) private(i) \
        schedule(static) if (N > 100)
632
633
    for (i = 0; i < N; ++i)
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
634
635
        auto& oes = g._out_edges[i];
        for (size_t j = 0; j < oes.size(); ++j)
636
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
637
638
639
            auto& oe = oes[j];
            if (oe.first > v)
                oe.first--;
640
        }
Tiago Peixoto's avatar
Tiago Peixoto committed
641
642
643

        auto& ies = g._in_edges[i];
        for (size_t j = 0; j < ies.size(); ++j)
644
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
645
646
647
            auto& ie = ies[j];
            if (ie.first > v)
                ie.first--;
648
649
        }
    }
650
651
}

652
653
654
655
656
657
658
659
660
661
662
663
664
665
// O(k + k_last)
template <class Vertex>
inline void remove_vertex_fast(Vertex v, adj_list<Vertex>& g)
{
    clear_vertex(v, g);
    Vertex back = g._out_edges.size() - 1;

    if (v < back)
    {
        g._out_edges[v].swap(g._out_edges[back]);
        g._in_edges[v].swap(g._in_edges[back]);

        for (size_t i = 0; i < g._out_edges[v].size(); ++i)
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
666
667
            auto& eu = g._out_edges[v][i];
            Vertex u = eu.first;
668
669
            if (u == back)
            {
Tiago Peixoto's avatar
Tiago Peixoto committed
670
                eu.first = v;
671
672
673
            }
            else
            {
Tiago Peixoto's avatar
Tiago Peixoto committed
674
675
676
677
678
679
680
                auto& ies = g._in_edges[u];
                for (size_t j = 0; j < ies.size(); ++j)
                {
                    auto& e = ies[j];
                    if (e.first == back)
                        e.first = v;
                }
681
682
683
684
685
            }
        }

        for (size_t i = 0; i < g._in_edges[v].size(); ++i)
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
686
687
            auto& eu = g._in_edges[v][i];
            Vertex u = eu.first;
688
689
            if (u == back)
            {
Tiago Peixoto's avatar
Tiago Peixoto committed
690
                eu.first = v;
691
692
693
            }
            else
            {
Tiago Peixoto's avatar
Tiago Peixoto committed
694
695
696
697
698
699
700
                auto& oes = g._out_edges[u];
                for (size_t j = 0; j < oes.size(); ++j)
                {
                    auto& e = oes[j];
                    if (e.first == back)
                        e.first = v;
                }
701
702
703
704
705
706
707
            }
        }
    }

    g._out_edges.pop_back();
    g._in_edges.pop_back();
}
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727

template <class Vertex>
inline typename std::pair<typename adj_list<Vertex>::edge_descriptor, bool>
add_edge(Vertex s, Vertex t, adj_list<Vertex>& g)
{
    Vertex idx;
    if (g._free_indexes.empty())
    {
        idx = g._last_idx++;
    }
    else
    {
        idx = g._free_indexes.front();
        g._free_indexes.pop_front();
    }

    g._out_edges[s].push_back(std::make_pair(t, idx));
    g._in_edges[t].push_back(std::make_pair(s, idx));
    g._n_edges++;

728
729
730
731
732
733
734
735
    if (g._keep_epos)
    {
        if (idx >= g._epos.size())
            g._epos.resize(idx + 1);
        g._epos[idx].first = g._out_edges[s].size() - 1;
        g._epos[idx].second = g._in_edges[t].size() - 1;
    }

Tiago Peixoto's avatar
Tiago Peixoto committed
736
    return std::make_pair(std::make_tuple(s, t, idx), true);
737
738
739
740
741
742
}

template <class Vertex>
inline void remove_edge(Vertex s, Vertex t,
                        adj_list<Vertex>& g)
{
743
    if (!g._keep_epos)
744
    {
Tiago Peixoto's avatar
Tiago Peixoto committed
745
        auto& oes = g._out_edges[s];
746
747
748
749
750
751
752
753
754
755
        for (size_t i = 0; i < oes.size(); ++i)
        {
            if (t == oes[i].first)
            {
                g._free_indexes.push_back(oes[i].second);
                oes.erase(oes.begin() + i);
                g._n_edges--;
            }
        }

Tiago Peixoto's avatar
Tiago Peixoto committed
756
        auto& ies = g._in_edges[t];
757
        for (size_t i = 0; i < ies.size(); ++i)
758
        {
759
760
            if (s == ies[i].first)
                ies.erase(ies.begin() + i);
761
762
        }
    }
763
    else
764
    {
765
        remove_edge(edge(s, t, g).first, g);
766
767
768
769
770
771
772
773
774
775
    }
}

template <class Vertex>
inline void remove_edge(const typename adj_list<Vertex>::edge_descriptor& e,
                        adj_list<Vertex>& g)
{
    Vertex s = get<0>(e);
    Vertex t = get<1>(e);
    Vertex idx = get<2>(e);
Tiago Peixoto's avatar
Tiago Peixoto committed
776
777
    auto& oes = g._out_edges[s];
    auto& ies = g._in_edges[t];
778

779
    bool found = false;
780
    if (!g._keep_epos) // O(k_s + k_t)
781
    {
782
        for (size_t i = 0; i < oes.size(); ++i)
783
        {
784
785
786
            if (t == oes[i].first && idx == oes[i].second)
            {
                oes.erase(oes.begin() + i);
787
                found = true;
788
789
                break;
            }
790
791
        }

792
        for (size_t i = 0; i < ies.size(); ++i)
793
        {
794
795
796
            if (s == ies[i].first && idx == ies[i].second)
            {
                ies.erase(ies.begin() + i);
797
                found = true;
798
799
                break;
            }
800
801
        }
    }
802
803
    else // O(1)
    {
804
805
        if (idx < g._epos.size())
        {
Tiago Peixoto's avatar
Tiago Peixoto committed
806
            const auto& pos = g._epos[idx];
807

808
809
810
            g._epos[oes.back().second].first = pos.first;
            oes[pos.first] = oes.back();
            oes.pop_back();
811

812
813
814
815
816
817
818
819
820
821
822
823
            g._epos[ies.back().second].second = pos.second;
            ies[pos.second] = ies.back();
            ies.pop_back();

            found = true;
        }
    }

    if (found)
    {
        g._free_indexes.push_back(idx);
        g._n_edges--;
824
    }
825
826
}

827

828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
template <class Vertex>
inline Vertex source(const typename adj_list<Vertex>::edge_descriptor& e,
                     const adj_list<Vertex>&)
{
    return get<0>(e);
}

template <class Vertex>
inline Vertex target(const typename adj_list<Vertex>::edge_descriptor& e,
                     const adj_list<Vertex>&)
{
    return get<1>(e);

}

//========================================================================
// Vertex and edge index property maps
//========================================================================

template <class Vertex>
struct property_map<adj_list<Vertex>, vertex_index_t>
{
    typedef identity_property_map type;
    typedef type const_type;
};

template <class Vertex>
struct property_map<const adj_list<Vertex>, vertex_index_t>
{
    typedef identity_property_map type;
    typedef type const_type;
};

template <class Vertex>
inline identity_property_map
get(vertex_index_t, adj_list<Vertex>&)
{
    return identity_property_map();
}

template <class Vertex>
inline identity_property_map
get(vertex_index_t, const adj_list<Vertex>&)
{
    return identity_property_map();
}

struct get_edge_index
{
    typedef size_t result_type;
    template <class Vertex>
    size_t operator()(const typename adj_list<Vertex>::edge_descriptor& e) const
    {
        return get<2>(e);
    }
};

template<class Vertex>
class adj_edge_index_property_map:
    public put_get_helper<Vertex, adj_edge_index_property_map<Vertex> >
{
public:
    typedef typename adj_list<Vertex>::edge_descriptor key_type;
    typedef Vertex reference;
    typedef Vertex value_type;
    typedef boost::readable_property_map_tag category;

    reference operator[](const key_type& k) const {return get<2>(k);}
};

template <class Vertex>
struct property_map<adj_list<Vertex>, edge_index_t>
{
    typedef adj_edge_index_property_map<Vertex> type;
    typedef type const_type;

};

template <class Vertex>
inline adj_edge_index_property_map<Vertex>
get(edge_index_t, const adj_list<Vertex>&)
{
    return adj_edge_index_property_map<Vertex>();
}

} // namespace boost

#endif //GRAPH_ADJACENCY_HH