Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

MaxFlowEdmondsKarp.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/Graph.h>
36 #include <ogdf/basic/GraphList.h>
37 #include <ogdf/basic/List.h>
38 #include <ogdf/basic/basic.h>
40 
41 #include <limits>
42 
43 namespace ogdf {
44 
46 
49 template<typename TCap>
50 class MaxFlowEdmondsKarp : public MaxFlowModule<TCap> {
51 private:
55  NodeArray<adjEntry> pred(*this->m_G, nullptr); // edges from the predecessor
56  List<node> queue;
57  queue.pushBack(*this->m_s);
58 
59  while (!queue.empty()) {
60  node v = queue.popFrontRet();
61  if (v == *this->m_t) {
62  // find minimum residual capacity value on s-t-path
63  TCap augmentVal = std::numeric_limits<TCap>::max();
64  for (node w = v; pred[w]; w = pred[w]->theNode()) {
65  const adjEntry adj = pred[w];
66  const edge e = adj->theEdge();
67  if (e->target() == w) { // real edge e = vw
68  if ((*this->m_cap)[e] - (*this->m_flow)[e] < augmentVal) {
69  augmentVal = (*this->m_cap)[e] - (*this->m_flow)[e];
70  }
71  } else { // virtual edge e = wv
72  if ((*this->m_flow)[e] < augmentVal) {
73  augmentVal = (*this->m_flow)[e];
74  }
75  }
76  }
77  // augment s-t-path by this value
78  for (node w = v; pred[w]; w = pred[w]->theNode()) {
79  const adjEntry adj = pred[w];
80  const edge e = adj->theEdge();
81  if (e->target() == w) { // real edge e = vw
82  (*this->m_flow)[e] += augmentVal;
83  } else { // virtual edge e = wv
84  (*this->m_flow)[e] -= augmentVal;
85  }
86  }
87  return true;
88  }
89  for (adjEntry adj : v->adjEntries) {
90  const node w = adj->twinNode();
91  if (w != (*this->m_s) && !pred[w]) // if not already visited
92  {
93  const edge e = adj->theEdge();
94  if (e->source() == v) { // real edge e = vw
95  if ((*this->m_et).greater((*this->m_cap)[e], (*this->m_flow)[e])) {
96  // reachable in residual graph
97  queue.pushBack(w);
98  pred[w] = adj;
99  }
100  } else { // virtual edge (adj) wv
101  if ((*this->m_et).greater((*this->m_flow)[e], (TCap)0)) {
102  // reachable in residual graph
103  queue.pushBack(w);
104  pred[w] = adj;
105  }
106  }
107  }
108  }
109  }
110 
111  return false;
112  }
113 
114 public:
119 
125  TCap computeValue(const EdgeArray<TCap>& cap, const node& s, const node& t) {
126  // clear old flow
127  this->m_flow->fill((TCap)0);
128  // store capacity, source and sink
129  this->m_cap = &cap;
130  this->m_s = &s;
131  this->m_t = &t;
133 
134  if (*this->m_s == *this->m_t) {
135  return (TCap)0;
136  }
137 
139  ;
140  }
141  TCap flowValue = 0;
142  for (adjEntry adj : s->adjEntries) {
143  edge e = adj->theEdge();
144  if (e->source() == s) {
145  flowValue += (*this->m_flow)[e];
146  } else {
147  flowValue -= (*this->m_flow)[e];
148  }
149  }
150  return flowValue;
151  }
152 
155  void computeFlowAfterValue() { /* nothing to do here */
156  }
157 
158  // use methods from super class
164 };
165 
166 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
Graph.h
Includes declaration of graph class.
ogdf::MaxFlowModule< TCap >::isFeasibleInstance
bool isFeasibleInstance() const
Return whether the instance is feasible, i.e. the capacities are non-negative.
Definition: MaxFlowModule.h:148
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:54
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
MaxFlowModule.h
Interface for Max Flow Algorithms.
ogdf::MaxFlowModule< TCap >::m_t
const node * m_t
Pointer to the sink node.
Definition: MaxFlowModule.h:48
ogdf::List::popFrontRet
E popFrontRet()
Removes the first element from the list and returns it.
Definition: List.h:1591
ogdf::MaxFlowEdmondsKarp
Computes a max flow via Edmonds-Karp.
Definition: MaxFlowEdmondsKarp.h:50
ogdf::AdjElement
Class for adjacency list elements.
Definition: Graph_d.h:142
ogdf::MaxFlowModule< TCap >::m_et
EpsilonTest * m_et
Pointer to the used EpsilonTest.
Definition: MaxFlowModule.h:43
ogdf::AdjElement::theEdge
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:160
ogdf::MaxFlowModule< TCap >::m_s
const node * m_s
Pointer to the source node.
Definition: MaxFlowModule.h:47
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:125
ogdf::MaxFlowModule< TCap >::m_G
const Graph * m_G
Pointer to the given graph.
Definition: MaxFlowModule.h:45
GraphList.h
Decralation of GraphElement and GraphList classes.
ogdf::NodeElement::adjEntries
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:271
ogdf::EdgeElement::source
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:398
ogdf::List
Doubly linked lists (maintaining the length of the list).
Definition: DfsMakeBiconnected.h:40
ogdf::internal::GraphRegisteredArray
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition: Graph_d.h:658
ogdf::AdjElement::twinNode
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:175
basic.h
Basic declarations, included by all source files.
ogdf::MaxFlowModule
Definition: MaxFlowModule.h:41
ogdf::EdgeElement
Class for the representation of edges.
Definition: Graph_d.h:363
List.h
Declaration of doubly linked lists and iterators.
ogdf::MaxFlowModule< TCap >::m_cap
const EdgeArray< TCap > * m_cap
Pointer to the given capacity array.
Definition: MaxFlowModule.h:46
ogdf::EdgeElement::target
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:401
ogdf::NodeElement
Class for the representation of nodes.
Definition: Graph_d.h:240
ogdf::MaxFlowModule< TCap >::m_flow
EdgeArray< TCap > * m_flow
Pointer to (extern) flow array.
Definition: MaxFlowModule.h:44
ogdf::List::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1547
ogdf::internal::EdgeArrayBase2
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition: Graph_d.h:716
ogdf::MaxFlowEdmondsKarp::computeFlowAfterValue
void computeFlowAfterValue()
Implementation of computeFlowAfterValue from the super class. This does nothing, because the algorith...
Definition: MaxFlowEdmondsKarp.h:155