Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
MaxFlowEdmondsKarp.h
Go to the documentation of this file.
1
33#pragma once
34
35#include <ogdf/basic/Graph.h>
37#include <ogdf/basic/List.h>
38#include <ogdf/basic/basic.h>
40
41#include <limits>
42
43namespace ogdf {
44
46
49template<typename TCap>
50class MaxFlowEdmondsKarp : public MaxFlowModule<TCap> {
51private:
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
114public:
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
160 using MaxFlowModule<TCap>::computeFlow;
163 using MaxFlowModule<TCap>::init;
164};
165
166}
Includes declaration of graph class.
Decralation of GraphElement and GraphList classes.
Declaration of doubly linked lists and iterators.
Interface for Max Flow Algorithms.
Basic declarations, included by all source files.
Class for adjacency list elements.
Definition Graph_d.h:143
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition Graph_d.h:161
Class for the representation of edges.
Definition Graph_d.h:364
node target() const
Returns the target node of the edge.
Definition Graph_d.h:402
node source() const
Returns the source node of the edge.
Definition Graph_d.h:399
Doubly linked lists (maintaining the length of the list).
Definition List.h:1451
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition List.h:1547
E popFrontRet()
Removes the first element from the list and returns it.
Definition List.h:1591
bool empty() const
Returns true iff the list is empty.
Definition List.h:286
Computes a max flow via Edmonds-Karp.
bool augmentShortestSourceSinkPath()
If there is an augumenting path: do an interation step. Otherwise return false so that the algorithm ...
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,...
void computeFlowAfterValue()
Implementation of computeFlowAfterValue from the super class. This does nothing, because the algorith...
const node * m_t
Pointer to the sink node.
virtual void init(const Graph &graph, EdgeArray< TCap > *flow=nullptr)
Initialize the problem with a graph and optional flow array. If no flow array is given,...
EdgeArray< TCap > * m_flow
Pointer to (extern) flow array.
void useEpsilonTest(const double &eps)
Change the used EpsilonTest from StandardEpsilonTest to a user given EpsilonTest.
bool isFeasibleInstance() const
Return whether the instance is feasible, i.e. the capacities are non-negative.
const Graph * m_G
Pointer to the given graph.
const node * m_s
Pointer to the source node.
const EdgeArray< TCap > * m_cap
Pointer to the given capacity array.
MaxFlowModule()
Empty Constructor.
TCap computeFlow(EdgeArray< TCap > &cap, node &s, node &t, EdgeArray< TCap > &flow)
Only a shortcut for computeValue and computeFlowAfterValue.
EpsilonTest * m_et
Pointer to the used EpsilonTest.
Class for the representation of nodes.
Definition Graph_d.h:241
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition Graph_d.h:272
RegisteredArray for edges of a graph, specialized for EdgeArray<edge>.
Definition Graph_d.h:717
RegisteredArray for nodes, edges and adjEntries of a graph.
Definition Graph_d.h:659
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
The namespace for all OGDF objects.