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