Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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