Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

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 = 0; // 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);
261 
262  m_label.init(*this->m_G);
263  m_ex.init(*this->m_G, 0);
264 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
265  m_activeLabelListPosition.init(*this->m_G, nullptr);
266  m_activeLabelList.init(1, this->m_G->numberOfNodes() - 1);
267  m_maxLabel = 0;
268 #endif
269 #ifdef OGDF_GT_USE_GAP_RELABEL_HEURISTIC
270  m_labelListPosition.init(*this->m_G, nullptr);
271  m_labelList.init(this->m_G->numberOfNodes() + 1);
272 #endif
273  m_cutNodes.clear();
274 
275  // initialize residual graph for first preflow
276  for(edge e : this->m_G->edges) {
277  if (e->source() == *this->m_s
278  && e->target() != *this->m_s) { // ignore loops
279  (*this->m_flow)[e] = getCap(e);
280  m_ex[e->target()] += getCap(e); // "+" needed for the case of multigraphs
281  }
282  }
283 
284  if(*this->m_t == *this->m_s) {
285  return (TCap) 0;
286  }
287 
288  NodeArray<adjEntry> curr(*this->m_G);
289  for (node v = this->m_G->firstNode(); v; v = v->succ()) {
290  curr[v] = v->firstAdj();
291  }
292 
293  globalRelabel(); // initialize distance labels
294 
295  int relCount = 0; // counts the relabel operations for the global relabeling heuristic
296 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
297  while (m_maxLabel != 0) {
298  OGDF_ASSERT(!m_activeLabelList[m_maxLabel].empty());
299  const node v = m_activeLabelList[m_maxLabel].front();
300  OGDF_ASSERT(m_maxLabel == m_label[v]);
301  OGDF_ASSERT(m_activeLabelListPosition[v] == m_activeLabelList[m_maxLabel].begin());
302 #else
303  List<node> active;
304  for(adjEntry adj : (*this->m_s)->adjEntries) {
305  node w = adj->theEdge()->target();
306  if (w != *this->m_s) {
307  active.pushBack(w);
308  }
309  }
310  while (!active.empty()) {
311  const node v = active.front();
312 #endif
313  adjEntry &adj = curr[v];
314  if (v == *this->m_s
315  || v == *this->m_t
316  || !isActive(v)) {
317  // source, sink or not active: remove activity status
318 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
319  setInactive(v);
320 #else
321  active.popFront();
322 #endif
323  } else {
324  while (this->m_et->greater(m_ex[v], (TCap) 0)) {
325  if (isAdmissible(adj)) {
326  // push and adjacent node becomes active
327 #ifdef OGDF_GT_USE_MAX_ACTIVE_LABEL
328  const node w = adj->twinNode();
329  if (w != *this->m_s
330  && w != *this->m_t
331  && !isActive(w)) {
332  // w will become active after push
333  setActive(w);
334  }
335  push(adj);
336  if (v != *this->m_s
337  && !isActive(v)) {
338  setInactive(v);
339  }
340 #else
341  push(adj);
342  active.pushBack(adj->twinNode());
343 #endif
344  } else {
345  if (adj != v->lastAdj()) {
346  adj = adj->succ();
347  } else { // end of adjacency list
348  adj = v->firstAdj();
349  relabel(v);
350  ++relCount;
351 #ifdef OGDF_GT_USE_GAP_RELABEL_HEURISTIC
352  // only gapRelabel if we do not do a globalRelabel directly afterwards
353  if (relCount != this->m_G->numberOfNodes()
354 # if (OGDF_GT_GRH_STEPS > 1)
355  && relCount % OGDF_GT_GRH_STEPS == 0 // obey frequency of gap relabel heuristic
356 # endif
357  ) {
358  gapRelabel();
359  }
360 #endif
361  break;
362  }
363  }
364  }
365  if (relCount == this->m_G->numberOfNodes()) {
366  relCount = 0;
367  globalRelabel();
368  }
369  }
370  }
371 
372  TCap result = 0;
373  for(adjEntry adj : (*this->m_t)->adjEntries) {
374  edge e = adj->theEdge();
375  if(e->target() == *this->m_t) {
376  result += (*this->m_flow)[e];
377  } else {
378  result -= (*this->m_flow)[e];
379  }
380  }
381  return result;
382  }
383 
384  // second stage: push excess that has not reached the sink back towards source
386  {
387  List<node> active;
388 #ifdef OGDF_GT_USE_PUSH_RELABEL_SECOND_STAGE
389  NodeArray<adjEntry> curr(*this->m_G);
390  for (node v = this->m_G->firstNode(); v; v = v->succ()) {
391  curr[v] = v->firstAdj();
392  m_label[v] = 1;
393  if (this->m_et->greater(m_ex[v], (TCap) 0) && v != *this->m_s && v != *this->m_t) {
394  active.pushBack(v);
395  }
396  }
397  if (active.empty()) {
398  return;
399  }
400 
401  m_label[*this->m_s] = 0;
402  while (!active.empty()) {
403  node v = active.front();
404  if (v == *this->m_s
405  || v == *this->m_t
406  || !isActive(v)) {
407  active.popFront();
408  } else {
409  adjEntry &adj = curr[v];
410  if (isAdmissible(adj)) {
411  push(adj);
412  active.pushBack(adj->twinNode());
413  } else {
414  if (adj == v->lastAdj()) {
415  // no admissible outgoing edge found -> relabel node!
416  relabelStage2(v);
417  adj = v->firstAdj();
418 #if 0
419  // node is still active but move it to the end of the queue
420  // (don't know if this is really necessary)
421  active.popFront();
422  active.pushBack(v);
423 #endif
424  } else {
425  adj = adj->succ();
426  }
427  }
428  }
429  }
430 #else // USE_PUSH_RELABEL_SECOND_STAGE
431  m_ex[*this->m_s] = m_ex[*this->m_t] = 0;
432  for (node v = this->m_G->firstNode(); v; v = v->succ()) {
433  if (this->m_et->greater(m_ex[v], (TCap) 0)) {
434  active.pushBack(v);
435  }
436  }
437  while (!active.empty()) {
438  const node v = active.popFrontRet();
439  if (this->m_et->greater(m_ex[v], (TCap) 0) && v != *this->m_s && v != *this->m_t) {
440  for (adjEntry adj = v->firstAdj(); adj; adj = adj->succ()) {
441  const edge e = adj->theEdge();
442  const node u = e->source();
443  if (u != v) { // e is incoming edge
444  if (this->m_et->greater(m_ex[v], (TCap) 0)
445  && isResidualEdge(adj)) {
446  push(adj);
447  if (u != *this->m_s) {
448  active.pushFront(u);
449  }
450  }
451  }
452  }
453  }
454  }
455 #endif // USE_PUSH_RELABEL_SECOND_STAGE
456  }
462 };
463 
464 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
void clear()
Removes all elements from the list.
Definition: List.h:1451
TCap getCap(const edge e) const
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:200
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
void init()
Reinitializes the array. Associates the array with no graph.
Definition: EdgeArray.h:283
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:255
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:354
void push(const adjEntry adj)
void popFront()
Removes the first element from the list.
Definition: List.h:1411
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:618
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:218
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:568
iterator pushFront(const E &x)
Adds element x at the beginning of the list.
Definition: List.h:1360
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:274
bool isResidualEdge(const adjEntry adj) const
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
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:108
Class for the representation of edges.
Definition: Graph_d.h:292
adjEntry twin() const
Returns the corresponding adjacency element associated with the same edge.
Definition: Graph_d.h:105
E popFrontRet()
Removes the first element from the list and returns it.
Definition: List.h:1417
adjEntry lastAdj() const
Returns the last entry in the adjacency list.
Definition: Graph_d.h:215
NodeArray< ListIterator< node > > m_activeLabelListPosition
Class for the representation of nodes.
Definition: Graph_d.h:169
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
bool isFeasibleInstance() const
Return whether the instance is feasible, i.e. the capacities are non-negative.
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:148
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
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:598
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:213
bool empty() const
Returns true iff the list is empty.
Definition: List.h:256
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:100
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:79
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:200