lemon/elevator.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 17 Nov 2008 15:41:15 +0000
changeset 379 1bab3a47be88
child 380 d916b8995e22
permissions -rw-r--r--
Port Elevator from svn -r3516 (#174)
- the unify script hes also been applied
alpar@379
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@379
     2
 *
alpar@379
     3
 * This file is a part of LEMON, a generic C++ optimization library.
alpar@379
     4
 *
alpar@379
     5
 * Copyright (C) 2003-2008
alpar@379
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@379
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@379
     8
 *
alpar@379
     9
 * Permission to use, modify and distribute this software is granted
alpar@379
    10
 * provided that this copyright notice appears in all copies. For
alpar@379
    11
 * precise terms see the accompanying LICENSE file.
alpar@379
    12
 *
alpar@379
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@379
    14
 * express or implied, and with no claim as to its suitability for any
alpar@379
    15
 * purpose.
alpar@379
    16
 *
alpar@379
    17
 */
alpar@379
    18
alpar@379
    19
#ifndef LEMON_ELEVATOR_H
alpar@379
    20
#define LEMON_ELEVATOR_H
alpar@379
    21
alpar@379
    22
///\ingroup auxdat
alpar@379
    23
///\file
alpar@379
    24
///\brief Elevator class
alpar@379
    25
///
alpar@379
    26
///Elevator class implements an efficient data structure
alpar@379
    27
///for labeling items in push-relabel type algorithms.
alpar@379
    28
///
alpar@379
    29
alpar@379
    30
#include <test/test_tools.h>
alpar@379
    31
namespace lemon {
alpar@379
    32
alpar@379
    33
  ///Class for handling "labels" in push-relabel type algorithms.
alpar@379
    34
alpar@379
    35
  ///A class for handling "labels" in push-relabel type algorithms.
alpar@379
    36
  ///
alpar@379
    37
  ///\ingroup auxdat
alpar@379
    38
  ///Using this class you can assign "labels" (nonnegative integer numbers)
alpar@379
    39
  ///to the edges or nodes of a graph, manipulate and query them through
alpar@379
    40
  ///operations typically arising in "push-relabel" type algorithms.
alpar@379
    41
  ///
alpar@379
    42
  ///Each item is either \em active or not, and you can also choose a
alpar@379
    43
  ///highest level active item.
alpar@379
    44
  ///
alpar@379
    45
  ///\sa LinkedElevator
alpar@379
    46
  ///
alpar@379
    47
  ///\param Graph the underlying graph type
alpar@379
    48
  ///\param Item Type of the items the data is assigned to (Graph::Node,
alpar@379
    49
  ///Graph::Edge, Graph::UEdge)
alpar@379
    50
  template<class Graph, class Item>
alpar@379
    51
  class Elevator
alpar@379
    52
  {
alpar@379
    53
  public:
alpar@379
    54
alpar@379
    55
    typedef Item Key;
alpar@379
    56
    typedef int Value;
alpar@379
    57
alpar@379
    58
  private:
alpar@379
    59
alpar@379
    60
    typedef typename std::vector<Item>::iterator Vit;
alpar@379
    61
    typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap;
alpar@379
    62
    typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap;
alpar@379
    63
alpar@379
    64
    const Graph &_g;
alpar@379
    65
    int _max_level;
alpar@379
    66
    int _item_num;
alpar@379
    67
    VitMap _where;
alpar@379
    68
    IntMap _level;
alpar@379
    69
    std::vector<Item> _items;
alpar@379
    70
    std::vector<Vit> _first;
alpar@379
    71
    std::vector<Vit> _last_active;
alpar@379
    72
alpar@379
    73
    int _highest_active;
alpar@379
    74
alpar@379
    75
    void copy(Item i, Vit p)
alpar@379
    76
    {
alpar@379
    77
      _where[*p=i]=p;
alpar@379
    78
    }
alpar@379
    79
    void copy(Vit s, Vit p)
alpar@379
    80
    {
alpar@379
    81
      if(s!=p)
alpar@379
    82
        {
alpar@379
    83
          Item i=*s;
alpar@379
    84
          *p=i;
alpar@379
    85
          _where[i]=p;
alpar@379
    86
        }
alpar@379
    87
    }
alpar@379
    88
    void swap(Vit i, Vit j)
alpar@379
    89
    {
alpar@379
    90
      Item ti=*i;
alpar@379
    91
      Vit ct = _where[ti];
alpar@379
    92
      _where[ti]=_where[*i=*j];
alpar@379
    93
      _where[*j]=ct;
alpar@379
    94
      *j=ti;
alpar@379
    95
    }
alpar@379
    96
alpar@379
    97
  public:
alpar@379
    98
alpar@379
    99
    ///Constructor with given maximum level.
alpar@379
   100
alpar@379
   101
    ///Constructor with given maximum level.
alpar@379
   102
    ///
alpar@379
   103
    ///\param g The underlying graph
alpar@379
   104
    ///\param max_level Set the range of the possible labels to
alpar@379
   105
    ///[0...\c max_level]
alpar@379
   106
    Elevator(const Graph &g,int max_level) :
alpar@379
   107
      _g(g),
alpar@379
   108
      _max_level(max_level),
alpar@379
   109
      _item_num(_max_level),
alpar@379
   110
      _where(g),
alpar@379
   111
      _level(g,0),
alpar@379
   112
      _items(_max_level),
alpar@379
   113
      _first(_max_level+2),
alpar@379
   114
      _last_active(_max_level+2),
alpar@379
   115
      _highest_active(-1) {}
alpar@379
   116
    ///Constructor.
alpar@379
   117
alpar@379
   118
    ///Constructor.
alpar@379
   119
    ///
alpar@379
   120
    ///\param g The underlying graph
alpar@379
   121
    ///The range of the possible labels is [0...\c max_level],
alpar@379
   122
    ///where \c max_level is equal to the number of labeled items in the graph.
alpar@379
   123
    Elevator(const Graph &g) :
alpar@379
   124
      _g(g),
alpar@379
   125
      _max_level(countItems<Graph, Item>(g)),
alpar@379
   126
      _item_num(_max_level),
alpar@379
   127
      _where(g),
alpar@379
   128
      _level(g,0),
alpar@379
   129
      _items(_max_level),
alpar@379
   130
      _first(_max_level+2),
alpar@379
   131
      _last_active(_max_level+2),
alpar@379
   132
      _highest_active(-1)
alpar@379
   133
    {
alpar@379
   134
    }
alpar@379
   135
alpar@379
   136
    ///Activate item \c i.
alpar@379
   137
alpar@379
   138
    ///Activate item \c i.
alpar@379
   139
    ///\pre Item \c i shouldn't be active before.
alpar@379
   140
    void activate(Item i)
alpar@379
   141
    {
alpar@379
   142
      const int l=_level[i];
alpar@379
   143
      swap(_where[i],++_last_active[l]);
alpar@379
   144
      if(l>_highest_active) _highest_active=l;
alpar@379
   145
    }
alpar@379
   146
alpar@379
   147
    ///Deactivate item \c i.
alpar@379
   148
alpar@379
   149
    ///Deactivate item \c i.
alpar@379
   150
    ///\pre Item \c i must be active before.
alpar@379
   151
    void deactivate(Item i)
alpar@379
   152
    {
alpar@379
   153
      swap(_where[i],_last_active[_level[i]]--);
alpar@379
   154
      while(_highest_active>=0 &&
alpar@379
   155
            _last_active[_highest_active]<_first[_highest_active])
alpar@379
   156
        _highest_active--;
alpar@379
   157
    }
alpar@379
   158
alpar@379
   159
    ///Query whether item \c i is active
alpar@379
   160
    bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; }
alpar@379
   161
alpar@379
   162
    ///Return the level of item \c i.
alpar@379
   163
    int operator[](Item i) const { return _level[i]; }
alpar@379
   164
alpar@379
   165
    ///Return the number of items on level \c l.
alpar@379
   166
    int onLevel(int l) const
alpar@379
   167
    {
alpar@379
   168
      return _first[l+1]-_first[l];
alpar@379
   169
    }
alpar@379
   170
    ///Return true if the level is empty.
alpar@379
   171
    bool emptyLevel(int l) const
alpar@379
   172
    {
alpar@379
   173
      return _first[l+1]-_first[l]==0;
alpar@379
   174
    }
alpar@379
   175
    ///Return the number of items above level \c l.
alpar@379
   176
    int aboveLevel(int l) const
alpar@379
   177
    {
alpar@379
   178
      return _first[_max_level+1]-_first[l+1];
alpar@379
   179
    }
alpar@379
   180
    ///Return the number of active items on level \c l.
alpar@379
   181
    int activesOnLevel(int l) const
alpar@379
   182
    {
alpar@379
   183
      return _last_active[l]-_first[l]+1;
alpar@379
   184
    }
alpar@379
   185
    ///Return true if there is not active item on level \c l.
alpar@379
   186
    bool activeFree(int l) const
alpar@379
   187
    {
alpar@379
   188
      return _last_active[l]<_first[l];
alpar@379
   189
    }
alpar@379
   190
    ///Return the maximum allowed level.
alpar@379
   191
    int maxLevel() const
alpar@379
   192
    {
alpar@379
   193
      return _max_level;
alpar@379
   194
    }
alpar@379
   195
alpar@379
   196
    ///\name Highest Active Item
alpar@379
   197
    ///Functions for working with the highest level
alpar@379
   198
    ///active item.
alpar@379
   199
alpar@379
   200
    ///@{
alpar@379
   201
alpar@379
   202
    ///Return a highest level active item.
alpar@379
   203
alpar@379
   204
    ///Return a highest level active item.
alpar@379
   205
    ///
alpar@379
   206
    ///\return the highest level active item or INVALID if there is no active
alpar@379
   207
    ///item.
alpar@379
   208
    Item highestActive() const
alpar@379
   209
    {
alpar@379
   210
      return _highest_active>=0?*_last_active[_highest_active]:INVALID;
alpar@379
   211
    }
alpar@379
   212
alpar@379
   213
    ///Return a highest active level.
alpar@379
   214
alpar@379
   215
    ///Return a highest active level.
alpar@379
   216
    ///
alpar@379
   217
    ///\return the level of the highest active item or -1 if there is no active
alpar@379
   218
    ///item.
alpar@379
   219
    int highestActiveLevel() const
alpar@379
   220
    {
alpar@379
   221
      return _highest_active;
alpar@379
   222
    }
alpar@379
   223
alpar@379
   224
    ///Lift the highest active item by one.
alpar@379
   225
alpar@379
   226
    ///Lift the item returned by highestActive() by one.
alpar@379
   227
    ///
alpar@379
   228
    void liftHighestActive()
alpar@379
   229
    {
alpar@379
   230
      ++_level[*_last_active[_highest_active]];
alpar@379
   231
      swap(_last_active[_highest_active]--,_last_active[_highest_active+1]);
alpar@379
   232
      --_first[++_highest_active];
alpar@379
   233
    }
alpar@379
   234
alpar@379
   235
    ///Lift the highest active item.
alpar@379
   236
alpar@379
   237
    ///Lift the item returned by highestActive() to level \c new_level.
alpar@379
   238
    ///
alpar@379
   239
    ///\warning \c new_level must be strictly higher
alpar@379
   240
    ///than the current level.
alpar@379
   241
    ///
alpar@379
   242
    void liftHighestActive(int new_level)
alpar@379
   243
    {
alpar@379
   244
      const Item li = *_last_active[_highest_active];
alpar@379
   245
alpar@379
   246
      copy(--_first[_highest_active+1],_last_active[_highest_active]--);
alpar@379
   247
      for(int l=_highest_active+1;l<new_level;l++)
alpar@379
   248
        {
alpar@379
   249
          copy(--_first[l+1],_first[l]);
alpar@379
   250
          --_last_active[l];
alpar@379
   251
        }
alpar@379
   252
      copy(li,_first[new_level]);
alpar@379
   253
      _level[li]=new_level;
alpar@379
   254
      _highest_active=new_level;
alpar@379
   255
    }
alpar@379
   256
alpar@379
   257
    ///Lift the highest active item.
alpar@379
   258
alpar@379
   259
    ///Lift the item returned by highestActive() to the top level and
alpar@379
   260
    ///deactivates it.
alpar@379
   261
    ///
alpar@379
   262
    ///\warning \c new_level must be strictly higher
alpar@379
   263
    ///than the current level.
alpar@379
   264
    ///
alpar@379
   265
    void liftHighestActiveToTop()
alpar@379
   266
    {
alpar@379
   267
      const Item li = *_last_active[_highest_active];
alpar@379
   268
alpar@379
   269
      copy(--_first[_highest_active+1],_last_active[_highest_active]--);
alpar@379
   270
      for(int l=_highest_active+1;l<_max_level;l++)
alpar@379
   271
        {
alpar@379
   272
          copy(--_first[l+1],_first[l]);
alpar@379
   273
          --_last_active[l];
alpar@379
   274
        }
alpar@379
   275
      copy(li,_first[_max_level]);
alpar@379
   276
      --_last_active[_max_level];
alpar@379
   277
      _level[li]=_max_level;
alpar@379
   278
alpar@379
   279
      while(_highest_active>=0 &&
alpar@379
   280
            _last_active[_highest_active]<_first[_highest_active])
alpar@379
   281
        _highest_active--;
alpar@379
   282
    }
alpar@379
   283
alpar@379
   284
    ///@}
alpar@379
   285
alpar@379
   286
    ///\name Active Item on Certain Level
alpar@379
   287
    ///Functions for working with the active items.
alpar@379
   288
alpar@379
   289
    ///@{
alpar@379
   290
alpar@379
   291
    ///Returns an active item on level \c l.
alpar@379
   292
alpar@379
   293
    ///Returns an active item on level \c l.
alpar@379
   294
    ///
alpar@379
   295
    ///Returns an active item on level \c l or \ref INVALID if there is no such
alpar@379
   296
    ///an item. (\c l must be from the range [0...\c max_level].
alpar@379
   297
    Item activeOn(int l) const
alpar@379
   298
    {
alpar@379
   299
      return _last_active[l]>=_first[l]?*_last_active[l]:INVALID;
alpar@379
   300
    }
alpar@379
   301
alpar@379
   302
    ///Lifts the active item returned by \c activeOn() member function.
alpar@379
   303
alpar@379
   304
    ///Lifts the active item returned by \c activeOn() member function
alpar@379
   305
    ///by one.
alpar@379
   306
    Item liftActiveOn(int level)
alpar@379
   307
    {
alpar@379
   308
      ++_level[*_last_active[level]];
alpar@379
   309
      swap(_last_active[level]--, --_first[level+1]);
alpar@379
   310
      if (level+1>_highest_active) ++_highest_active;
alpar@379
   311
    }
alpar@379
   312
alpar@379
   313
    ///Lifts the active item returned by \c activeOn() member function.
alpar@379
   314
alpar@379
   315
    ///Lifts the active item returned by \c activeOn() member function
alpar@379
   316
    ///to the given level.
alpar@379
   317
    void liftActiveOn(int level, int new_level)
alpar@379
   318
    {
alpar@379
   319
      const Item ai = *_last_active[level];
alpar@379
   320
alpar@379
   321
      copy(--_first[level+1], _last_active[level]--);
alpar@379
   322
      for(int l=level+1;l<new_level;l++)
alpar@379
   323
        {
alpar@379
   324
          copy(_last_active[l],_first[l]);
alpar@379
   325
          copy(--_first[l+1], _last_active[l]--);
alpar@379
   326
        }
alpar@379
   327
      copy(ai,_first[new_level]);
alpar@379
   328
      _level[ai]=new_level;
alpar@379
   329
      if (new_level>_highest_active) _highest_active=new_level;
alpar@379
   330
    }
alpar@379
   331
alpar@379
   332
    ///Lifts the active item returned by \c activeOn() member function.
alpar@379
   333
alpar@379
   334
    ///Lifts the active item returned by \c activeOn() member function
alpar@379
   335
    ///to the top level.
alpar@379
   336
    void liftActiveToTop(int level)
alpar@379
   337
    {
alpar@379
   338
      const Item ai = *_last_active[level];
alpar@379
   339
alpar@379
   340
      copy(--_first[level+1],_last_active[level]--);
alpar@379
   341
      for(int l=level+1;l<_max_level;l++)
alpar@379
   342
        {
alpar@379
   343
          copy(_last_active[l],_first[l]);
alpar@379
   344
          copy(--_first[l+1], _last_active[l]--);
alpar@379
   345
        }
alpar@379
   346
      copy(ai,_first[_max_level]);
alpar@379
   347
      --_last_active[_max_level];
alpar@379
   348
      _level[ai]=_max_level;
alpar@379
   349
alpar@379
   350
      if (_highest_active==level) {
alpar@379
   351
        while(_highest_active>=0 &&
alpar@379
   352
              _last_active[_highest_active]<_first[_highest_active])
alpar@379
   353
          _highest_active--;
alpar@379
   354
      }
alpar@379
   355
    }
alpar@379
   356
alpar@379
   357
    ///@}
alpar@379
   358
alpar@379
   359
    ///Lift an active item to a higher level.
alpar@379
   360
alpar@379
   361
    ///Lift an active item to a higher level.
alpar@379
   362
    ///\param i The item to be lifted. It must be active.
alpar@379
   363
    ///\param new_level The new level of \c i. It must be strictly higher
alpar@379
   364
    ///than the current level.
alpar@379
   365
    ///
alpar@379
   366
    void lift(Item i, int new_level)
alpar@379
   367
    {
alpar@379
   368
      const int lo = _level[i];
alpar@379
   369
      const Vit w = _where[i];
alpar@379
   370
alpar@379
   371
      copy(_last_active[lo],w);
alpar@379
   372
      copy(--_first[lo+1],_last_active[lo]--);
alpar@379
   373
      for(int l=lo+1;l<new_level;l++)
alpar@379
   374
        {
alpar@379
   375
          copy(_last_active[l],_first[l]);
alpar@379
   376
          copy(--_first[l+1],_last_active[l]--);
alpar@379
   377
        }
alpar@379
   378
      copy(i,_first[new_level]);
alpar@379
   379
      _level[i]=new_level;
alpar@379
   380
      if(new_level>_highest_active) _highest_active=new_level;
alpar@379
   381
    }
alpar@379
   382
alpar@379
   383
    ///Mark the node as it did not reach the max level
alpar@379
   384
alpar@379
   385
    ///Mark the node as it did not reach the max level. It sets the
alpar@379
   386
    ///level to the under the max level value. The node will be never
alpar@379
   387
    ///more activated because the push operation from the maximum
alpar@379
   388
    ///level is forbidden in the push-relabel algorithms. The node
alpar@379
   389
    ///should be lifted previously to the top level.
alpar@379
   390
    void markToBottom(Item i) {
alpar@379
   391
      _level[i] = _max_level - 1;
alpar@379
   392
    }
alpar@379
   393
alpar@379
   394
    ///Lift all nodes on and above a level to the top (and deactivate them).
alpar@379
   395
alpar@379
   396
    ///This function lifts all nodes on and above level \c l to \c
alpar@379
   397
    ///maxLevel(), and also deactivates them.
alpar@379
   398
    void liftToTop(int l)
alpar@379
   399
    {
alpar@379
   400
      const Vit f=_first[l];
alpar@379
   401
      const Vit tl=_first[_max_level];
alpar@379
   402
      for(Vit i=f;i!=tl;++i)
alpar@379
   403
        _level[*i]=_max_level;
alpar@379
   404
      for(int i=l;i<=_max_level;i++)
alpar@379
   405
        {
alpar@379
   406
          _first[i]=f;
alpar@379
   407
          _last_active[i]=f-1;
alpar@379
   408
        }
alpar@379
   409
      for(_highest_active=l-1;
alpar@379
   410
          _highest_active>=0 &&
alpar@379
   411
            _last_active[_highest_active]<_first[_highest_active];
alpar@379
   412
          _highest_active--) ;
alpar@379
   413
    }
alpar@379
   414
alpar@379
   415
  private:
alpar@379
   416
    int _init_lev;
alpar@379
   417
    Vit _init_num;
alpar@379
   418
alpar@379
   419
  public:
alpar@379
   420
alpar@379
   421
    ///\name Initialization
alpar@379
   422
    ///Using this function you can initialize the levels of the item.
alpar@379
   423
    ///\n
alpar@379
   424
    ///This initializatios is started with calling \c initStart().
alpar@379
   425
    ///Then the
alpar@379
   426
    ///items should be listed levels by levels statring with the lowest one
alpar@379
   427
    ///(with level 0). This is done by using \c initAddItem()
alpar@379
   428
    ///and \c initNewLevel(). Finally \c initFinish() must be called.
alpar@379
   429
    ///The items not listed will be put on the highest level.
alpar@379
   430
    ///@{
alpar@379
   431
alpar@379
   432
    ///Start the initialization process.
alpar@379
   433
alpar@379
   434
    void initStart()
alpar@379
   435
    {
alpar@379
   436
      _init_lev=0;
alpar@379
   437
      _init_num=_items.begin();
alpar@379
   438
      _first[0]=_items.begin();
alpar@379
   439
      _last_active[0]=_items.begin()-1;
alpar@379
   440
      Vit n=_items.begin();
alpar@379
   441
      for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i)
alpar@379
   442
        {
alpar@379
   443
          *n=i;
alpar@379
   444
          _where[i]=n;
alpar@379
   445
          _level[i]=_max_level;
alpar@379
   446
          ++n;
alpar@379
   447
        }
alpar@379
   448
    }
alpar@379
   449
alpar@379
   450
    ///Add an item to the current level.
alpar@379
   451
alpar@379
   452
    void initAddItem(Item i)
alpar@379
   453
    {
alpar@379
   454
     swap(_where[i],_init_num);
alpar@379
   455
      _level[i]=_init_lev;
alpar@379
   456
      ++_init_num;
alpar@379
   457
    }
alpar@379
   458
alpar@379
   459
    ///Start a new level.
alpar@379
   460
alpar@379
   461
    ///Start a new level.
alpar@379
   462
    ///It shouldn't be used before the items on level 0 are listed.
alpar@379
   463
    void initNewLevel()
alpar@379
   464
    {
alpar@379
   465
      _init_lev++;
alpar@379
   466
      _first[_init_lev]=_init_num;
alpar@379
   467
      _last_active[_init_lev]=_init_num-1;
alpar@379
   468
    }
alpar@379
   469
alpar@379
   470
    ///Finalize the initialization process.
alpar@379
   471
alpar@379
   472
    void initFinish()
alpar@379
   473
    {
alpar@379
   474
      for(_init_lev++;_init_lev<=_max_level;_init_lev++)
alpar@379
   475
        {
alpar@379
   476
          _first[_init_lev]=_init_num;
alpar@379
   477
          _last_active[_init_lev]=_init_num-1;
alpar@379
   478
        }
alpar@379
   479
      _first[_max_level+1]=_items.begin()+_item_num;
alpar@379
   480
      _last_active[_max_level+1]=_items.begin()+_item_num-1;
alpar@379
   481
      _highest_active = -1;
alpar@379
   482
    }
alpar@379
   483
alpar@379
   484
    ///@}
alpar@379
   485
alpar@379
   486
  };
alpar@379
   487
alpar@379
   488
  ///Class for handling "labels" in push-relabel type algorithms.
alpar@379
   489
alpar@379
   490
  ///A class for handling "labels" in push-relabel type algorithms.
alpar@379
   491
  ///
alpar@379
   492
  ///\ingroup auxdat
alpar@379
   493
  ///Using this class you can assign "labels" (nonnegative integer numbers)
alpar@379
   494
  ///to the edges or nodes of a graph, manipulate and query them through
alpar@379
   495
  ///operations typically arising in "push-relabel" type algorithms.
alpar@379
   496
  ///
alpar@379
   497
  ///Each item is either \em active or not, and you can also choose a
alpar@379
   498
  ///highest level active item.
alpar@379
   499
  ///
alpar@379
   500
  ///\sa Elevator
alpar@379
   501
  ///
alpar@379
   502
  ///\param Graph the underlying graph type
alpar@379
   503
  ///\param Item Type of the items the data is assigned to (Graph::Node,
alpar@379
   504
  ///Graph::Edge, Graph::UEdge)
alpar@379
   505
  template <class Graph, class Item>
alpar@379
   506
  class LinkedElevator {
alpar@379
   507
  public:
alpar@379
   508
alpar@379
   509
    typedef Item Key;
alpar@379
   510
    typedef int Value;
alpar@379
   511
alpar@379
   512
  private:
alpar@379
   513
alpar@379
   514
    typedef typename ItemSetTraits<Graph,Item>::
alpar@379
   515
    template Map<Item>::Type ItemMap;
alpar@379
   516
    typedef typename ItemSetTraits<Graph,Item>::
alpar@379
   517
    template Map<int>::Type IntMap;
alpar@379
   518
    typedef typename ItemSetTraits<Graph,Item>::
alpar@379
   519
    template Map<bool>::Type BoolMap;
alpar@379
   520
alpar@379
   521
    const Graph &_graph;
alpar@379
   522
    int _max_level;
alpar@379
   523
    int _item_num;
alpar@379
   524
    std::vector<Item> _first, _last;
alpar@379
   525
    ItemMap _prev, _next;
alpar@379
   526
    int _highest_active;
alpar@379
   527
    IntMap _level;
alpar@379
   528
    BoolMap _active;
alpar@379
   529
alpar@379
   530
  public:
alpar@379
   531
    ///Constructor with given maximum level.
alpar@379
   532
alpar@379
   533
    ///Constructor with given maximum level.
alpar@379
   534
    ///
alpar@379
   535
    ///\param g The underlying graph
alpar@379
   536
    ///\param max_level Set the range of the possible labels to
alpar@379
   537
    ///[0...\c max_level]
alpar@379
   538
    LinkedElevator(const Graph& graph, int max_level)
alpar@379
   539
      : _graph(graph), _max_level(max_level), _item_num(_max_level),
alpar@379
   540
        _first(_max_level + 1), _last(_max_level + 1),
alpar@379
   541
        _prev(graph), _next(graph),
alpar@379
   542
        _highest_active(-1), _level(graph), _active(graph) {}
alpar@379
   543
alpar@379
   544
    ///Constructor.
alpar@379
   545
alpar@379
   546
    ///Constructor.
alpar@379
   547
    ///
alpar@379
   548
    ///\param g The underlying graph
alpar@379
   549
    ///The range of the possible labels is [0...\c max_level],
alpar@379
   550
    ///where \c max_level is equal to the number of labeled items in the graph.
alpar@379
   551
    LinkedElevator(const Graph& graph)
alpar@379
   552
      : _graph(graph), _max_level(countItems<Graph, Item>(graph)),
alpar@379
   553
        _item_num(_max_level),
alpar@379
   554
        _first(_max_level + 1), _last(_max_level + 1),
alpar@379
   555
        _prev(graph, INVALID), _next(graph, INVALID),
alpar@379
   556
        _highest_active(-1), _level(graph), _active(graph) {}
alpar@379
   557
alpar@379
   558
alpar@379
   559
    ///Activate item \c i.
alpar@379
   560
alpar@379
   561
    ///Activate item \c i.
alpar@379
   562
    ///\pre Item \c i shouldn't be active before.
alpar@379
   563
    void activate(Item i) {
alpar@379
   564
      _active.set(i, true);
alpar@379
   565
alpar@379
   566
      int level = _level[i];
alpar@379
   567
      if (level > _highest_active) {
alpar@379
   568
        _highest_active = level;
alpar@379
   569
      }
alpar@379
   570
alpar@379
   571
      if (_prev[i] == INVALID || _active[_prev[i]]) return;
alpar@379
   572
      //unlace
alpar@379
   573
      _next.set(_prev[i], _next[i]);
alpar@379
   574
      if (_next[i] != INVALID) {
alpar@379
   575
        _prev.set(_next[i], _prev[i]);
alpar@379
   576
      } else {
alpar@379
   577
        _last[level] = _prev[i];
alpar@379
   578
      }
alpar@379
   579
      //lace
alpar@379
   580
      _next.set(i, _first[level]);
alpar@379
   581
      _prev.set(_first[level], i);
alpar@379
   582
      _prev.set(i, INVALID);
alpar@379
   583
      _first[level] = i;
alpar@379
   584
alpar@379
   585
    }
alpar@379
   586
alpar@379
   587
    ///Deactivate item \c i.
alpar@379
   588
alpar@379
   589
    ///Deactivate item \c i.
alpar@379
   590
    ///\pre Item \c i must be active before.
alpar@379
   591
    void deactivate(Item i) {
alpar@379
   592
      _active.set(i, false);
alpar@379
   593
      int level = _level[i];
alpar@379
   594
alpar@379
   595
      if (_next[i] == INVALID || !_active[_next[i]])
alpar@379
   596
        goto find_highest_level;
alpar@379
   597
alpar@379
   598
      //unlace
alpar@379
   599
      _prev.set(_next[i], _prev[i]);
alpar@379
   600
      if (_prev[i] != INVALID) {
alpar@379
   601
        _next.set(_prev[i], _next[i]);
alpar@379
   602
      } else {
alpar@379
   603
        _first[_level[i]] = _next[i];
alpar@379
   604
      }
alpar@379
   605
      //lace
alpar@379
   606
      _prev.set(i, _last[level]);
alpar@379
   607
      _next.set(_last[level], i);
alpar@379
   608
      _next.set(i, INVALID);
alpar@379
   609
      _last[level] = i;
alpar@379
   610
alpar@379
   611
    find_highest_level:
alpar@379
   612
      if (level == _highest_active) {
alpar@379
   613
        while (_highest_active >= 0 && activeFree(_highest_active))
alpar@379
   614
          --_highest_active;
alpar@379
   615
      }
alpar@379
   616
    }
alpar@379
   617
alpar@379
   618
    ///Query whether item \c i is active
alpar@379
   619
    bool active(Item i) const { return _active[i]; }
alpar@379
   620
alpar@379
   621
    ///Return the level of item \c i.
alpar@379
   622
    int operator[](Item i) const { return _level[i]; }
alpar@379
   623
alpar@379
   624
    ///Return the number of items on level \c l.
alpar@379
   625
    int onLevel(int l) const {
alpar@379
   626
      int num = 0;
alpar@379
   627
      Item n = _first[l];
alpar@379
   628
      while (n != INVALID) {
alpar@379
   629
        ++num;
alpar@379
   630
        n = _next[n];
alpar@379
   631
      }
alpar@379
   632
      return num;
alpar@379
   633
    }
alpar@379
   634
alpar@379
   635
    ///Return true if the level is empty.
alpar@379
   636
    bool emptyLevel(int l) const {
alpar@379
   637
      return _first[l] == INVALID;
alpar@379
   638
    }
alpar@379
   639
alpar@379
   640
    ///Return the number of items above level \c l.
alpar@379
   641
    int aboveLevel(int l) const {
alpar@379
   642
      int num = 0;
alpar@379
   643
      for (int level = l + 1; level < _max_level; ++level)
alpar@379
   644
        num += onLevel(level);
alpar@379
   645
      return num;
alpar@379
   646
    }
alpar@379
   647
alpar@379
   648
    ///Return the number of active items on level \c l.
alpar@379
   649
    int activesOnLevel(int l) const {
alpar@379
   650
      int num = 0;
alpar@379
   651
      Item n = _first[l];
alpar@379
   652
      while (n != INVALID && _active[n]) {
alpar@379
   653
        ++num;
alpar@379
   654
        n = _next[n];
alpar@379
   655
      }
alpar@379
   656
      return num;
alpar@379
   657
    }
alpar@379
   658
alpar@379
   659
    ///Return true if there is not active item on level \c l.
alpar@379
   660
    bool activeFree(int l) const {
alpar@379
   661
      return _first[l] == INVALID || !_active[_first[l]];
alpar@379
   662
    }
alpar@379
   663
alpar@379
   664
    ///Return the maximum allowed level.
alpar@379
   665
    int maxLevel() const {
alpar@379
   666
      return _max_level;
alpar@379
   667
    }
alpar@379
   668
alpar@379
   669
    ///\name Highest Active Item
alpar@379
   670
    ///Functions for working with the highest level
alpar@379
   671
    ///active item.
alpar@379
   672
alpar@379
   673
    ///@{
alpar@379
   674
alpar@379
   675
    ///Return a highest level active item.
alpar@379
   676
alpar@379
   677
    ///Return a highest level active item.
alpar@379
   678
    ///
alpar@379
   679
    ///\return the highest level active item or INVALID if there is no
alpar@379
   680
    ///active item.
alpar@379
   681
    Item highestActive() const {
alpar@379
   682
      return _highest_active >= 0 ? _first[_highest_active] : INVALID;
alpar@379
   683
    }
alpar@379
   684
alpar@379
   685
    ///Return a highest active level.
alpar@379
   686
alpar@379
   687
    ///Return a highest active level.
alpar@379
   688
    ///
alpar@379
   689
    ///\return the level of the highest active item or -1 if there is
alpar@379
   690
    ///no active item.
alpar@379
   691
    int highestActiveLevel() const {
alpar@379
   692
      return _highest_active;
alpar@379
   693
    }
alpar@379
   694
alpar@379
   695
    ///Lift the highest active item by one.
alpar@379
   696
alpar@379
   697
    ///Lift the item returned by highestActive() by one.
alpar@379
   698
    ///
alpar@379
   699
    void liftHighestActive() {
alpar@379
   700
      Item i = _first[_highest_active];
alpar@379
   701
      if (_next[i] != INVALID) {
alpar@379
   702
        _prev.set(_next[i], INVALID);
alpar@379
   703
        _first[_highest_active] = _next[i];
alpar@379
   704
      } else {
alpar@379
   705
        _first[_highest_active] = INVALID;
alpar@379
   706
        _last[_highest_active] = INVALID;
alpar@379
   707
      }
alpar@379
   708
      _level.set(i, ++_highest_active);
alpar@379
   709
      if (_first[_highest_active] == INVALID) {
alpar@379
   710
        _first[_highest_active] = i;
alpar@379
   711
        _last[_highest_active] = i;
alpar@379
   712
        _prev.set(i, INVALID);
alpar@379
   713
        _next.set(i, INVALID);
alpar@379
   714
      } else {
alpar@379
   715
        _prev.set(_first[_highest_active], i);
alpar@379
   716
        _next.set(i, _first[_highest_active]);
alpar@379
   717
        _first[_highest_active] = i;
alpar@379
   718
      }
alpar@379
   719
    }
alpar@379
   720
alpar@379
   721
    ///Lift the highest active item.
alpar@379
   722
alpar@379
   723
    ///Lift the item returned by highestActive() to level \c new_level.
alpar@379
   724
    ///
alpar@379
   725
    ///\warning \c new_level must be strictly higher
alpar@379
   726
    ///than the current level.
alpar@379
   727
    ///
alpar@379
   728
    void liftHighestActive(int new_level) {
alpar@379
   729
      Item i = _first[_highest_active];
alpar@379
   730
      if (_next[i] != INVALID) {
alpar@379
   731
        _prev.set(_next[i], INVALID);
alpar@379
   732
        _first[_highest_active] = _next[i];
alpar@379
   733
      } else {
alpar@379
   734
        _first[_highest_active] = INVALID;
alpar@379
   735
        _last[_highest_active] = INVALID;
alpar@379
   736
      }
alpar@379
   737
      _level.set(i, _highest_active = new_level);
alpar@379
   738
      if (_first[_highest_active] == INVALID) {
alpar@379
   739
        _first[_highest_active] = _last[_highest_active] = i;
alpar@379
   740
        _prev.set(i, INVALID);
alpar@379
   741
        _next.set(i, INVALID);
alpar@379
   742
      } else {
alpar@379
   743
        _prev.set(_first[_highest_active], i);
alpar@379
   744
        _next.set(i, _first[_highest_active]);
alpar@379
   745
        _first[_highest_active] = i;
alpar@379
   746
      }
alpar@379
   747
    }
alpar@379
   748
alpar@379
   749
    ///Lift the highest active to top.
alpar@379
   750
alpar@379
   751
    ///Lift the item returned by highestActive() to the top level and
alpar@379
   752
    ///deactivates the node.
alpar@379
   753
    ///
alpar@379
   754
    void liftHighestActiveToTop() {
alpar@379
   755
      Item i = _first[_highest_active];
alpar@379
   756
      _level.set(i, _max_level);
alpar@379
   757
      if (_next[i] != INVALID) {
alpar@379
   758
        _prev.set(_next[i], INVALID);
alpar@379
   759
        _first[_highest_active] = _next[i];
alpar@379
   760
      } else {
alpar@379
   761
        _first[_highest_active] = INVALID;
alpar@379
   762
        _last[_highest_active] = INVALID;
alpar@379
   763
      }
alpar@379
   764
      while (_highest_active >= 0 && activeFree(_highest_active))
alpar@379
   765
        --_highest_active;
alpar@379
   766
    }
alpar@379
   767
alpar@379
   768
    ///@}
alpar@379
   769
alpar@379
   770
    ///\name Active Item on Certain Level
alpar@379
   771
    ///Functions for working with the active items.
alpar@379
   772
alpar@379
   773
    ///@{
alpar@379
   774
alpar@379
   775
    ///Returns an active item on level \c l.
alpar@379
   776
alpar@379
   777
    ///Returns an active item on level \c l.
alpar@379
   778
    ///
alpar@379
   779
    ///Returns an active item on level \c l or \ref INVALID if there is no such
alpar@379
   780
    ///an item. (\c l must be from the range [0...\c max_level].
alpar@379
   781
    Item activeOn(int l) const
alpar@379
   782
    {
alpar@379
   783
      return _active[_first[l]] ? _first[l] : INVALID;
alpar@379
   784
    }
alpar@379
   785
alpar@379
   786
    ///Lifts the active item returned by \c activeOn() member function.
alpar@379
   787
alpar@379
   788
    ///Lifts the active item returned by \c activeOn() member function
alpar@379
   789
    ///by one.
alpar@379
   790
    Item liftActiveOn(int l)
alpar@379
   791
    {
alpar@379
   792
      Item i = _first[l];
alpar@379
   793
      if (_next[i] != INVALID) {
alpar@379
   794
        _prev.set(_next[i], INVALID);
alpar@379
   795
        _first[l] = _next[i];
alpar@379
   796
      } else {
alpar@379
   797
        _first[l] = INVALID;
alpar@379
   798
        _last[l] = INVALID;
alpar@379
   799
      }
alpar@379
   800
      _level.set(i, ++l);
alpar@379
   801
      if (_first[l] == INVALID) {
alpar@379
   802
        _first[l] = _last[l] = i;
alpar@379
   803
        _prev.set(i, INVALID);
alpar@379
   804
        _next.set(i, INVALID);
alpar@379
   805
      } else {
alpar@379
   806
        _prev.set(_first[l], i);
alpar@379
   807
        _next.set(i, _first[l]);
alpar@379
   808
        _first[l] = i;
alpar@379
   809
      }
alpar@379
   810
      if (_highest_active < l) {
alpar@379
   811
        _highest_active = l;
alpar@379
   812
      }
alpar@379
   813
    }
alpar@379
   814
alpar@379
   815
    /// \brief Lifts the active item returned by \c activeOn() member function.
alpar@379
   816
    ///
alpar@379
   817
    /// Lifts the active item returned by \c activeOn() member function
alpar@379
   818
    /// to the given level.
alpar@379
   819
    void liftActiveOn(int l, int new_level)
alpar@379
   820
    {
alpar@379
   821
      Item i = _first[l];
alpar@379
   822
      if (_next[i] != INVALID) {
alpar@379
   823
        _prev.set(_next[i], INVALID);
alpar@379
   824
        _first[l] = _next[i];
alpar@379
   825
      } else {
alpar@379
   826
        _first[l] = INVALID;
alpar@379
   827
        _last[l] = INVALID;
alpar@379
   828
      }
alpar@379
   829
      _level.set(i, l = new_level);
alpar@379
   830
      if (_first[l] == INVALID) {
alpar@379
   831
        _first[l] = _last[l] = i;
alpar@379
   832
        _prev.set(i, INVALID);
alpar@379
   833
        _next.set(i, INVALID);
alpar@379
   834
      } else {
alpar@379
   835
        _prev.set(_first[l], i);
alpar@379
   836
        _next.set(i, _first[l]);
alpar@379
   837
        _first[l] = i;
alpar@379
   838
      }
alpar@379
   839
      if (_highest_active < l) {
alpar@379
   840
        _highest_active = l;
alpar@379
   841
      }
alpar@379
   842
    }
alpar@379
   843
alpar@379
   844
    ///Lifts the active item returned by \c activeOn() member function.
alpar@379
   845
alpar@379
   846
    ///Lifts the active item returned by \c activeOn() member function
alpar@379
   847
    ///to the top level.
alpar@379
   848
    void liftActiveToTop(int l)
alpar@379
   849
    {
alpar@379
   850
      Item i = _first[l];
alpar@379
   851
      if (_next[i] != INVALID) {
alpar@379
   852
        _prev.set(_next[i], INVALID);
alpar@379
   853
        _first[l] = _next[i];
alpar@379
   854
      } else {
alpar@379
   855
        _first[l] = INVALID;
alpar@379
   856
        _last[l] = INVALID;
alpar@379
   857
      }
alpar@379
   858
      _level.set(i, _max_level);
alpar@379
   859
      if (l == _highest_active) {
alpar@379
   860
        while (_highest_active >= 0 && activeFree(_highest_active))
alpar@379
   861
          --_highest_active;
alpar@379
   862
      }
alpar@379
   863
    }
alpar@379
   864
alpar@379
   865
    ///@}
alpar@379
   866
alpar@379
   867
    /// \brief Lift an active item to a higher level.
alpar@379
   868
    ///
alpar@379
   869
    /// Lift an active item to a higher level.
alpar@379
   870
    /// \param i The item to be lifted. It must be active.
alpar@379
   871
    /// \param new_level The new level of \c i. It must be strictly higher
alpar@379
   872
    /// than the current level.
alpar@379
   873
    ///
alpar@379
   874
    void lift(Item i, int new_level) {
alpar@379
   875
      if (_next[i] != INVALID) {
alpar@379
   876
        _prev.set(_next[i], _prev[i]);
alpar@379
   877
      } else {
alpar@379
   878
        _last[new_level] = _prev[i];
alpar@379
   879
      }
alpar@379
   880
      if (_prev[i] != INVALID) {
alpar@379
   881
        _next.set(_prev[i], _next[i]);
alpar@379
   882
      } else {
alpar@379
   883
        _first[new_level] = _next[i];
alpar@379
   884
      }
alpar@379
   885
      _level.set(i, new_level);
alpar@379
   886
      if (_first[new_level] == INVALID) {
alpar@379
   887
        _first[new_level] = _last[new_level] = i;
alpar@379
   888
        _prev.set(i, INVALID);
alpar@379
   889
        _next.set(i, INVALID);
alpar@379
   890
      } else {
alpar@379
   891
        _prev.set(_first[new_level], i);
alpar@379
   892
        _next.set(i, _first[new_level]);
alpar@379
   893
        _first[new_level] = i;
alpar@379
   894
      }
alpar@379
   895
      if (_highest_active < new_level) {
alpar@379
   896
        _highest_active = new_level;
alpar@379
   897
      }
alpar@379
   898
    }
alpar@379
   899
alpar@379
   900
    ///Mark the node as it did not reach the max level
alpar@379
   901
alpar@379
   902
    ///Mark the node as it did not reach the max level. It sets the
alpar@379
   903
    ///level to the under the max level value. The node will be never
alpar@379
   904
    ///more activated because the push operation from the maximum
alpar@379
   905
    ///level is forbidden in the push-relabel algorithms. The node
alpar@379
   906
    ///should be lifted previously to the top level.
alpar@379
   907
    void markToBottom(Item i) {
alpar@379
   908
      _level.set(i, _max_level - 1);
alpar@379
   909
    }
alpar@379
   910
alpar@379
   911
    ///Lift all nodes on and above a level to the top (and deactivate them).
alpar@379
   912
alpar@379
   913
    ///This function lifts all nodes on and above level \c l to \c
alpar@379
   914
    ///maxLevel(), and also deactivates them.
alpar@379
   915
    void liftToTop(int l)  {
alpar@379
   916
      for (int i = l + 1; _first[i] != INVALID; ++i) {
alpar@379
   917
        Item n = _first[i];
alpar@379
   918
        while (n != INVALID) {
alpar@379
   919
          _level.set(n, _max_level);
alpar@379
   920
          n = _next[n];
alpar@379
   921
        }
alpar@379
   922
        _first[i] = INVALID;
alpar@379
   923
        _last[i] = INVALID;
alpar@379
   924
      }
alpar@379
   925
      if (_highest_active > l - 1) {
alpar@379
   926
        _highest_active = l - 1;
alpar@379
   927
        while (_highest_active >= 0 && activeFree(_highest_active))
alpar@379
   928
          --_highest_active;
alpar@379
   929
      }
alpar@379
   930
    }
alpar@379
   931
alpar@379
   932
  private:
alpar@379
   933
alpar@379
   934
    int _init_level;
alpar@379
   935
alpar@379
   936
  public:
alpar@379
   937
alpar@379
   938
    ///\name Initialization
alpar@379
   939
    ///Using this function you can initialize the levels of the item.
alpar@379
   940
    ///\n
alpar@379
   941
    ///This initializatios is started with calling \c initStart().
alpar@379
   942
    ///Then the
alpar@379
   943
    ///items should be listed levels by levels statring with the lowest one
alpar@379
   944
    ///(with level 0). This is done by using \c initAddItem()
alpar@379
   945
    ///and \c initNewLevel(). Finally \c initFinish() must be called.
alpar@379
   946
    ///The items not listed will be put on the highest level.
alpar@379
   947
    ///@{
alpar@379
   948
alpar@379
   949
    ///Start the initialization process.
alpar@379
   950
alpar@379
   951
    void initStart() {
alpar@379
   952
alpar@379
   953
      for (int i = 0; i <= _max_level; ++i) {
alpar@379
   954
        _first[i] = _last[i] = INVALID;
alpar@379
   955
      }
alpar@379
   956
      _init_level = 0;
alpar@379
   957
      for(typename ItemSetTraits<Graph,Item>::ItemIt i(_graph);
alpar@379
   958
          i != INVALID; ++i) {
alpar@379
   959
        _level.set(i, _max_level);
alpar@379
   960
        _active.set(i, false);
alpar@379
   961
      }
alpar@379
   962
    }
alpar@379
   963
alpar@379
   964
    ///Add an item to the current level.
alpar@379
   965
alpar@379
   966
    void initAddItem(Item i) {
alpar@379
   967
      _level.set(i, _init_level);
alpar@379
   968
      if (_last[_init_level] == INVALID) {
alpar@379
   969
        _first[_init_level] = i;
alpar@379
   970
        _last[_init_level] = i;
alpar@379
   971
        _prev.set(i, INVALID);
alpar@379
   972
        _next.set(i, INVALID);
alpar@379
   973
      } else {
alpar@379
   974
        _prev.set(i, _last[_init_level]);
alpar@379
   975
        _next.set(i, INVALID);
alpar@379
   976
        _next.set(_last[_init_level], i);
alpar@379
   977
        _last[_init_level] = i;
alpar@379
   978
      }
alpar@379
   979
    }
alpar@379
   980
alpar@379
   981
    ///Start a new level.
alpar@379
   982
alpar@379
   983
    ///Start a new level.
alpar@379
   984
    ///It shouldn't be used before the items on level 0 are listed.
alpar@379
   985
    void initNewLevel() {
alpar@379
   986
      ++_init_level;
alpar@379
   987
    }
alpar@379
   988
alpar@379
   989
    ///Finalize the initialization process.
alpar@379
   990
alpar@379
   991
    void initFinish() {
alpar@379
   992
      _highest_active = -1;
alpar@379
   993
    }
alpar@379
   994
alpar@379
   995
    ///@}
alpar@379
   996
alpar@379
   997
  };
alpar@379
   998
alpar@379
   999
alpar@379
  1000
} //END OF NAMESPACE LEMON
alpar@379
  1001
alpar@379
  1002
#endif
alpar@379
  1003