Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

MaxFlowGoldbergTarjan.h
Go to the documentation of this file.
1 
33 #pragma once
34 
36 
37 //#define OGDF_GT_USE_GAP_RELABEL_HEURISTIC
38 #define OGDF_GT_USE_MAX_ACTIVE_LABEL
39 #ifdef OGDF_GT_USE_GAP_RELABEL_HEURISTIC
40 //#define OGDF_GT_GRH_STEPS 1 // gap relabel frequency: call gapRelabel() after OGDF_GT_GRH_STEPS relabel() operations (1 == off)
41 #endif
42 #define OGDF_GT_USE_PUSH_RELABEL_SECOND_STAGE
43 // world666 is much better without OGDF_GT_USE_PUSH_RELABEL_SECOND_STAGE
44 
45 namespace ogdf {
46 
48 
51 template<typename TCap>
53 {
55  NodeArray<TCap> m_ex; // ex_f(v) values will be saved here to save runtime
56 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
57  NodeArray< ListIterator<node> > m_activeLabelListPosition; // holds the iterator of every active node in the corresp. list of m_labeList
58  Array< List<node> > m_activeLabelList; // array indexed by label, contains list of active nodes with that label
59  int m_maxLabel; // the maximum label among all active nodes
60 #endif
61 #ifdef OGDF_GT_USE_GAP_RELABEL_HEURISTIC
62  NodeArray< ListIterator<node> > m_labelListPosition; // holds the iterator of every node in the corresp. list of m_labeList
63  Array< List<node> > m_labelList; // array indexed by label, contains list of nodes with that label
64 #endif
65 
68 
69  inline TCap getCap(const edge e) const {
70  return e->target() == *this->m_s ? 0 : (*this->m_cap)[e];
71  }
72 
73  inline bool isResidualEdge(const adjEntry adj) const
74  {
75  const edge e = adj->theEdge();
76  if (adj->theNode() == e->source()) {
77  return this->m_et->less((*this->m_flow)[e], getCap(e));
78  }
79  return this->m_et->greater((*this->m_flow)[e], (TCap) 0);
80  }
81 
82  inline bool isAdmissible(const adjEntry adj) const
83  {
84  OGDF_ASSERT(adj);
85  return (isResidualEdge(adj)
86  && m_label[adj->theNode()] == m_label[adj->twinNode()] + 1);
87  }
88 
89  inline bool isActive(const node v) const
90  {
91  OGDF_ASSERT((v != *this->m_s && v != *this->m_t)
92  || (m_label[*this->m_s] == this->m_G->numberOfNodes() && m_label[*this->m_t] == 0));
93  return (this->m_et->greater(m_ex[v], (TCap) 0)
94  && this->m_G->numberOfNodes() > m_label[v]
95  && m_label[v] > 0);
96  }
97 
98 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
99  inline void setActive(const node v)
100  {
101  const int label = m_label[v];
102  OGDF_ASSERT(0 < label);
103  OGDF_ASSERT(label < this->m_G->numberOfNodes());
104  OGDF_ASSERT(!m_activeLabelListPosition[v].valid());
105  m_activeLabelListPosition[v] = m_activeLabelList[label].pushBack(v);
106  if (label > m_maxLabel) {
107  m_maxLabel = label;
108  }
109  }
110 
111  inline void findNewMaxLabel()
112  {
113  while (m_maxLabel > 0
114  && m_activeLabelList[m_maxLabel].empty()) {
115  --m_maxLabel;
116  }
117  }
118 
119  inline void setInactive(const node v)
120  {
121  OGDF_ASSERT(m_activeLabelListPosition[v].valid());
122  m_activeLabelList[m_label[v]].del(m_activeLabelListPosition[v]);
123  m_activeLabelListPosition[v] = nullptr;
124  findNewMaxLabel();
125  }
126 #endif
127 
128  // sets label of v, maintaining m_labelList (moves node v to the correct list in the array)
129  inline void setLabel(const node v, int label)
130  {
131 #ifdef OGDF_GT_USE_GAP_RELABEL_HEURISTIC
132  if (m_labelListPosition[v].valid()) {
133  m_labelList[m_label[v]].del(m_labelListPosition[v]); // delete node from old list using noted position
134  }
135  m_labelListPosition[v] = m_labelList[label].pushBack(v); // push node to new list and update iterator
136 #endif
137 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
138  if (m_activeLabelListPosition[v].valid()) {
139  OGDF_ASSERT(0 < m_label[v]);
140  OGDF_ASSERT(m_label[v] < this->m_G->numberOfNodes());
141  setInactive(v);
142  }
143  m_label[v] = label; // update label
144  if (v != *this->m_s
145  && v != *this->m_t
146  && isActive(v)) {
147  setActive(v);
148  }
149 #else
150  m_label[v] = label; // update label
151 #endif
152  }
153 
154 #ifdef OGDF_GT_USE_GAP_RELABEL_HEURISTIC
155  void gapRelabel()
156  {
157 # ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
158  // XXX: this is a test but it seems to work and it seems to be fast!
159  const int n = m_maxLabel + 1;
160 # else
161  const int n = this->m_G->numberOfNodes();
162 # endif
163  for (int i = 1; i < n - 1; ++i) {
164  if (m_labelList[i].empty()) {
165  for (int j = i + 1; j < n; ++j) {
166  while (!m_labelList[j].empty()) {
167  setLabel(m_labelList[j].front(), this->m_G->numberOfNodes());
168  }
169  }
170  break;
171  }
172  }
173  }
174 #endif
175 
176  void push(const adjEntry adj)
177  {
178  const edge e = adj->theEdge();
179  const node v = adj->theNode();
180  if (v == e->source()) {
181  const TCap value = min(m_ex[v], getCap(e) - (*this->m_flow)[e]);
182  OGDF_ASSERT(this->m_et->geq(value, (TCap) 0));
183  (*this->m_flow)[e] += value;
184  m_ex[v] -= value;
185  m_ex[adj->twinNode()] += value;
186  } else {
187  const TCap value = min(m_ex[v], (*this->m_flow)[adj]);
188  OGDF_ASSERT(this->m_et->geq(value, (TCap) 0));
189  (*this->m_flow)[adj] -= value;
190  m_ex[v] -= value;
191  m_ex[adj->twinNode()] += value;
192  }
193  }
194 
196  {
197  // breadth-first search to relabel nodes with their respective distance to the sink in the residual graph
198  const int n = this->m_G->numberOfNodes();
199  NodeArray<int> dist(*this->m_G, n); // distance array
200  List<node> queue; // reachable, not processed nodes
201  dist[*this->m_t] = 0;
202  queue.pushBack(*this->m_t);
203  while (!queue.empty()) { // is there a node to check?
204  node w = queue.popFrontRet();
205  for(adjEntry adj : w->adjEntries) {
206  node x = adj->twinNode();
207  if (isResidualEdge(adj->twin())
208  && dist[x] == n) { // not already seen
209  dist[x] = dist[w] + 1; // set distance of node to sink
210  queue.pushBack(x);
211  }
212  }
213  }
214  // set distance of unreachable nodes to "number of nodes" thus making them inactive
215  for(node w : this->m_G->nodes) {
216  setLabel(w, dist[w]);
217  }
218  }
219 
220  void relabel(const node v)
221  {
222  int minLabel = this->m_G->numberOfNodes() - 1;
223  for(adjEntry adj : v->adjEntries) {
224  if (isResidualEdge(adj)) {
225  const int label = m_label[adj->twinNode()];
226  if (label < minLabel) {
227  minLabel = label;
228  }
229  }
230  }
231  if (minLabel + 1 != m_label[v]) { // == can happen after global relabel
232  setLabel(v, minLabel + 1);
233  }
234  }
235 
236  void relabelStage2(const node v)
237  {
238  int minLabel = this->m_G->numberOfNodes() - 1;
239  for(adjEntry adj : v->adjEntries) {
240  if (isResidualEdge(adj)) {
241  const int label = m_label[adj->twinNode()];
242  if (label < minLabel) {
243  minLabel = label;
244  }
245  }
246  }
247  OGDF_ASSERT(minLabel + 1 != m_label[v]);
248  m_label[v] = minLabel + 1;
249  }
250 
251 public:
252  // first stage: push excess towards sink
253  TCap computeValue(const EdgeArray<TCap> &cap, const node &s, const node &t)
254  {
255  // TODO: init this stuff in the module?
256  this->m_s = &s;
257  this->m_t = &t;
258  this->m_cap = &cap;
259  this->m_flow->init(*this->m_G, (TCap) 0);
260 
261  m_label.init(*this->m_G);
262  m_ex.init(*this->m_G, 0);
263 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
264  m_activeLabelListPosition.init(*this->m_G, nullptr);
265  m_activeLabelList.init(1, this->m_G->numberOfNodes() - 1);
266  m_maxLabel = 0;
267 #endif
268 #ifdef OGDF_GT_USE_GAP_RELABEL_HEURISTIC
269  m_labelListPosition.init(*this->m_G, nullptr);
270  m_labelList.init(this->m_G->numberOfNodes() + 1);
271 #endif
272  m_cutNodes.clear();
273 
274  // initialize residual graph for first preflow
275  for(edge e : this->m_G->edges) {
276  if (e->source() == *this->m_s
277  && e->target() != *this->m_s) { // ignore loops
278  (*this->m_flow)[e] = getCap(e);
279  m_ex[e->target()] += getCap(e); // "+" needed for the case of multigraphs
280  }
281  }
282 
283  if(*this->m_t == *this->m_s) {
284  return (TCap) 0;
285  }
286 
287  NodeArray<adjEntry> curr(*this->m_G);
288  for (node v = this->m_G->firstNode(); v; v = v->succ()) {
289  curr[v] = v->firstAdj();
290  }
291 
292  globalRelabel(); // initialize distance labels
293 
294  int relCount = 0; // counts the relabel operations for the global relabeling heuristic
295 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
296  while (m_maxLabel != 0) {
297  OGDF_ASSERT(!m_activeLabelList[m_maxLabel].empty());
298  const node v = m_activeLabelList[m_maxLabel].front();
299  OGDF_ASSERT(m_maxLabel == m_label[v]);
300  OGDF_ASSERT(m_activeLabelListPosition[v] == m_activeLabelList[m_maxLabel].begin());
301 #else
302  List<node> active;
303  for(adjEntry adj : this->m_s->adjEntries) {
304  node w = adj->theEdge()->target();
305  if (w != *this->m_s) {
306  active.pushBack(w);
307  }
308  }
309  while (!active.empty()) {
310  const node v = active.front();
311 #endif
312  adjEntry &adj = curr[v];
313  if (v == *this->m_s
314  || v == *this->m_t
315  || !isActive(v)) {
316  // source, sink or not active: remove activity status
317 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
318  setInactive(v);
319 #else
320  active.popFront();
321 #endif
322  } else {
323  while (this->m_et->greater(m_ex[v], (TCap) 0)) {
324  if (isAdmissible(adj)) {
325  // push and adjacent node becomes active
326 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
327  const node w = adj->twinNode();
328  if (w != *this->m_s
329  && w != *this->m_t
330  && !isActive(w)) {
331  // w will become active after push
332  setActive(w);
333  }
334  push(adj);
335  if (v != *this->m_s
336  && !isActive(v)) {
337  setInactive(v);
338  }
339 #else
340  push(adj);
341  active.pushBack(adj->twinNode());
342 #endif
343  } else {
344  if (adj != v->lastAdj()) {
345  adj = adj->succ();
346  } else { // end of adjacency list
347  adj = v->firstAdj();
348  relabel(v);
349  ++relCount;
350 #ifdef OGDF_GT_USE_GAP_RELABEL_HEURISTIC
351  // only gapRelabel if we do not do a globalRelabel directly afterwards
352  if (relCount != this->m_G->numberOfNodes()
353 # if (OGDF_GT_GRH_STEPS > 1)
354  && relCount % OGDF_GT_GRH_STEPS == 0 // obey frequency of gap relabel heuristic
355 # endif
356  ) {
357  gapRelabel();
358  }
359 #endif
360  break;
361  }
362  }
363  }
364  if (relCount == this->m_G->numberOfNodes()) {
365  relCount = 0;
366  globalRelabel();
367  }
368  }
369  }
370 
371  TCap result = 0;
372  for(adjEntry adj : (*this->m_t)->adjEntries) {
373  edge e = adj->theEdge();
374  if(e->target() == *this->m_t) {
375  result += (*this->m_flow)[e];
376  } else {
377  result -= (*this->m_flow)[e];
378  }
379  }
380  return result;
381  }
382 
383  // second stage: push excess that has not reached the sink back towards source
385  {
386  List<node> active;
387 #ifdef OGDF_GT_USE_PUSH_RELABEL_SECOND_STAGE
388  NodeArray<adjEntry> curr(*this->m_G);
389  for (node v = this->m_G->firstNode(); v; v = v->succ()) {
390  curr[v] = v->firstAdj();
391  m_label[v] = 1;
392  if (this->m_et->greater(m_ex[v], (TCap) 0) && v != *this->m_s && v != *this->m_t) {
393  active.pushBack(v);
394  }
395  }
396  if (active.empty()) {
397  return;
398  }
399 
400  m_label[*this->m_s] = 0;
401  while (!active.empty()) {
402  node v = active.front();
403  if (v == *this->m_s
404  || v == *this->m_t
405  || !isActive(v)) {
406  active.popFront();
407  } else {
408  adjEntry &adj = curr[v];
409  if (isAdmissible(adj)) {
410  push(adj);
411  active.pushBack(adj->twinNode());
412  } else {
413  if (adj == v->lastAdj()) {
414  // no admissible outgoing edge found -> relabel node!
415  relabelStage2(v);
416  adj = v->firstAdj();
417 #if 0
418  // node is still active but move it to the end of the queue
419  // (don't know if this is really necessary)
420  active.popFront();
421  active.pushBack(v);
422 #endif
423  } else {
424  adj = adj->succ();
425  }
426  }
427  }
428  }
429 #else // USE_PUSH_RELABEL_SECOND_STAGE
430  m_ex[*this->m_s] = m_ex[*this->m_t] = 0;
431  for (node v = this->m_G->firstNode(); v; v = v->succ()) {
432  if (this->m_et->greater(m_ex[v], (T) 0)) {
433  active.pushBack(v);
434  }
435  }
436  while (!active.empty()) {
437  const node v = active.popFrontRet();
438  if (this->m_et->greater(m_ex[v], (T) 0) && v != *this->m_s && v != *this->m_t) {
439  for (adjEntry adj = v->firstAdj(); adj; adj = adj->succ()) {
440  const edge e = adj->theEdge();
441  const node u = e->source();
442  if (u != v) { // e is incoming edge
443  if (this->m_et->greater(m_ex[v], (T) 0)
444  && isResidualEdge(adj)) {
445  push(adj);
446  if (u != *this->m_s) {
447  active.pushFront(u);
448  }
449  }
450  }
451  }
452  }
453  }
454 #endif // USE_PUSH_RELABEL_SECOND_STAGE
455  }
461 };
462 
463 } // namespace ogdf
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
void clear()
Removes all elements from the list.
Definition: List.h:1394
TCap getCap(const edge e) const
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:56
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:95
void init()
Reinitializes the array. Associates the array with no graph.
Definition: EdgeArray.h:320
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:292
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:206
void push(const adjEntry adj)
void popFront()
Removes the first element from the list.
Definition: List.h:1354
const node * m_t
Pointer to the sink node.
Definition: MaxFlowModule.h:48
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:598
EpsilonTest * m_et
Pointer to the used EpsilonTest.
Definition: MaxFlowModule.h:43
Interface for Max Flow Algorithms.
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:216
const Graph * m_G
Pointer to the given graph.
Definition: MaxFlowModule.h:45
std::enable_if< std::is_integral< T >::value, bool >::type less(const T &x, const T &y) const
Compare if x is LESS than y for integral types.
Definition: EpsilonTest.h:57
Array< List< node > > m_activeLabelList
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:548
iterator pushFront(const E &x)
Adds element x at the beginning of the list.
Definition: List.h:1303
const node * m_s
Pointer to the source node.
Definition: MaxFlowModule.h:47
void computeFlowAfterValue()
Compute the flow itself after the flow value is already computed. Only used in algorithms with 2 phas...
const_reference front() const
Returns a const reference to the first element.
Definition: List.h:254
bool isResidualEdge(const adjEntry adj) const
Doubly linked lists (maintaining the length of the list).
Definition: List.h:39
bool isAdmissible(const adjEntry adj) const
TCap computeValue(const EdgeArray< TCap > &cap, const node &s, const node &t)
Compute only the value of the flow.
EdgeArray< TCap > * m_flow
Pointer to (extern) flow array.
Definition: MaxFlowModule.h:44
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:105
Class for the representation of edges.
Definition: Graph_d.h:291
adjEntry twin() const
Returns the corresponding adjacency element associated with the same edge.
Definition: Graph_d.h:102
E popFrontRet()
Removes the first element from the list and returns it.
Definition: List.h:1360
adjEntry lastAdj() const
Returns the last entry in the adjacency list.
Definition: Graph_d.h:213
NodeArray< ListIterator< node > > m_activeLabelListPosition
Class for the representation of nodes.
Definition: Graph_d.h:167
std::enable_if< std::is_integral< T >::value, bool >::type greater(const T &x, const T &y) const
Compare if x is GREATER than y for integral types.
Definition: EpsilonTest.h:169
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:145
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
The namespace for all OGDF objects.
std::enable_if< std::is_integral< T >::value, bool >::type geq(const T &x, const T &y) const
Compare if x is GEQ to y for integral types.
Definition: EpsilonTest.h:141
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:578
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:545
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:211
bool empty() const
Returns true iff the list is empty.
Definition: List.h:236
const EdgeArray< TCap > * m_cap
Pointer to the given capacity array.
Definition: MaxFlowModule.h:46
node theNode() const
Returns the node whose adjacency list contains this element.
Definition: Graph_d.h:99
Computes a max flow via Preflow-Push (global relabeling and gap relabeling heuristic).
void setLabel(const node v, int label)
Class for adjacency list elements.
Definition: Graph_d.h:78
bool isActive(const node v) const
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:198