3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_ELEVATOR_H
20 #define LEMON_ELEVATOR_H
24 ///\brief Elevator class
26 ///Elevator class implements an efficient data structure
27 ///for labeling items in push-relabel type algorithms.
30 #include <test/test_tools.h>
33 ///Class for handling "labels" in push-relabel type algorithms.
35 ///A class for handling "labels" in push-relabel type algorithms.
38 ///Using this class you can assign "labels" (nonnegative integer numbers)
39 ///to the edges or nodes of a graph, manipulate and query them through
40 ///operations typically arising in "push-relabel" type algorithms.
42 ///Each item is either \em active or not, and you can also choose a
43 ///highest level active item.
45 ///\param Graph the underlying graph type
46 ///\param Item Type of the items the data is assigned to (Graph::Node,
47 ///Graph::Edge, Graph::UEdge)
48 template<class Graph, class Item>
52 typedef typename std::vector<Item>::iterator Vit;
53 typedef DefaultMap<Graph,Item,Vit> VitMap;
54 typedef DefaultMap<Graph,Item,int> IntMap;
61 std::vector<Item> _items;
62 std::vector<Vit> _first;
63 std::vector<Vit> _last_active;
67 void copy(Item i, Vit p)
71 void copy(Vit s, Vit p)
80 void swap(Vit i, Vit j)
84 _where[ti]=_where[*i=*j];
91 for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i)
95 check(*w==i,"GEBASZ: CORRUPT DS");
96 check(_first[l]<=w,"GEBASZ: CORRUPT DS");
97 check(_first[l+1]>w,"GEBASZ: CORRUPT DS");
99 for(int l=0;l<=_max_level;++l)
101 check(_first[l]<=_last_active[l]+1,"GEBASZ: CORRUPT DS");
102 check(_last_active[l]<_first[l+1],"GEBASZ: CORRUPT DS");
103 check(_first[l]<=_first[l+1],"GEBASZ: CORRUPT DS");
105 check(_highest_active<0 ||
106 _first[_highest_active]<=_last_active[_highest_active],
107 "GEBASZ: CORRUPT DS");
111 ///Constructor with given maximum level.
113 ///Constructor with given maximum level.
115 ///\param g The underlying graph
116 ///\param max_level Set the range of the possible labels to
117 ///[0...\c max_level]
118 Elevator(const Graph &g,int max_level) :
120 _max_level(max_level),
121 _item_num(_max_level),
125 _first(_max_level+2),
126 _last_active(_max_level+2),
127 _highest_active(-1) {}
132 ///\param g The underlying graph
133 ///The range of the possible labels is [0...\c max_level],
134 ///where \c max_level is equal to the number of labeled items in the graph.
135 Elevator(const Graph &g) :
137 _max_level(countItems<Graph, Item>(g)),
138 _item_num(_max_level),
142 _first(_max_level+2),
143 _last_active(_max_level+2),
148 ///Activate item \c i.
149 void activate(Item i)
151 const int l=_level[i];
152 swap(_where[i],++_last_active[l]);
153 if(l>_highest_active) _highest_active=l;
156 ///Deactivate item \c i.
157 void deactivate(Item i)
159 swap(_where[i],_last_active[_level[i]]--);
160 while(_highest_active>=0 &&
161 _last_active[_highest_active]<_first[_highest_active])
165 ///Query whether item \c i is active
166 bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; }
168 ///Return the level of item \c i.
169 int operator[](Item i) const { return _level[i]; }
171 ///Returns an active item on level \c l.
173 ///Returns an active item on level \c l.
175 ///Returns an active item on level \c l or \ref INVALID if there is no such
176 ///an item. (\c l must be from the range [0...\c max_level].
177 Item operator[](int l) const
179 return _last_active[l]>=_first[l]?*_last_active[l]:INVALID;
182 ///Return the number of items on level \c l.
183 int onLevel(int l) const
185 return _first[l+1]-_first[l];
187 ///Return the number of items above level \c l.
188 int aboveLevel(int l) const
190 return _first[_max_level+1]-_first[l+1];
192 ///Return the number of active items on level \c l.
193 int activesOnLevel(int l) const
195 return _last_active[l]-_first[l]+1;
197 ///Return the maximum allowed level.
203 ///\name Highest Active Item
204 ///Functions for working with the highest level
209 ///Return a highest level active item.
211 ///Return a highest level active item.
213 ///\return the highest level active item or INVALID if there is no active
215 Item highestActive() const
217 return _highest_active>=0?*_last_active[_highest_active]:INVALID;
220 ///Return a highest active level.
222 ///Return a highest active level.
224 ///\return the level of the highest active item or -1 if there is no active
226 int highestActiveLevel() const
228 return _highest_active;
231 ///Lift the highest active item by one.
233 ///Lift the item returned by highestActive() by one.
235 void liftHighestActive()
237 ++_level[*_last_active[_highest_active]];
238 swap(_last_active[_highest_active]--,_last_active[_highest_active+1]);
239 --_first[++_highest_active];
242 ///Lift the highest active item.
244 ///Lift the item returned by highestActive() to level \c new_level.
246 ///\warning \c new_level must be strictly higher
247 ///than the current level.
249 void liftHighestActiveTo(int new_level)
251 const Item li = *_last_active[_highest_active];
253 copy(--_first[_highest_active+1],_last_active[_highest_active]--);
254 for(int l=_highest_active+1;l<new_level;l++)
256 copy(--_first[l+1],_first[l]);
259 copy(li,_first[new_level]);
260 _level[li]=new_level;
261 _highest_active=new_level;
266 ///Lift an active item to a higher level.
268 ///Lift an active item to a higher level.
269 ///\param i The item to be lifted. It must be active.
270 ///\param new_level The new level of \c i. It must be strictly higher
271 ///than the current level.
273 void liftTo(Item i, int new_level)
275 const int lo = _level[i];
276 const Vit w = _where[i];
278 copy(_last_active[lo],w);
279 copy(--_first[lo+1],_last_active[lo]--);
280 for(int l=lo+1;l<new_level;l++)
282 copy(_last_active[l],_first[l]);
283 copy(--_first[l+1],_last_active[l]--);
285 copy(i,_first[new_level]);
287 if(new_level>_highest_active) _highest_active=new_level;
290 // void liftToTop(int l)
292 // const Vit f=_first[l];
293 // for(int i=l;i<=_max_level;i++)
296 // _last_active[i]=f-1;
298 // for(Vit i=f;i!=_items.end();++i)
299 // _level[*i]=_max_level;
300 // for(_highest_active=l-1;
301 // _highest_active>=0 &&
302 // _last_active[_highest_active]<_first[_highest_active];
303 // _highest_active--) ;
306 ///Lift all nodes on and above a level to the top (and deactivate them).
308 ///This function lifts all nodes on and above level \c l to \c maxLevel(),
310 ///also deactivates them.
311 void liftToTop(int l)
313 const Vit f=_first[l];
314 const Vit tl=_first[_max_level];
315 for(Vit i=f;i!=tl;++i)
316 _level[*i]=_max_level;
317 for(int i=l;i<=_max_level;i++)
322 for(_highest_active=l-1;
323 _highest_active>=0 &&
324 _last_active[_highest_active]<_first[_highest_active];
334 ///\name Initialization
335 ///Using this function you can initialize the levels of the item.
337 ///This initializatios is started with calling \c initStart().
339 ///items should be listed levels by levels statring with the lowest one
340 ///(with level 0). This is done by using \c initAddItem()
341 ///and \c initNewLevel(). Finally \c initFinish() must be called.
342 ///The items not listed will be put on the highest level.
345 ///Start the initialization process.
350 _init_num=_items.begin();
351 _first[0]=_items.begin();
352 _last_active[0]=_items.begin()-1;
353 Vit n=_items.begin();
354 for(typename ItemSetTraits<Graph,Item>::ItemIt i(_g);i!=INVALID;++i)
358 _level[i]=_max_level;
363 ///Add an item to the current level.
365 void initAddItem(Item i)
367 swap(_where[i],_init_num);
372 ///Start a new level.
374 ///Start a new level.
375 ///It shouldn't be used before the items on level 0 are listed.
379 _first[_init_lev]=_init_num;
380 _last_active[_init_lev]=_init_num-1;
383 ///Finalize the initialization process.
387 for(_init_lev++;_init_lev<=_max_level;_init_lev++)
389 _first[_init_lev]=_init_num;
390 _last_active[_init_lev]=_init_num-1;
392 _first[_max_level+1]=_items.begin()+_item_num;
393 _last_active[_max_level+1]=_items.begin()+_item_num-1;
400 } //END OF NAMESPACE LEMON