Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
MinCostFlowModule.h
Go to the documentation of this file.
1
35#pragma once
36
37#include <ogdf/basic/Graph.h>
39#include <ogdf/basic/basic.h>
42
43namespace ogdf {
44
45
49template<typename TCost>
51public:
54
55 // destruction
56 virtual ~MinCostFlowModule() { }
57
72 virtual bool call(const Graph& G, // directed graph
73 const EdgeArray<int>& lowerBound, // lower bound for flow
74 const EdgeArray<int>& upperBound, // upper bound for flow
75 const EdgeArray<TCost>& cost, // cost of an edge
76 const NodeArray<int>& supply, // supply (if neg. demand) of a node
77 EdgeArray<int>& flow) // computed flow
78 {
79 NodeArray<TCost> dual(G);
80 return call(G, lowerBound, upperBound, cost, supply, flow, dual);
81 }
82
98 virtual bool call(const Graph& G, // directed graph
99 const EdgeArray<int>& lowerBound, // lower bound for flow
100 const EdgeArray<int>& upperBound, // upper bound for flow
101 const EdgeArray<TCost>& cost, // cost of an edge
102 const NodeArray<int>& supply, // supply (if neg. demand) of a node
103 EdgeArray<int>& flow, // computed flow
104 NodeArray<TCost>& dual // computed dual variables
105 ) = 0;
106
107
108 //
109 // static functions
110 //
111
116 static void generateProblem(Graph& G, int n, int m, EdgeArray<int>& lowerBound,
117 EdgeArray<int>& upperBound, EdgeArray<TCost>& cost, NodeArray<int>& supply);
118
119
133 static bool checkProblem(const Graph& G, const EdgeArray<int>& lowerBound,
134 const EdgeArray<int>& upperBound, const NodeArray<int>& supply);
135
136
156 static bool checkComputedFlow(const Graph& G, const EdgeArray<int>& lowerBound,
157 const EdgeArray<int>& upperBound, const EdgeArray<TCost>& cost,
158 const NodeArray<int>& supply, const EdgeArray<int>& flow, TCost& value);
159
178 static bool checkComputedFlow(const Graph& G, const EdgeArray<int>& lowerBound,
179 const EdgeArray<int>& upperBound, const EdgeArray<TCost>& cost,
180 const NodeArray<int>& supply, const EdgeArray<int>& flow) {
181 TCost value;
182 return checkComputedFlow(G, lowerBound, upperBound, cost, supply, flow, value);
183 }
184};
185
186// Implementation
187
188template<typename TCost>
190 EdgeArray<int>& upperBound, EdgeArray<TCost>& cost, NodeArray<int>& supply) {
191 ogdf::randomGraph(G, n, m);
192
193 node s = G.firstNode();
194 node t = G.lastNode();
195
196 for (node v : G.nodes) {
197 G.newEdge(s, v);
198 G.newEdge(v, t);
199 }
200
201 for (edge e : G.edges) {
202 lowerBound[e] = 0;
203 upperBound[e] = (e->source() != s) ? ogdf::randomNumber(1, 10) : ogdf::randomNumber(2, 13);
204 cost[e] = static_cast<TCost>(ogdf::randomNumber(0, 100));
205 }
206
207
208 for (node v = G.firstNode(), vl = G.lastNode(); true; v = v->succ(), vl = vl->pred()) {
209 if (v == vl) {
210 supply[v] = 0;
211 break;
212 }
213
214 supply[v] = -(supply[vl] = ogdf::randomNumber(-1, 1));
215
216 if (vl == v->succ()) {
217 break;
218 }
219 }
220}
221
222template<typename TCost>
224 const EdgeArray<int>& upperBound, const NodeArray<int>& supply) {
225 if (!isConnected(G)) {
226 return false;
227 }
228
229 for (edge e : G.edges) {
230 if (lowerBound[e] > upperBound[e]) {
231 return false;
232 }
233 }
234
235 int sum = 0;
236 for (node v : G.nodes) {
237 sum += supply[v];
238 }
239
240 return sum == 0;
241}
242
243template<typename TCost>
245 const EdgeArray<int>& upperBound, const EdgeArray<TCost>& cost,
246 const NodeArray<int>& supply, const EdgeArray<int>& flow, TCost& value) {
247 value = 0;
248
249 for (edge e : G.edges) {
250 if (flow[e] < lowerBound[e] || upperBound[e] < flow[e]) {
251 return false;
252 }
253
254 value += flow[e] * cost[e];
255 }
256
257 for (node v : G.nodes) {
258 int sum = 0;
259
260 for (adjEntry adj : v->adjEntries) {
261 edge e = adj->theEdge();
262
263 if (e->isSelfLoop()) {
264 continue;
265 }
266
267 if (e->source() == v) {
268 sum += flow[e];
269 } else {
270 sum -= flow[e];
271 }
272 }
273 if (sum != supply[v]) {
274 return false;
275 }
276 }
277
278 return true;
279}
280
281}
Includes declaration of graph class.
Decralation of GraphElement and GraphList classes.
Basic declarations, included by all source files.
Class for adjacency list elements.
Definition Graph_d.h:143
Class for the representation of edges.
Definition Graph_d.h:364
bool isSelfLoop() const
Returns true iff the edge is a self-loop (source node = target node).
Definition Graph_d.h:420
node source() const
Returns the source node of the edge.
Definition Graph_d.h:399
Data type for general directed graphs (adjacency list representation).
Definition Graph_d.h:866
Interface for min-cost flow algorithms.
static bool checkComputedFlow(const Graph &G, const EdgeArray< int > &lowerBound, const EdgeArray< int > &upperBound, const EdgeArray< TCost > &cost, const NodeArray< int > &supply, const EdgeArray< int > &flow, TCost &value)
checks if a computed flow is a feasible solution to the given problem instance.
virtual bool call(const Graph &G, const EdgeArray< int > &lowerBound, const EdgeArray< int > &upperBound, const EdgeArray< TCost > &cost, const NodeArray< int > &supply, EdgeArray< int > &flow, NodeArray< TCost > &dual)=0
Computes a min-cost flow in the directed graph G.
static void generateProblem(Graph &G, int n, int m, EdgeArray< int > &lowerBound, EdgeArray< int > &upperBound, EdgeArray< TCost > &cost, NodeArray< int > &supply)
Generates an instance of a min-cost flow problem with n nodes and m + n edges.
static bool checkComputedFlow(const Graph &G, const EdgeArray< int > &lowerBound, const EdgeArray< int > &upperBound, const EdgeArray< TCost > &cost, const NodeArray< int > &supply, const EdgeArray< int > &flow)
checks if a computed flow is a feasible solution to the given problem instance.
MinCostFlowModule()
Initializes a min-cost flow module.
static bool checkProblem(const Graph &G, const EdgeArray< int > &lowerBound, const EdgeArray< int > &upperBound, const NodeArray< int > &supply)
Checks if a given min-cost flow problem instance satisfies the preconditions.
virtual bool call(const Graph &G, const EdgeArray< int > &lowerBound, const EdgeArray< int > &upperBound, const EdgeArray< TCost > &cost, const NodeArray< int > &supply, EdgeArray< int > &flow)
Computes a min-cost flow in the directed graph G.
Class for the representation of nodes.
Definition Graph_d.h:241
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
Declaration of graph generators.
bool isConnected(const Graph &G)
Returns true iff G is connected.
void randomGraph(Graph &G, int n, int m)
Creates a random graph.
int randomNumber(int low, int high)
Returns random integer between low and high (including).
The namespace for all OGDF objects.
Declaration of simple graph algorithms.