Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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 = 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;
130 
131  if (*this->m_s == *this->m_t) {
132  return (TCap) 0;
133  }
134 
136  TCap flowValue = 0;
137  for(adjEntry adj : s->adjEntries) {
138  edge e = adj->theEdge();
139  if(e->source() == s) {
140  flowValue += (*this->m_flow)[e];
141  } else {
142  flowValue -= (*this->m_flow)[e];
143  }
144  }
145  return flowValue;
146  }
147 
150  void computeFlowAfterValue() { /* nothing to do here */ }
151 
152  // use methods from super class
158 };
159 
160 }// namespace ogdf
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:95
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
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:339
Computes a max flow via Edmonds-Karp.
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:423
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:39
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:105
Class for the representation of edges.
Definition: Graph_d.h:291
E popFrontRet()
Removes the first element from the list and returns it.
Definition: List.h:1360
Class for the representation of nodes.
Definition: Graph_d.h:167
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:325
The namespace for all OGDF objects.
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
Class for adjacency list elements.
Definition: Graph_d.h:78
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:198