Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

MaxFlowEdmondsKarp.h
Go to the documentation of this file.
1 
33 #pragma once
34 
36 
37 namespace ogdf {
38 
40 
43 template<typename TCap>
44 class MaxFlowEdmondsKarp : public MaxFlowModule<TCap>
45 {
46 private:
47 
51  {
52  NodeArray<adjEntry> pred(*this->m_G, nullptr); // edges from the predecessor
53  List<node> queue;
54  queue.pushBack(*this->m_s);
55 
56  while (!queue.empty()) {
57  node v = queue.popFrontRet();
58  if (v == *this->m_t) {
59  // find minimum residual capacity value on s-t-path
60  TCap augmentVal = std::numeric_limits<TCap>::max();
61  for (node w = v; pred[w]; w = pred[w]->theNode()) {
62  const adjEntry adj = pred[w];
63  const edge e = adj->theEdge();
64  if (e->target() == w) { // real edge e = vw
65  if ((*this->m_cap)[e] - (*this->m_flow)[e] < augmentVal) {
66  augmentVal = (*this->m_cap)[e] - (*this->m_flow)[e];
67  }
68  } else { // virtual edge e = wv
69  if ((*this->m_flow)[e] < augmentVal) {
70  augmentVal = (*this->m_flow)[e];
71  }
72  }
73  }
74  // augment s-t-path by this value
75  for (node w = v; pred[w]; w = pred[w]->theNode()) {
76  const adjEntry adj = pred[w];
77  const edge e = adj->theEdge();
78  if (e->target() == w) { // real edge e = vw
79  (*this->m_flow)[e] += augmentVal;
80  } else { // virtual edge e = wv
81  (*this->m_flow)[e] -= augmentVal;
82  }
83  }
84  return true;
85  }
86  for(adjEntry adj : v->adjEntries) {
87  const node w = adj->twinNode();
88  if (w != (*this->m_s)
89  && !pred[w]) // if not already visited
90  {
91  const edge e = adj->theEdge();
92  if (e->source() == v) { // real edge e = vw
93  if ( (*this->m_et).greater((*this->m_cap)[e],(*this->m_flow)[e]) ) { // reachable in residual graph
94  queue.pushBack(w);
95  pred[w] = adj;
96  }
97  } else { // virtual edge (adj) wv
98  if ( (*this->m_et).greater( (*this->m_flow)[e] , (TCap) 0 )) { // reachable in residual graph
99  queue.pushBack(w);
100  pred[w] = adj;
101  }
102  }
103  }
104  }
105  }
106 
107  return false;
108  }
109 
110 public:
111 
116 
122  TCap computeValue(const EdgeArray<TCap> &cap, const node &s, const node &t)
123  {
124  // clear old flow
125  this->m_flow->fill((TCap) 0);
126  // store capacity, source and sink
127  this->m_cap = &cap;
128  this->m_s = &s;
129  this->m_t = &t;
131 
132  if (*this->m_s == *this->m_t) {
133  return (TCap) 0;
134  }
135 
137  TCap flowValue = 0;
138  for(adjEntry adj : s->adjEntries) {
139  edge e = adj->theEdge();
140  if(e->source() == s) {
141  flowValue += (*this->m_flow)[e];
142  } else {
143  flowValue -= (*this->m_flow)[e];
144  }
145  }
146  return flowValue;
147  }
148 
151  void computeFlowAfterValue() { /* nothing to do here */ }
152 
153  // use methods from super class
159 };
160 
161 }
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
const node * m_t
Pointer to the sink node.
Definition: MaxFlowModule.h:48
EpsilonTest * m_et
Pointer to the used EpsilonTest.
Definition: MaxFlowModule.h:43
Interface for Max Flow Algorithms.
const Graph * m_G
Pointer to the given graph.
Definition: MaxFlowModule.h:45
void fill(const T &x)
Sets all array elements to x.
Definition: EdgeArray.h:302
Computes a max flow via Edmonds-Karp.
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:438
bool augmentShortestSourceSinkPath()
If there is an augumenting path: do an interation step. Otherwise return false so that the algorithm ...
const node * m_s
Pointer to the source node.
Definition: MaxFlowModule.h:47
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
EdgeArray< TCap > * m_flow
Pointer to (extern) flow array.
Definition: MaxFlowModule.h:44
TCap computeValue(const EdgeArray< TCap > &cap, const node &s, const node &t)
Implementation of computeValue from the super class. The flow array is cleared, cap, s and t are stored and Edmonds&Karp starts. After this first phase, the flow itself is already computed! Returns 0 if source and sink are identical.
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
E popFrontRet()
Removes the first element from the list and returns it.
Definition: List.h:1417
Class for the representation of nodes.
Definition: Graph_d.h:169
bool isFeasibleInstance() const
Return whether the instance is feasible, i.e. the capacities are non-negative.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
The namespace for all OGDF objects.
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
Class for adjacency list elements.
Definition: Graph_d.h:79
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
void computeFlowAfterValue()
Implementation of computeFlowAfterValue from the super class. This does nothing, because the algorith...
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200